如何从java访问ilog决策变量?

时间:2012-08-23 13:58:37

标签: java simulation cplex ilog

我有一个在IBM ILOG CPLEX Optimization Studio中建模的线性问题,它返回正确的解决方案,即客观值。 出于模拟目的,我使用ILOG模型模型文件和我从java调用的数据文件:

IloOplFactory.setDebugMode(false);
IloOplFactory oplF = new IloOplFactory();
IloOplErrorHandler errHandler = oplF.createOplErrorHandler(System.out);
IloOplModelSource modelSource = oplF.createOplModelSource("CDA_Welfare_Examination_sparse2.mod");
IloCplex cplex = oplF.createCplex();
IloOplSettings settings = oplF.createOplSettings(errHandler);
IloOplModelDefinition def=oplF.createOplModelDefinition(modelSource,settings);
IloOplModel opl=oplF.createOplModel(def,cplex);

String inDataFile =  path;
IloOplDataSource dataSource=oplF.createOplDataSource(inDataFile);
opl.addDataSource(dataSource);

opl.generate();
opl.convertAllIntVars(); // converts integer bounds into LP compatible format
if (cplex.solve()){                              
 }
else{
System.out.println("Solution could not be achieved, probably insufficient memory or some other weird problem.");
             }

现在,我想从java中访问实际的决策变量匹配[Matchable]。

在ILOG CPLEX Optimization Studio中,我使用以下命名法:

tuple bidAsk{
int b;
int a;  
}

{bidAsk} Matchable = ...;

dvar float match[Matchable];

在Java中,我按以下方式访问目标值(工作正常):

double sol = new Double(opl.getSolutionGetter().getObjValue()); 

现在,我如何访问决策变量"匹配"?到目前为止,我已经开始

IloOplElement dVarMatch = opl.getElement("match");

但我似乎无法继续下去。非常感谢帮助!非常感谢!

2 个答案:

答案 0 :(得分:2)

你走在正确的轨道上。您需要获取代表Matchable中每个有效bidAsk的元组,然后使用元组作为决策变量对象的索引。这是Visual Basic中的一些示例代码(我现在正在编写的内容,应该很容易转换为java):

  ' Get the tuple set named "Matchable"
  Dim matchable As ITupleSet = opl.GetElement("Matchable").AsTupleSet
  ' Get the decision variables named "match"
  Dim match As INumVarMap = opl.GetElement("match").AsNumVarMap

  ' Loop through each bidAsk in Matchable
  For Each bidAsk As ITuple In matchable
     ' This is the current bidAsk's 'b' value
     Dim b As Integer = bidAsk.GetIntValue("b")

     ' This is the current bidAsk's 'a' value
     Dim a As Integer = bidAsk.GetIntValue("a")

     ' this is another way to get bidAsk.b and bidAsk.a
     b = bidAsk.GetIntValue(0)
     a = bidAsk.GetIntValue(1)

     ' This is the decision variable object for match[<b,a>]
     Dim this_variable As INumVar = match.Get(bidAsk)

     ' This is the value of that decision variable in the current solution
     Dim val As Double = opl.Cplex.GetValue(this_variable)
  Next

答案 1 :(得分:1)

您可以通过IloCplex-Object获取变量值,如下所示:

cplex.getValue([variable reference]);

我从未导入过这样的模型。在java中创建模型时,很容易对决策变量的引用,但应该有一种方法来获取变量。您可以查看文档:

cplex docu