2016-09-14 3 views
1

CPlex java-API로 가져 오려는 OptimizationModel abc.lp이 있다고 가정 해 봅니다. 내가 사용 : importModel 함수 (click) 가져올 수 있습니다. 이제 제약 변수 또는 객관적으로 의사 결정 변수의 요인을 변경하고 싶습니다. 예를 들어 다음과 같이 가져온 모델 abc.lp 외모 : 나 factor1factor2를 들어가져온 cplex에서 동적으로 용어 요소 설정 모델

Objective: Minimize <factor1>x1 + <factor2>x2

Constraint: <factor1>x1 + <factor2>x2 <= 40 

함수의 입력 매개 변수입니다. 그래서 내가 얻습니다 :

Cplex-API를 사용하여 가져온 모델에서 요인을 동적으로 설정하는 편리한 방법이 있습니까?

고맙습니다.

+0

내 대답이 문제를 해결합니까? 해결해야 할 부분이 남아 있습니까? – rkersh

답변

1

예, 가능합니다. 그것은 나에게 적어도 직관적이지 않습니다.

는 여기에 LP (선형 목적과 제약) 가정 예제 코드 조각입니다 : 여기

// Read model from file with name args[0] into cplex optimizer object 
cplex.importModel(args[0]); 

// Get the objective and modify it. 
IloObjective obj = cplex.getObjective(); 
IloLinearNumExpr objExpr = (IloLinearNumExpr) obj.getExpr(); 
IloLinearNumExprIterator iter = objExpr.linearIterator(); 
// Loop through the linear objective and modify, as necessary. 
while (iter.hasNext()) { 
    IloNumVar var = iter.nextNumVar(); 
    System.out.println("Old coefficient for " + var + ": " + iter.getValue()); 
    // Modify as needed. 
    if (var.getName().equals("x1")) { 
     iter.setValue(42); 
     System.out.println("New coefficient for " + var + ": " + iter.getValue()); 
    } 
} 
// Save the changes. 
obj.setExpr(objExpr); 

// Assumes that there is an LP Matrix. The fact that we used 
// importModel() above guarantees that there will be at least 
// one. 
IloLPMatrix lp = (IloLPMatrix) cplex.LPMatrixIterator().next(); 
for (int i = 0; i < lp.getNrows(); i++) { 
    IloRange range = lp.getRange(i); 
    System.out.println("Constraint " + range.getName()); 
    IloLinearNumExpr conExpr = (IloLinearNumExpr) range.getExpr(); 
    IloLinearNumExprIterator conIter = conExpr.linearIterator(); 
    // Loop through the linear constraints and modify, as necessary. 
    while (conIter.hasNext()) { 
     IloNumVar var = conIter.nextNumVar(); 
     System.out.println("Coefficient for " + var + ": " + conIter.getValue()); 
     // Modify as needed (as above). 
     if (var.getName().equals("x1")) { 
      conIter.setValue(42); 
      System.out.println("New coefficient for " + var + ": " + conIter.getValue()); 
     } 
    } 
    // Save changes (as above). 
    range.setExpr(conExpr); 
} 
cplex.exportModel("modified.lp"); 

// Solve the model and display the solution if one was found 
if (cplex.solve()) { 
    // do something here. 
} 

을, 우리는 "1 개"라는 이름의 변수를 찾고 있습니다. 목표와 모든 선형 제약 조건에서 계수를 42로 설정했습니다. println은 디버깅 용입니다. 이 작업을 신속하게 수행 했으므로 테스트를 수행해야합니다. 그렇지 않으면 필요에 맞게 수정할 수 있어야합니다. 희망이 도움이됩니다.