简体   繁体   中英

python scipy.optimize.minimize define constraints

Maybe quite a specific question I am using the scipy optimize function to get new value for seven variables. the seven value are set by the array x0 for the initial input. a, b, c, d are actually int value.

When running the complete code and the minimize function, everything is working perfectly.

If i print how my seven variables evolve after each iteration i can see that a, b, c, d are converted to float to modify it by small step.

Though i am still getting to my final result, it would be great if i can indicate at some constraint that those 4 variable are integer only, so the optimize function would perform less iterations, and not try to set variables a, b,c ,d to 8.00000164 or other floating point but directly to 6,7, 8, 9, 10... for example

I am not sure it is even possible, but if anybody know how to, i'll be happy to learn.

Below is the important part of the code showing how the minimize function is setup.

a = 8
b = 23
c = 54
d = 89

... some code before and after

# FUNCTION - SOLVER CONSTRAINTS
def solver_constraint_1(t):
  return t[1] - t[0]
def solver_constraint_2(t):
  return t[2] - t[1]
def solver_constraint_3(t):
  return t[3] - t[2]

... some code before and after

x0 = [a, b, c, d, 1.0, 1.0, 1.0]

x_bounds = [[0, 20], [10, 60], [20, 80], [60, 100], [0.25, 2.5], [0.25, 2.5], [0.25, 2.5]]

x_cons = ({'type': 'ineq', 'fun': solver_constraint_1}, 
          {'type': 'ineq', 'fun': solver_constraint_2},    
          {'type': 'ineq', 'fun': solver_constraint_3})

solution = minimize(objective, x0, method='SLSQP', bounds=x_bounds, constraints=x_cons, options={'ftol': 1e-8, 'maxiter': 1000, 'disp': True})

To give the short answer and with the help of @kazemakase in the above reply, it is simply not easily possible to fix the step size to be integer in the minimize function. Also doing integer programming is in fact a much complex problem to solve than the linear programming

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM