使用 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) ;
}
}