Topic
IC4NOTICE: developerWorks Community will be offline May 29-30, 2015 while we upgrade to the latest version of IBM Connections. For more information, read our upgrade FAQ.
2 replies Latest Post - ‏2013-01-02T07:25:25Z by SystemAdmin
SystemAdmin
SystemAdmin
7929 Posts
ACCEPTED ANSWER

Pinned topic How to accessing intermediate solutions without declaring vars in Java?

‏2012-12-28T07:22:06Z |
I find the example in DOCS: here.
Well,in my project, the vars declarations are in .mod file only and I import them using :
IloOplRunConfiguration rc0 = oplF.createOplRunConfiguration(DATADIR
                + "/mod_v1.mod", DATADIR + "/data_v1_2.dat");//The model definition,specify the data source for the model

Now, i want to print each solutions,like:
while(cp.next()){
...
                IloOplElement K = oplModel.getElement("K");//取出解中的K值oplModel=rc0.getOplModel()
                double KNum = K.asNum();//取出解中的K值
                newK=Double.toString(KNum);//取出解中的K值
...
                System.out.println(newK);
}

The problem is: value of "newK" is always same as the first solution that cp has found.

Does anyone familiar with this problem?
Updated on 2014-03-24T22:43:24Z at 2014-03-24T22:43:24Z by iron-man
  • SystemAdmin
    SystemAdmin
    7929 Posts
    ACCEPTED ANSWER

    Re: How to accessing intermediate solutions without declaring vars in Java?

    ‏2012-12-31T03:52:28Z  in response to SystemAdmin
    I found a solution:
    
    IloOplFactory oplF = 
    
    new IloOplFactory();
    //The model definition IloOplRunConfiguration rc0 = oplF.createOplRunConfiguration(DATADIR + 
    "/mod_v1.mod", DATADIR + 
    "/data_v1_2.dat");
    //The model definition,specify the data source for the model IloOplModel oplModel=rc0.getOplModel();
    //The model itself oplModel.generate();
    //The model itself IloIntVar K = oplModel.getElement(
    "K").asIntVar();
    //取出解中的K值 IloIntVarMap CenInfs =oplModel.getElement(
    "CenInfs").asIntVarMap();
    //取出解中的CenInfs值 IloIntVarMap Compositions = oplModel.getElement(
    "Compositions").asIntVarMap();
    //取出解中的Compositions值  IloCP cp=rc0.getCP();
    //The solver cp.startNewSearch();
    //Start solving 
    
    int solCount=0;
    //计数:解的数量 
    
    while(cp.next())
    {
    //找到一个解,进行输出 solCount++;
    //解的数量加一 String newObj=Double.toString(cp.getObjValue());
    //解对应的目标函数值 String newK=
    "";
    //对应决策变量K:TAZ个数(见.mod文件) List<String> newCenInfs=
    
    new ArrayList<String>();
    //对应决策变量CenInfs:TAZ中心影响区(INF)的ID(见.mod文件) Map<String,List<String>> newCompositions=
    
    new HashMap<String,List<String>>();
    //对应决策变量Compositions:TAZ组成矩阵(见.mod文件) 
    
    double KNum =cp.getValue(K);
    //取出解中的K值 newK=Double.toString(KNum);
    //取出解中的K值 
    
    for(
    
    int i=1;i<=CenInfs.getSize();i++)
    {
    //取出解中的CenInfs值 
    
    if(cp.getValue(CenInfs.get(i))==1)
    { newCenInfs.add(Integer.toString(i));
    //取出解中的CenInfs值,保存被选中为中心INF的INF的Id,例如:[2,3,4,11] 
    } 
    } 
    //取出解中的Compositions值 
    
    for(
    
    int i=0;i<newCenInfs.size();i++)
    {;
    //取出解中的Compositions值:取出第i个中心INF的TAZ的Compositions String thisCenInfIdStr=newCenInfs.get(i);
    //例如:i=0,thisCenInfIdStr="2" 
    
    int thisCenInfId=Integer.parseInt(thisCenInfIdStr);
    //int型的thisCenInfIdStr List<String> CellIdsOfNewComps=
    
    new ArrayList<String>(); 
    
    for(
    
    int j=1;j<=Compositions.getSub(thisCenInfId).getSize();j++)
    { 
    
    if(cp.getValue(Compositions.getSub(thisCenInfId).get(j))==1)
    { String thisCellIdStr=Integer.toString(j); CellIdsOfNewComps.add(thisCellIdStr); 
    } 
    } newCompositions.put(thisCenInfIdStr,CellIdsOfNewComps); 
    } Solution oneSol=
    
    new Solution(newObj,newK,newCenInfs,newCompositions);
    //根据该解的结果创建一个solution printSolution(oneSol); writeSolution_TXT(oneSol,fatFolder,Integer.toString(solCount)); fea_Sols.add(oneSol); 
    }
    
  • SystemAdmin
    SystemAdmin
    7929 Posts
    ACCEPTED ANSWER

    Re: How to accessing intermediate solutions without declaring vars in Java?

    ‏2013-01-02T07:25:25Z  in response to SystemAdmin
    Since your code uses OPL you are probably better off asking this question on the OPL forum. There you will find the experts to answer your question and/or discuss the solution you found.