use of gurobi.GRBQuadExpr in project chordatlas by twak.
the class GurobiSkelSolver method buildProblem.
public void buildProblem() throws GRBException {
model = new GRBModel(new GRBEnv(Tweed.SCRATCH + System.currentTimeMillis() + ".log"));
target = new GRBQuadExpr();
edgeInfo = new HashMap<>();
faceInfo = new HashMap<>();
buildColouringProblem();
buildIsEdge();
buildBadGeom(false);
if (globalProfs != null)
buildProfiles();
if (minis != null && !minis.isEmpty())
buildMini();
for (HalfEdge he : edges) {
target.addTerm(20 * he.length(), edgeInfo.get(he).edgeNoProfile);
target.addTerm(40 * he.length(), edgeInfo.get(he).profileNoEdge);
}
// target.addTerm( 50 * he.length(), edgeInfo.get(he).isEdge );
double isEdgeHeight = 0, isNotEdgeHeight = 0;
for (HalfEdge e : edges) {
double cost;
if (e.over != null) {
cost = e.length() * Math.abs(((SuperFace) e.face).height - ((SuperFace) e.over.face).height);
isEdgeHeight += cost;
isNotEdgeHeight += cost;
// target.addTerm ( cost, edgeInfo.get(e).isNotEdge );
target.addTerm(-4 * cost, edgeInfo.get(e).isEdge);
}
}
print("is not edge height penalty " + isNotEdgeHeight + " (" + (isNotEdgeHeight / faceInfo.size()) + ")");
print("is edge height penalty " + isEdgeHeight);
model.setObjective(target, GRB.MINIMIZE);
}
use of gurobi.GRBQuadExpr in project chordatlas by twak.
the class SliceSolver method sliceOptimize.
public static int[] sliceOptimize(int numSlices, double[][] data, double[][] alignment) {
try {
GRBEnv env = new GRBEnv("mip1.log");
GRBModel model = new GRBModel(env);
GRBVar[][] xij = new GRBVar[numSlices][numSlices];
for (int i = 0; i < numSlices; i++) for (int j = 0; j < numSlices; j++) xij[i][j] = model.addVar(0.0, 1.0, 1.0, GRB.BINARY, "x" + i + "_" + j);
// overlap term n^2 terms
for (int i = 0; i < numSlices; i++) {
GRBLinExpr expr = new GRBLinExpr();
for (int j = 0; j < numSlices; j++) expr.addTerm(1, xij[i][j]);
model.addConstr(expr, GRB.EQUAL, 1, "overlap_" + i);
}
// for ( int i = 0; i < numSlices-1; i++ ) {
// for ( int j = 0; j < numSlices; j++ ) {
// GRBLinExpr expr = new GRBLinExpr();
// expr.addTerm( 1, xij[ i ] [ j ] );
// expr.addTerm( -1, xij[ i+1 ] [ j ] );
// model.addConstr( expr, GRB.EQUAL, 0, "super_adjacency" + j );
// }
// }
// data fitting term n^2 terms
GRBQuadExpr target = new GRBQuadExpr();
for (int i = 0; i < numSlices; i++) {
for (int j = 0; j < numSlices; j++) {
if (data[i][j] != Double.MAX_VALUE)
target.addTerm(data[i][j] * 30, xij[i][j]);
else
target.addTerm(1e3, xij[i][j]);
}
}
// neighbour alignment n^3 quadratic terms
for (int i = 0; i < numSlices - 1; i++) for (int ja = 0; ja < numSlices; ja++) for (int jb = 0; jb < numSlices; jb++) target.addTerm(alignment[ja][jb] == Double.MAX_VALUE ? 1e2 : alignment[ja][jb], xij[i][ja], xij[i + 1][jb]);
model.setObjective(target, GRB.MINIMIZE);
model.getEnv().set(GRB.DoubleParam.TimeLimit, 10.0);
model.optimize();
// xij[0][0].set( DoubleAttr.X, 1 );
// target.getValue();
System.out.println("Obj: " + model.get(GRB.DoubleAttr.ObjVal));
int[] out = new int[numSlices];
for (int i = 0; i < numSlices; i++) {
System.out.print("i: " + i + " ");
for (int j = 0; j < numSlices; j++) {
System.out.print(xij[i][j].get(GRB.DoubleAttr.X) + " ");
if (xij[i][j].get(GRB.DoubleAttr.X) == 1)
out[i] = j;
}
System.out.println();
}
// Dispose of model and environment
model.dispose();
env.dispose();
return out;
} catch (GRBException e) {
System.out.println("Error code: " + e.getErrorCode() + ". " + e.getMessage());
e.printStackTrace();
}
return null;
}
Aggregations