Two models that have same constraints but with different parameters applied.
AnsweredGreetings to everyone who may see this question. I would like to build two Gurobi models. These two models have the same constraints but must be applied with different parameters. But after I defined a function as MODEL(a, b, c), I called it twice by using model(a, b, c) and model(e, f, g), and they gave me the exact same results.
Here is a more detailed example.
After applying this, I get two exact same results (x_1 == x_2). However, they should be different as their parameters are different.
# define a model function
def MODEL(a, b, c):
MODEL = gp.Model('')
x = MODEL.addVars(n_1, n_2, vtype=...)
MODEL.addConstrs(x[i, j] <= a for i in range(n_1) for j in range(n_2))
MODEL.addConstrs(x[i] >= b for i in range(n_1))
MODEL.addConstrs(x[j] <= c for j in range(n_2))
MODEL.update()
MODEL.optimize()
return MODEL.x.X
# Call it for the first time
x_1 = MODEL(a,b,c)
# Call it for the second time
x_2 = MODEL(e,d,f)
-
Hi Yujie,
There are a couple of things in your code which are a bit suspicious:
- x is defined to be a variables with two index sets n_1, n_2, but then in the 2nd and 3rd set of constraints you index it with one value (x[i], x[j])
- the return statement MODEL.x.X doesn't make sense as the x attribute on MODEL doesn't exist
For the purposes of your question I'll just assume these are typos in an example created for the sake of an example. Your claim that you get the same solution for each of the two model instances, and that they should be different, is essentially claiming that one of these models produces a non-optimal solution. In your example I cannot see an objective being set, and so the claim would further be reduced to one which states that the solution is not feasible, which seems unlikely.
Does your model have an objective value? Is the solution returned either not feasible, or non-optimal, for either of the model instances?
- Riley
0 -
Hi Riley,
Thank you for your response. Maybe my example listed above is over-simplified and induces more questions on irrelevant things, I'm terribly sorry about that.
Please allow me to rephrase my inquiry, that is, how can I repeatedly solve an MIP model while modifying certain input parameters for each computation? It is doable to achieve this target if I specify all constraints and parameters into different MIP models individually. But this would result in lengthy code. Hence, I would like to explore alternative methods to do that.
I have constructed an MIP model using Gurobi as a Python function and re-called it multiple times with varying parameters in order to get diverse optimization strategies. However, I encountered an issue where the results remained identical, despite my expectation that they should be diverged. I attempted to address this issue by using Model.reset() but doesn't work. An example is shown as follows for illustrative purposes.
Again, I appreciate your time and guidance in resolving this matter.
def model_defined(a, b, c):
MIP Optimization Model using Gurobi
Save the optimal result values to 'a'
return a
result_1 = model_defined(a, b, c)
result_2 = model_defined(e, f, g)
# the results shows that result_1 == result_2
# but that is incorrect.0 -
I have constructed an MIP model using Gurobi as a Python function and re-called it multiple times with varying parameters in order to get diverse optimization strategies. However, I encountered an issue where the results remained identical, despite my expectation that they should be diverged.
Rebuilding a new model from scratch each time using different data should absolutely result in different models. Are you positive the optimal solution should change when the model is solved with the different data? We can take a closer look if you post a minimal, self-contained code example that shows this behavior.
That said, there are other approaches to modifying the model data. One approach is to individually change the model's coefficients. Let's say you add the the constraint \(2x <= 5\) to your model in the following manner:
x = model.addVar(name="x")
c = model.addConstr(2*x <= 5)You can change a linear constraint coefficient via Model.chgCoeff():
# change constraint to 3*x <= 5
model.chgCoeff(c, x, 3)You can change the constraint's right-hand side by modifying the RHS linear constraint attribute:
# now change constraint to 3*x <= 6
c.RHS = 6You can change a variable's linear objective coefficient by modifying the Obj variable attribute:
# change x's objective term to 4*x
x.Obj = 4You can change variable bounds via the LB and UB variable attributes:
# 1 <= x <= 9
x.LB = 1
x.UB = 9Gurobi's support for multi-scenario optimization may also be interesting to you. It allows you to define scenarios, each of which corresponds to different data (linear objective coefficients, variable bounds, and constraint right-hand sides). When you call Model.optimize(), Gurobi then finds solutions for all of the individual scenarios.
0
Please sign in to leave a comment.
Comments
3 comments