2016-07-09 2 views
0

내 C++ Concert Cplex에 문제가 있습니다. 최단 경로 문제를 다시 만들려고합니다. 텍스트 파일 출력은 : 나는 점점 계속 출력은 그러나Cplex 모델 : No solution

IloCplex spp(model); 
    spp.setParam(IloCplex::RootAlg, IloCplex::AutoAlg); 
    spp.solve(); 
    IloArray<IloNumArray> vals(env); 
    env.out() << "Solution status = " << spp.getStatus() << endl; 
    env.out() << "Solution value = " << spp.getObjValue() << endl; 
    env.out() << "Values x  = " << vals << endl; 

:

Solution status = Optimal 
Solution value = 0 
Values x  = [] 

은 아무도 알고 있나요

Minimize 
obj: 2 x_12 + x_13 + 2 x_21 + x_24 + x_31 + 3 x_34 + x_42 + 3 x_43 + x9 
Subject To 
c1: x_12 + x_13 - x_21 - x_31 + x_14 - x_41 = 1 
c2: - x_12 + x_21 + x_24 - x_42 + x_23 - x_32 = 0 
c3: - x_13 + x_31 + x_34 - x_43 - x_23 + x_32 = 0 
c4: - x_24 - x_34 + x_42 + x_43 - x_14 + x_41 = -1 
Bounds 
     x9 = 0 
End 

내가 그 해결책을 얻기 위해 다음 코드를 사용 내 프로그램에 무슨 문제가 있니? 감사

편집 :

IloEnv env; 
    IloModel model(env); 
    IloArray<IloNumVarArray> x(env); 
    IloRangeArray c(env); 
    IloInt nnodes = G.size(); 
    IloInt i, j; 
    IloEnv env = model.getEnv(); 

    //SHORTEST PATH PROBLEM 

    for (i = 0; i < nnodes; i++){ //x decision variables 
     x.add(IloNumVarArray(env, nnodes, 0, IloInfinity)); 
    } 
    for (i = 0; i < nnodes; i++){ 
     for (j = 0; j < nnodes; j++){ 
      stringstream sts; 
      sts << "x_" << i + 1 << j + 1; 
      x[i][j].setName(sts.str().c_str()); //SET NAMES 
     } 
    } 

    //set objective min sum_(all ij)[c_ij][x_ij] 
    IloExpr obj(env); 
    for (i = 0; i < nnodes; i++){ 
     for (j = 0; j < nnodes; j++){ 
      obj += G[i][j] * x[i][j]; 
     } 
    } 
    model.add(IloMinimize(env, obj)); 
    obj.end(); 

    //constraints sum_j[x_ij]-sum_j[x_ji] = 1 for s, -1 for t, or 0 
    for (i = 0; i < nnodes; i++){ 
     int ss = 0; 
     if (i == s) ss = 1; 
     if (i == t) ss = -1; 
     IloExpr sum1(env); 
     IloExpr sum2(env); 
     for (j = 0; j < nnodes; j++){ 
      sum1 += x[i][j]; 
      sum2 += x[j][i]; 
     } 
     c.add(sum1 - sum2 == ss); 
     sum1.end(); 
     sum2.end(); 
    } 
    model.add(c); 

    //solving--------------------------------------------------------- 
    IloCplex spp(model); 

    //write to file 
    spp.exportModel("model1.lp"); 
    spp.solve(); 

답변

0

코드에 뭔가 빠져있는 것 같습니다.

IloEnv env; 
IloModel model(env); 
IloArray<IloNumVarArray> x(env); 
IloRangeArray c(env); 

그러나 나중에 코드 당신이 솔루션은 다음과 같습니다 들어갈 때 사용하는 말 :

