Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- This code minimizes x1 * x4 * (x1 + x2 + x3) + x3
- s.t. x1 * x2 * x3 * x4 >= 25
- s.t. x1^2 + x2^2 + x3^2 + x4^2 = 40
- s.t. 1 <= x1,x2,x3,x4,x5 <= 5
- '''
- import numpy as np
- from scipy.optimize import minimize
- # objective function is the literal objective, what you want to achieve
- 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[1]*x[2]*x[3]-25.0
- def constraint2(x):
- sum_eq = 40.0
- for i in range(4):
- sum_eq = sum_eq - x[i]**2
- return sum_eq
- # initial guesses
- n = 4
- x0 = np.zeros(n)
- x0[0] = 1.0
- x0[1] = 5.0
- x0[2] = 5.0
- x0[3] = 1.0
- # show initial objective
- print('Initial Objective: ' + str(objective(x0)))
- # optimize
- b = (1.0,5.0)
- bnds = (b, b, b, b)
- con1 = {'type': 'ineq', 'fun': constraint1}
- con2 = {'type': 'eq', 'fun': constraint2}
- cons = ([con1,con2])
- solution = minimize(objective,x0,method='SLSQP',\
- bounds=bnds,constraints=cons)
- x = solution.x
- # show final objective
- print('Final Objective: ' + str(objective(x)))
- # print solution
- print('Solution')
- print('x1 = ' + str(x[0]))
- print('x2 = ' + str(x[1]))
- print('x3 = ' + str(x[2]))
- print('x4 = ' + str(x[3]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement