Reputation: 395
I am using pyomo
and ipopt
to solve a nonlinear problem involving over 100 variables. I don't know too much math to know what the options in ipopt mean.
If my conditions are set relatively loosely, I find that every time my program is restarted, the results are far different from the last, and even if they are optimal
, only some of them are satisfactory to me. I've tried to make the constraints a little bit tighter, but that also reduces the probability that I'm going to get a valid result.
So my approach is to loop solver.solve
multiple times, storing the appropriate result into a file and then constrainting it. If I put model.cons = ConstraintList()
into the loop, the program is running too slow; If I just put solver.solve(...)
in the loop, as long as I don't run the program again, I'll get almost the same solution no matter how many times I loop through it.
I am not sure whether this problem is about pyomo
or ipopt
. I hope anyone can help me so that I can get different solutions in each cycle, thank you.
Here is my code:
# Create model is extremly slow, can't bear it in loop
model = ConcreteModel()
model.x = Var(range(n),range(n),[0,1],within=NonNegativeIntegers)
model.tolerance = Param(initialize=s_tolerance, mutable=True)
# Some constraints
model.cons = ConstraintList()
for j in range(n):
model.cons.add(sum(model.x[i,j,0] for i in range(n)) == data[j][3])
model.cons.add(sum(model.x[i,j,1] for i in range(n)) == data[j][4])
# Get almost the same solutions with these code
while True:
# Not work for random solution
seed = random.uniform(-0.5, 0.5)
model.tolerance.set_value(seed + s_tolerance)
solver = SolverFactory('ipopt', keepfiles=False)
results = solver.solve(model, tee=False, symbolic_solver_labels=False, options={'max_iter':2000})
if good_result(results):
break
Upvotes: 0
Views: 383