Cplex Java用于解决小型模型

时间:2014-12-25 14:00:48

标签: java mathematical-optimization solver cplex

我正在尝试用额外的约束(权重)解决类似于TSP的问题, 我已经用Java编写了我的代码,但编译时遇到问题: 这是我的代码:

  package example01;
  import ilog.concert.*;
  import ilog.cplex.*;
  import java.io.*;
  import java.util.Arrays;

  public class modele {
  public static void solveme1 (){

        int     n    = 5;           
        int     [] W = {1,2,3,4,5};         
        int      [][]   distance =  {
                 {  0,14,10,12,20 },
                 {  14,0,23,4,30 },
                 {  10,23,0,25,18 },
                 {  12,4,25,0,17 },
                 {  20,30,18,17,0 },
             };

        try {
            IloCplex cplex = new IloCplex();

            //variables
            IloNumVar[][] x = new IloNumVar[n][];
            for (int i=0; i<n ; i++){
                x[i] = cplex.boolVarArray(n);
            }

            IloNumVar [] time = cplex.numVarArray(n, 0, Double.MAX_VALUE);

            // objective
            IloLinearNumExpr obj = cplex.linearNumExpr();
            for (int i =0; i<n; i++) {                  
                        obj.addTerm(time[i], W[i]);

            }
            cplex.addMinimize(obj);             

            //constraints
            // Flux In
            for (int j=0; j<n; j++){
                IloLinearNumExpr expr = cplex.linearNumExpr();
                for(int i=0; i<n; i++){
                    if (i!=j){
                        expr.addTerm(1.0,x[i][j]);
                    }
                }
                cplex.addEq(expr, 1);               
            }

            // Flux Out
            for (int j=0; j<n; j++){
                IloLinearNumExpr expr = cplex.linearNumExpr();
                for(int i=0; i<n; i++){
                    if (i!=j){
                        expr.addTerm(1.0,x[j][i]);
                    }
                }
                cplex.addEq(expr, 1);                   
            }
            // Starting at time =0
            cplex.addEq(time[0],0);

            // To force the starting from the location 0
            for (int j=1; j<n; j++){
                IloLinearNumExpr expr = cplex.linearNumExpr();
                expr.addTerm(1.0,x[0][j]);
                cplex.addEq(expr, 1);
            }

            // Constraint to determine the time in "J" after visiting "I"
            for (int j=0; j<n;j++){
                for (int i=0;i<n;i++){
                        if (i!=j){
                        cplex.addGe(time[j],  cplex.sum(cplex.sum(time[i], cplex.prod(x[i][j], 
                        distance[i][j])),cplex.prod(cplex.sum(x[i][j],-1),Double.MAX_VALUE)));
                        }
                }
            }

    if (cplex.solve()) 
    {
    System.out.println("objective = "+cplex.getObjValue());
    }
    else {
    System.out.println("Not solved");

    }
    cplex.end();

 }
        catch (IloException e){
            e.printStackTrace();
        }
    }
 }

期刊日志如下:

&#34;专栏&#39; x2&#39;设置为无限下限。     预先计算时间= 0.00秒。 (0.01滴答)     根节点处理(在b&amp; c之前):     实时= 0.00秒。 (0.01滴答)     并行b&amp; c,8个线程:     实时= 0.00秒。 (0.00刻度)     同步时间(平均)= 0.00秒。     等待时间(平均值)= 0.00秒     总计(根+分支和切割)= 0.00秒。 (0.01滴答)     没解决&#34;

Thank you in advance for your answers ! */

1 个答案:

答案 0 :(得分:1)

您声明编译时遇到问题。在我看来,你在运行程序时遇到了问题,并且它告诉你模型是不可行的(具有无限下限的变量听起来不可行)。