IloCplex spp(model); 
spp.setParam(IloCplex::RootAlg, IloCplex::AutoAlg); 
spp.solve(); 
IloArray<IloNumArray> vals(env); 
env.out() << "Solution status = " << spp.getStatus() << endl; 
env.out() << "Solution value = " << spp.getObjValue() << endl; 
env.out() << "Values x  = " << vals << endl; 
모델 생성 코드의 시작에서 당신은 IloNumVars X의 2 차원 배열의 선언이

val (IloNum의 2-D 배열)과 x (모델의 IloNumVars의 2-D 배열) 값을 연관시키는 작업을 어디에서 할 수 있는지 알 수 없습니다. 나는 당신이 IloNum의 배열에 값을 가져 오기 위해 IloNumVars에서 spp.getValue (...)와 같은 것을 호출해야한다고 생각한다.

1

분명히 당신이 파일에서 모델을 읽는되지 않은 : 내 모델은 프로그램 자체에 내장되어

가 여기에 첫 번째 부분입니다. 다음은 example입니다. 따라서 귀하의 경우 :

#include <ilcplex/ilocplex.h> 
    ILOSTLBEGIN 

    int main (int argc, char **argv) 
    { 
    IloEnv env; 
    try { 
     IloModel model(env); 
     IloCplex cplex(model); 

     IloObjective obj; 
     IloNumVarArray var(env); 
     IloRangeArray con(env); 

     cplex.importModel(model, "tmp.lp", obj, var, con); 
     cplex.extract(model); 

     // Optimize the problem and obtain solution. 
     if (!cplex.solve()) { 
      env.error() << "Failed to optimize LP" << endl; 
      throw(-1); 
     } 

     IloNumArray vals(env); 
     env.out() << "Solution status = " << cplex.getStatus() << endl; 
     env.out() << "Solution value = " << cplex.getObjValue() << endl; 
     cplex.getValues(vals, var); 
     env.out() << "Values  = " << vals << endl; 
     cplex.getSlacks(vals, con); 
     env.out() << "Slacks  = " << vals << endl; 
     cplex.getDuals(vals, con); 
     env.out() << "Duals   = " << vals << endl; 
     cplex.getReducedCosts(vals, var); 
     env.out() << "Reduced Costs = " << vals << endl; 
    } 
    catch (IloException& e) { 
     cerr << "Concert exception caught: " << e << endl; 
    } 
    catch (...) { 
     cerr << "Unknown exception caught" << endl; 
    } 

    env.end(); 

    return 0; 
    } // END main 

여기서 tmp.lp는 LP 모델 파일입니다. 이 코드를 실행하십시오.

Tried aggregator 1 time. 
LP Presolve eliminated 3 rows and 12 columns. 
Aggregator did 1 substitutions. 
All rows and columns eliminated. 
Presolve time = 0.00 sec. (0.01 ticks) 
Solution status = Optimal 
Solution value = 0 
Values  = [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0] 
Slacks  = [0, 0, 0, 0] 
Duals   = [1, 0, 0, 1] 
Reduced Costs = [1, 0, 3, 2, 2, 4, 0, 2, 1, 0, 0, 0, 0] 
+0

답장을 보내 주셔서 감사합니다. 원래 게시물을 수정했습니다. 문제의 모델은 cplex에 의해 생성됩니다. 텍스트 파일 출력은 "exportModel()"의 결과입니다. 그래서 제 경우에는 solve()에 오류가 있습니까? – Michael

0

먼저 인용 된 mp에서 뭔가를 말하게하십시오. 이 개 솔루션은 솔루션 CPLEX에 대해 완벽하게 유효한 있습니다 당신을 제공합니다

x_14 = 1, all other x_ijs = 0 or x_41 = -1 all other x_ijs = 0 

을 목표 값 0 결과.

x_14x_41을 변수로 사용하지만 목적에 따른 비용과 관련이없는 경우 기본적으로 소스에서 비용이 들지 않는 여행을 권장합니다. 문제는 모델과 관련이 없으며 정상적으로 작동합니다. 그래프 작성 방법입니다.