Originally posted by: brown_rice
Hi Daniel,
I am trying to implement the framework that you suggested. As an example, I implemented the model provided here and modeled the scalable warehouse problem. I created 20 instanced and CPLEX took 408.413 second when solving each instance one by one. When I went into the parallel solving, it looks like the instances are still being solved one by one. I was wondering if you could help me to figure out what I might be doing wrong.
import java.util.Iterator;
import java.util.Vector;
import ilog.concert.IloException;
import ilog.concert.IloIntVar;
import ilog.concert.IloLinearNumExpr;
import ilog.concert.IloNumVar;
import ilog.cplex.IloCplex;
public class concurrent {
public static final double convert = 0.001;
public static int Fixed;
public static int NbWarehouses;
public static int NbStores;
public static void main(String[] args) throws IloException, InterruptedException {
// TODO Auto-generated method stub
long start = System.currentTimeMillis();
int nProblems = 20;
final Vector<Integer> v = new Vector<Integer>();
for (int i = 1; i <= nProblems; ++i) v.add(i);
final Iterator<Integer> it = v.iterator();
int NTHREADS = 4; //numofCores
Thread[] threads = new Thread[NTHREADS];
for (int t = 0; t < NTHREADS; ++t) {
threads[t] = new Thread(new Runnable() {
public void run() {
while (true) {
int i;
synchronized (it) {
if ( !it.hasNext() ) return;
i = it.next();
IloCplex sub = null;
try {
sub = new IloCplex();
} catch (IloException e) {
e.printStackTrace();
}
try {
Fixed = 400 + i;
NbWarehouses = 400 ;
NbStores = 800 + i;
int[] Capacity = new int[NbWarehouses];
int[][] SupplyCost= new int[NbStores][NbWarehouses];
for(int w=0; w< NbWarehouses ; w++) {
Capacity[w] = (NbStores/ NbWarehouses) + (w % ( NbStores/ NbWarehouses));
for(int s=0; s< NbStores ; s++) {
SupplyCost[s][w] = 1 + ( ( s + 10 * w) % 100 );
}
}
IloIntVar[] open = new IloIntVar[NbWarehouses];
IloNumVar[][] supply = new IloNumVar[NbStores][NbWarehouses];
IloLinearNumExpr expr = sub.linearNumExpr();
IloLinearNumExpr expr2 = sub.linearNumExpr();
for(int w=0; w< NbWarehouses ; w++) {
open[w] = sub.boolVar("open_"+w);
sub.add(open[w]);
expr.addTerm(Fixed, open[w]);
for(int s=0; s< NbStores ; s++) {
supply[s][w] = sub.numVar(0, 1, "supply"+s + "_" + w);
sub.add(supply[s][w]);
expr.addTerm(SupplyCost[s][w], supply[s][w]);
expr2.addTerm(1, supply[s][w]);
}
sub.addLe(expr2, sub.prod(open[w], Capacity[w]) , "ctOpen_"+ w );
expr2.clear();
}
sub.addMinimize(expr);
expr.clear();
for(int s=0; s< NbStores ; s++) {
for(int w=0; w< NbWarehouses ; w++) {
expr.addTerm(1, supply[s][w]);
}
sub.addEq(1, expr, "ctStoreHasOneWarehouse_"+s);
expr.clear();
}
System.out.println(i + "th problem is being solved.");
sub.setOut(null);
sub.solve();
System.out.println(i + "th problem is " + sub.getStatus());
} catch (IloException e) {
e.printStackTrace();
}
finally { sub.end(); }
}
}
}
});
}
for (int t = 0; t < NTHREADS; ++t) { threads[t].start(); }
for (int t = 0; t < NTHREADS; ++t) { threads[t].join(); }
System.out.println("Total solution time is " + (System.currentTimeMillis() - start) * convert );
}
}
#CPLEXOptimizers#DecisionOptimization