2015-04-05 1 views
0

Используя java и плагин OptimJ, я пишу модель Cplex и тестирую ее, используя множество тестовых примеров. Но когда я его выполняю, некоторые случаи занимают много времени, и это не практично. Мне интересно, есть ли способ установить максимальное время в java для Cplex, чтобы вернуть решение, в котором оно может быть не оптимальным.Установка времени завершения в CPLEX с использованием Java

Ниже приведен мой код. Какие-либо предложения?

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

      } 


    } 

ответ

0

При использовании CPLEX в Java, вы можете определить предел времени следующим

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

Вы должны смотреть в код PeakDemand и установите этот параметр перед тем, как решить проблему.

+0

IloCplex не может быть разрешен для типа. Это то, что я получаю. Вы считаете, что я использую плагин OptimJ? С ним все по-другому –