有没有针对非线性和非标量目标函数的Python优化包
Is there a Python optimization package for non linear and non scalar target functions
我希望解决具有以下特征的问题:
max {x * max (f(var_1),f(var_2)) + y * min (f(var_3),f(var_4)) + ...}
s.t.
b_0 <= x <= b_1
c_0 <= y <= c_1
d_0 <= var_1 <= d_1
d_2 <= var_2 <= d_3
d_4 <= var_3 <= d_5
d_6 <= var_4 <= d_7
等...
使用 python.
谢谢
我可以使用 CasADi software, please follow this instruction (https://web.casadi.org/get/) 将软件包安装到您的计算机中来解决此类问题
在这里,我为您提供解决问题的方法,您可以根据需要更改功能(f1,..f4)和值。
from casadi import *
x = SX.sym('x')
y = SX.sym('y')
var_1 = SX.sym('var_1')
var_2 = SX.sym('var_2')
var_3 = SX.sym('var_3')
var_4 = SX.sym('var_4')
f1 = x + var_1 -y
f2 = x + var_2 -y
f3 = var_3*x
f4 = var_4-y
obj = fmax(f1, f2) + y*fmin(f3,f4)
opt_val = vertcat(x, y, var_1, var_2, var_3, var_4)
nlp_prob = {'f':obj, 'x':opt_val, 'p':[]}
opts = {}
opts["expand"] = True
opts["ipopt.max_iter"] = 100
opts["ipopt.tol"] = 1e-4
opts["ipopt.print_level"] = 0
opts["print_time"] = 1
opts["ipopt.acceptable_tol"] = 1e-8
solver = nlpsol('solver', 'ipopt', nlp_prob, opts)
lbx = DM(opt_val.size1(),1)
ubx = DM(opt_val.size1(),1)
lbx[0] = -100 #b_0
lbx[1] = -100 #c_0
lbx[2] = -100 #d_0
lbx[3] = -100 #d_2
lbx[4] = -100 #d_4
lbx[5] = -100 #d_6
ubx[0] = 500 #b_1
ubx[1] = 500 #c_1
ubx[2] = 500 #d_1
ubx[3] = 500 #d_3
ubx[4] = 500 #d_5
ubx[5] = 500 #d_7
# initial values for x,y, val_1, val_2, val_3, val_4
x0 = DM([[3],[6],[34],[3],[23],[25]])
args = {'lbx':lbx, 'ubx':ubx, 'lbg':-inf, 'ubg':inf, 'p':[], 'x0':x0}
sol = solver(**args)
print("======Solution=====")
sol = sol['x'].full().flatten()
print("x: ", sol[0])
print("y: ", sol[1])
print("val_1: ", sol[2])
print("val_2: ", sol[3])
print("val_3: ", sol[4])
print("val_4: ", sol[5])
预期输出
======Solution=====
('x: ', 3.0756501068719735)
('y: ', -99.999974650383308)
('val_1: ', -98.442843531628299)
('val_2: ', 108.68842844611186)
('val_3: ', 195.0797522765082)
('val_4: ', 499.99687090550719)
有什么不清楚的请告诉我
我希望解决具有以下特征的问题:
max {x * max (f(var_1),f(var_2)) + y * min (f(var_3),f(var_4)) + ...}
s.t.
b_0 <= x <= b_1
c_0 <= y <= c_1
d_0 <= var_1 <= d_1
d_2 <= var_2 <= d_3
d_4 <= var_3 <= d_5
d_6 <= var_4 <= d_7
等...
使用 python.
谢谢
我可以使用 CasADi software, please follow this instruction (https://web.casadi.org/get/) 将软件包安装到您的计算机中来解决此类问题
在这里,我为您提供解决问题的方法,您可以根据需要更改功能(f1,..f4)和值。
from casadi import *
x = SX.sym('x')
y = SX.sym('y')
var_1 = SX.sym('var_1')
var_2 = SX.sym('var_2')
var_3 = SX.sym('var_3')
var_4 = SX.sym('var_4')
f1 = x + var_1 -y
f2 = x + var_2 -y
f3 = var_3*x
f4 = var_4-y
obj = fmax(f1, f2) + y*fmin(f3,f4)
opt_val = vertcat(x, y, var_1, var_2, var_3, var_4)
nlp_prob = {'f':obj, 'x':opt_val, 'p':[]}
opts = {}
opts["expand"] = True
opts["ipopt.max_iter"] = 100
opts["ipopt.tol"] = 1e-4
opts["ipopt.print_level"] = 0
opts["print_time"] = 1
opts["ipopt.acceptable_tol"] = 1e-8
solver = nlpsol('solver', 'ipopt', nlp_prob, opts)
lbx = DM(opt_val.size1(),1)
ubx = DM(opt_val.size1(),1)
lbx[0] = -100 #b_0
lbx[1] = -100 #c_0
lbx[2] = -100 #d_0
lbx[3] = -100 #d_2
lbx[4] = -100 #d_4
lbx[5] = -100 #d_6
ubx[0] = 500 #b_1
ubx[1] = 500 #c_1
ubx[2] = 500 #d_1
ubx[3] = 500 #d_3
ubx[4] = 500 #d_5
ubx[5] = 500 #d_7
# initial values for x,y, val_1, val_2, val_3, val_4
x0 = DM([[3],[6],[34],[3],[23],[25]])
args = {'lbx':lbx, 'ubx':ubx, 'lbg':-inf, 'ubg':inf, 'p':[], 'x0':x0}
sol = solver(**args)
print("======Solution=====")
sol = sol['x'].full().flatten()
print("x: ", sol[0])
print("y: ", sol[1])
print("val_1: ", sol[2])
print("val_2: ", sol[3])
print("val_3: ", sol[4])
print("val_4: ", sol[5])
预期输出
======Solution=====
('x: ', 3.0756501068719735)
('y: ', -99.999974650383308)
('val_1: ', -98.442843531628299)
('val_2: ', 108.68842844611186)
('val_3: ', 195.0797522765082)
('val_4: ', 499.99687090550719)
有什么不清楚的请告诉我