Question: I am trying to minimize this the objective function in this script in python 2.7.12 and keep getting the error 'raise ValueError('Constraint %d has no
I am trying to minimize this the objective function in this script in python 2.7.12 and keep getting the error 'raise ValueError('Constraint %d has no function defined.' % ic) ValueError: Constraint 0 has no function defined.' Can someone tell me what I'm doing wrong? The function I'm minimizing along witht the constraints are on the thumbnail of this video http://apmonitor.com/che263/index.php/Main/PythonOptimization: but something just goes wrong when I compile this script:
import numpy as np from scipy.optimize import minimize
def objective(x): x1 = x[0] x2 = x[1] x3 = x[2] x4 = x[3] return x1*x4*(x1+x2+x3)+x3 def constraint1(x): return x[0]*x[3]*(x[0]+x[1]+x[2])+x[2]
def constraint2(x): sum_sq = 40 for i in range (4): sum_sq = sum_sq = x[i]**2 return sum_sq x0 = [1,5,5,1] print(objective(x0))
b = (1.0,5.0) bnds = (b,b,b,b) con1 = {'type': 'ineq', 'function': constraint1} con2 = {'type': 'eq', 'function': constraint2} cons = [con1,con2]
solution = minimize(objective,x0,method = 'SLSQP',\ bounds=bnds, constraints=cons) print solution
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
