0

Im trying to get the global minimum of a non linear function with linear constraints. I share you the code:

rho = 1.0
g = 9.8
C = 1.0 #roughness coefficient
J = rho*g/(float(1e6)) #constant of function

capacity = [0.3875, 0.607,  0.374] #Supply data
demand = [0.768, 0.315, 0.38,] #Demand data

m= len(capacity)
n = len(demand)

x0 = np.array(m*n*[0.01]) #Initial point for algorithm


# In[59]:


#READ L AND d FROM ARCGIS !!!
L = (314376.57, 277097.9663,    253756.9869 ,265786.5632,   316712.6028,    232857.1468,    112063.9914,    135762.94,  131152.8206)
h= (75, 75, 75, 75, 75, 75, 1320,75,75)
K = np.zeros(m*n)
N=np.zeros(m*n)

# In[60]:


#Each path has its own L,d, therefore, own constant   



# In[61]:

#np.seterr(all='ignore')

def diameter(x):
  d = 1.484
  if x >= 0.276:
    d = 1.484
  elif x >= 0.212:
    d = 1.299
  elif x >= 0.148:
    d = 1.086
  elif x >= 0.0975:
    d = 0.881
  elif x >= 0.079:
    d = 0.793
  elif x >= 0.062:
    d = 0.705
  elif x >= 0.049:
    d = 0.626
  elif x >= 0.038:
    d = 0.555
  elif x >= 0.030:
    d = 0.494
  elif x >= 0.024:
    d = 0.441
  elif x >= 0.0198:
    d = 0.397
  elif x >= 0.01565:
    d = 0.353
  elif x >= 0.0123:
    d = 0.313
  elif x >= 0.0097:
    d = 0.278
  elif x >= 0.0077:
    d = 0.247
  elif x >= 0.0061:
    d = 0.22
  elif x >= 0.0049:
    d = 0.198
  elif x >= 0.00389:
    d = 0.176
  elif x >= 0.0032:
    d = 0.159
  elif x >= 0.0025:
    d = 0.141
  elif x >= 0:
    d = 0.123
  return d


#Definition of Objetive function
def objective(x):
  sum_obj = 0.0
  for i in range(len(L)): 
      K = 10.674*L[i]/(C**1.852*diameter(x[i])**4.871)
      N[i] = K*x[i]**2.852*J+x[i]*J*h[i]
      sum_obj = sum_obj + N[i]
  return sum_obj

print(str(objective(x0)))

#Definition of the constraints 

GB=[]
for i in range(m):
        GB.append(n*i*[0]+n*[1]+(m-1-i)*n*[0])
P=[]
for i in range(n):
    P.append(m*(i*[0]+[1]+(n-1-i)*[0]))

DU=np.array(GB+P)

lb = np.array(m*[0] + demand) # Supply
ub = np.array(capacity + n*[np.inf]) # Demand


# In[62]:
b = (0, 1)
bnds = []
for i in range(m*n):
    bnds.append(b)
cons = LinearConstraint(DU,lb,ub)
solution = differential_evolution(objective,x0,cons,bnds)
x = solution.x

# show initial objective
print('Initial SSE Objective: ' + str(objective(x0)))


# show final objective
print('Final SSE Objective: ' + str(objective(x)))

# print solution
print('Solution Supply to Demand:')
print('Q = ' + str(((np.around(x,6)).reshape(10,18))))

I dont know why, but when I run appear the following:

"if strategy in self._binomial: TypeError: unhashable type: 'list'

Anyone have gotten the same mistake ? Its my first time trying to solve optimization problem, so I need a little bit of help. Any advice is welcome !! Thanks a lot !

1 Answers1

0

You're not calling differential_evolution correctly. For your example you should call it as:

differential_evolution(objective, bnds, contraints=(cons,))

(I'm not sure if there are additional problems)

Andrew Nelson
  • 460
  • 3
  • 11