Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
479 views
in Technique[技术] by (71.8m points)

opl - How can I add new constraints to existing model in Cplex Script (flow control)?

In my heuristic algorithm I solve the model multiple times with different data and after each solve I need to add some new constraints to the existing model and solve again. I want to do this in Cplex Script (flow control).

question from:https://stackoverflow.com/questions/65862686/how-can-i-add-new-constraints-to-existing-model-in-cplex-script-flow-control

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I am not sure whether it is an optimal way to do it (but I did not find a better one), but if your constraints have known structure, you might edit (in runtime) a set of bounds in a .dat file and let CPLEX generate constraints from it.

dvar float k;

{float} bounds = ...; //data.dat: bounds = {4.0};

main {
  var model = thisOplModel
  model.generate()
  cplex.solve()
  writeln(cplex.getObjValue())

  var model2 = new IloOplModel(model.modelDefinition, cplex)
  var data = model.dataElements
  data.bounds.add(3.0)
  model2.addDataSource(data)
  model2.generate()
  cplex.solve()
  writeln(cplex.getObjValue())
}

maximize k;

subject to {
  forall (b in bounds) k <= b;
}

The output is then

4
3

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...