2012-08-23 28 views
2

我有一個在IBM ILOG CPLEX Optimization Studio中建模的線性問題,它返回正確的解決方案,即客觀值。 爲了仿真目的,我使用ILOG模型的模型文件和我都從Java調用數據文件:如何從java訪問ilog決策變量?

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訪問實際的決策變量匹配[可匹配。

在ILOG CPLEX優化工作室我用下面的nomenclatura:

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

你在正確的軌道上。您需要在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

你可以通過這樣的IloCplex-對象獲得的變量值:

cplex.getValue([variable reference]); 

我從來沒有進口這樣一個模式。在java中創建模型時,對決策變量的引用很容易掌握,但應該有一種方法來獲取變量。你可以查看文檔:

cplex docu