使用Java在CPLEX中设置终止时间

时间:2015-04-05 13:02:35

标签: java optimization cplex integer-programming

使用java和OptimJ插件,我正在编写Cplex模型并使用许多测试用例对其进行测试。但是当我执行它时,有些情况需要很长时间才能完成,这是不切实际的。我想知道是否有一种方法可以在java中为Cplex设置最大时间以返回解决方案,其中可能不是最佳解决方案。

以下是我的代码。有什么建议吗?

 // a main entry point to test our model
      public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException
      {
            File dataFile = new File("test_1.txt");
            Scanner dataReader = new Scanner (dataFile);

            int test= dataReader.nextInt();


    PrintWriter writer = new PrintWriter("result_1.txt", "UTF-8");      

    for(int tes=0; tes<test; tes++ )
    {

          int n= dataReader.nextInt();
          int T=dataReader.nextInt();

          double []d= new double[n];
          int [] m = new int[n];

          for(int i=0; i<n; i++)
          {
              d[i]= dataReader.nextDouble();
              m[i]= (int)dataReader.nextDouble();
          }


        // instantiate the model
       PeakDemand p= new PeakDemand(n,T,d,m);

        // solve it
       p.extract();

       System.out.print("test case #"+tes+": ");
       System.out.println( p.solve());

       double objectiveValue = p.ObjectiveValue();
       System.out.println("Value of Objective Function is: "+ objectiveValue);


       writer.println(""+objectiveValue);


    }writer.close();

      }

以下是我的PeakDemand类

public model PeakDemand extends PeakDemandParam solver cplex12
{


    public PeakDemand(int n,int T,double[] d, int [] m)
    {
        super(n,T,d,m);


    }

    public double ObjectiveValue()
    {
        /*
        for(int i=0; i<n; i++ )
        {
            System.out.print("[ ");
            for(int j=0; j<T; j++)
            {

                System.out.print(d[i]*value(I[i][j]));
                if(j+1==T)
                    System.out.println("]");
                else
                    System.out.print(" ");
            }
        }*/


        double maxx=0;
        for(int i=0; i<T; i++ )
        {
            double summ=0;
            for(int j=0; j<n; j++)
            {
                summ=summ + (d[j]*value(I[j][i]));
                //System.out.print("value:" +(d[j]*value(I[i][j])+", "));
            }//System.out.println();

            if(summ>maxx)
                maxx=summ;
        }

        return maxx;
    }

//decision variable 
 var int [][]I [n][T];


 minimize
 max {int t: 0 .. T-1}
    {sum {int i : 0 .. n-1}
          {d[i]*I[i][t]}};


  // neighbouring countries must have a different color
  constraints {

         forall ( int  i:0 .. n-1)
         {
         forall (int t:0 .. T-1)
          {
             I[i][t]>=0;
             I[i][t]<=1;
          }

         }

         forall ( int  i:0 .. n-1)
          {
             sum {int t:0 .. T-1}
                 {I[i][t]}== m[i];


          }


            forall (int i:0 .. n-1)
            {

               forall (int t: 1 .. T-m[i])
                 {

                   sum{int v:t .. t+m[i]-1}{ I[i][v]}!= m[i]=> !(I[i][t] - I[i][t-1]==1) ;   //RIGHT???? NOT SURE!

                 }

               (sum {int v:0 .. m[i]-1}{ I[i][v]}!=m[i]) => !(I[i][0]==1)  ;

            }  


  }

1 个答案:

答案 0 :(得分:0)

在java中使用cplex时,可以按如下方式定义时间限制

  IloCplex cplex = new IloCplex();
  cplex.setParam(IloCplex.DoubleParam.TiLim, varLimit);//varLimit is a double in sec

您需要查看PeakDemand的代码并在求解之前设置此参数。