2

我有一个在 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 访问实际的决策变量 match[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()); 

现在,我如何访问决策变量“match”?到目前为止,我已经开始

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

但我似乎无法更进一步。非常感谢您的帮助!非常感谢!

4

2 回答 2

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
于 2013-04-12T22:30:56.733 回答
1

您可以像这样通过 IloCplex-Object 获取变量值:

cplex.getValue([variable reference]);

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

复杂文档

于 2013-02-20T16:00:25.693 回答