gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 05:34:52 27 4
gpt4 key购买 nike

我有一个在 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()); 

现在,我如何访问决策变量“匹配”?到目前为止,我已经开始
IloOplElement dVarMatch = opl.getElement("match");

但我似乎不能再进一步了。非常感谢帮助!非常感谢!

最佳答案

你在正确的轨道上。您需要获取代表 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

关于java - 如何从java访问ilog决策变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12093374/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com