I want to optimize a problem in Python using scipy
.
To achieve this goal:
I define an array of a certain length, but without defining its elements explicitly.
Then, I need to use this array to optimize the problem.
In the last step, I want to print this array as a result.
Here is the simple example:
JavaScript
x
16
16
1
import numpy as np
2
from scipy.optimize import minimize
3
4
b0 = [1,3,4,2,5]
5
a = [] # This is the array I want to define. The length of the a should be equal to the b.
6
7
def objective(b):
8
print(a*b)
9
z=a.b.(-1)
10
return z
11
12
def constraint(b)
13
return b[0]*b[1]*b[2]*b[3]*b[4]
14
15
print(objective(b0))
16
Advertisement
Answer
Python has following syntax:
JavaScript
1
2
1
array_name = [some_value]*length
2
it creates an array filled with some_value with provided length
so try:
JavaScript
1
2
1
a = [None]*len(b)
2
or if you want to not change the type:
JavaScript
1
2
1
a=[0]*len(b)
2
as an alternative use numPy array