2016-07-09 18 views
0

Probleme mit meinem C++ Concert Cplex. Ich versuche, das Kürzeste-Pfad-Problem neu zu erstellen. Die Ausgabe in einer Textdatei ist:Cplex-Modell: keine Lösung

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 

ich dann den folgenden Code verwendet, um die Lösung zu erhalten:

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; 

jedoch die Ausgabe ich immer halten ist:

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

Weiß jemand, Was ist falsch an meinem Programm? Dank

EDIT:

Mein Modell in das Programm eingebaut selbst, hier ist der erste Teil:

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(); 

Antwort

0

Da scheint etwas in Ihrem Code fehlt. Zu Beginn des Modellerstellung Code haben Sie eine Erklärung eines 2-D-Array von IloNumVars x:

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

Aber später sagen Sie, dass der Code, den Sie die Lösung wie folgt aussieht bekommen verwenden:

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; 

Ich kann nicht sehen, wo Sie nichts tun, um Vals (ein 2-D-Array von IloNums) mit den Werten in x (dem 2-D-Array von IloNumVars in Ihrem Modell) zu assoziieren. Ich denke, dass Sie etwas wie spp.getValue (...) auf den IloNumVars aufrufen sollten, um die Werte in das Array von IloNums zu bekommen.

1

Anscheinend sind Sie nicht das Modell aus der Datei zu lesen. Hier ist ein example. Also in Ihrem Fall:

#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 

wo tmp.lp Sie Datei LP-Modell ist. Ausführen dieses Codes Ich habe

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

Danke für die Antwort, ich habe eine Bearbeitung zu meinem ursprünglichen Beitrag gemacht. Das betreffende Modell wird von cplex erstellt. Die Textdateiausgabe ist nur ein Ergebnis von "exportModel()". Also, gibt es in meinem Fall einen Fehler in solve()? – Michael

0

Zuerst lassen Sie mich etwas in Ihrem zitierten mp bemerken. Es gibt zwei Lösungen, die perfekt in Bezug auf die Lösung cplex gültig liefert Sie:

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

in objektivem Wert, den ich 0.

Mit x_14 und x_41 als Variablen, aber nicht mit irgendwelchen Kosten in der Zielsetzung verbunden, ermutigen Sie grundsätzlich eine Reise von der Quelle zu sinken ohne Kosten. Das Problem ist nicht mit Ihrem Modell, es funktioniert gut. Es ist die Art, wie Sie Ihren Graphen konstruieren, wie auch immer.

Verwandte Themen