use of org.ojalgo.optimisation.Expression in project ojAlgo by optimatika.
the class P20130225 method makeModel.
static ExpressionsBasedModel makeModel() {
final double alpha = 0.1;
final TreeMap preCalculateCosts = P20130225.preCalculateCosts();
final TreeMap variablesStation = new TreeMap();
final TreeMap variablesUVStation = new TreeMap();
final ArrayList allVariables = new ArrayList();
for (int i = 0; i < preCalculateCosts.size(); i++) {
final double[] costs = (double[]) preCalculateCosts.get(i);
// Cost function = Min(sum(C_i_j*X_i_j + alpha*(sum(Ui + Vi))
final int availableDocks = costs.length;
final Variable u = new Variable("U_" + i).lower(BigDecimal.valueOf(0)).weight(BigDecimal.valueOf(alpha));
final Variable v = new Variable("V_" + i).lower(BigDecimal.valueOf(0)).weight(BigDecimal.valueOf(alpha));
allVariables.add(u);
allVariables.add(v);
final ArrayList uvVariables = new ArrayList();
uvVariables.add(u);
uvVariables.add(v);
variablesUVStation.put(i, uvVariables);
for (int j = 0; j < availableDocks; j++) {
final double cost = costs[j];
final Variable variable = new Variable("X_" + i + "_" + j).binary().weight(BigDecimal.valueOf(cost));
if (variablesStation.containsKey(i)) {
final ArrayList vars = (ArrayList) variablesStation.get(i);
vars.add(variable);
} else {
final ArrayList vars = new ArrayList();
vars.add(variable);
variablesStation.put(i, vars);
}
allVariables.add(variable);
}
}
final ExpressionsBasedModel tmpIntegerModel = new ExpressionsBasedModel(allVariables);
// Exp_total_bikes = sum(j*X_i_j) <= 91;
final Expression expresion1 = tmpIntegerModel.addExpression("Exp_total_bikes");
for (int i = 0; i < tmpIntegerModel.countVariables(); i++) {
final Variable v = tmpIntegerModel.getVariable(i);
final String name = v.getName();
if (name.startsWith("X_")) {
final String state = name.substring(name.lastIndexOf("_") + 1, name.length());
expresion1.set(v, BigDecimal.valueOf((Integer.parseInt(state))));
}
}
expresion1.upper(BigDecimal.valueOf(91));
for (int i = 0; i < preCalculateCosts.size(); i++) {
// Exp_i = sum(X_i_j) = 1
final ArrayList varsStation = (ArrayList) variablesStation.get(i);
final Expression expresion2 = tmpIntegerModel.addExpression("Exp_" + i);
expresion2.setLinearFactorsSimple(varsStation);
expresion2.level(BigDecimal.valueOf(1));
}
for (int i = 0; i < preCalculateCosts.size(); i++) {
// Exp_UV_i = Ui - Vi + sum(j*X_i_j) = 5
final Expression expresion3 = tmpIntegerModel.addExpression("Exp_UV_" + i);
final ArrayList varsStation = (ArrayList) variablesStation.get(i);
for (int j = 0; j < varsStation.size(); j++) {
final Variable v = (Variable) varsStation.get(j);
final String name = v.getName();
final int state = Integer.parseInt(name.substring(name.lastIndexOf("_") + 1, name.length()));
expresion3.set(v, state);
}
final ArrayList uvStation = (ArrayList) variablesUVStation.get(i);
final Variable u = (Variable) uvStation.get(0);
final Variable v = (Variable) uvStation.get(1);
expresion3.set(u, BigDecimal.ONE);
expresion3.set(v, BigDecimal.valueOf(-1));
expresion3.level(BigDecimal.valueOf(5));
}
return tmpIntegerModel;
}
use of org.ojalgo.optimisation.Expression in project ojAlgo by optimatika.
the class P20150127a method getModel.
public static ExpressionsBasedModel getModel() {
final ExpressionsBasedModel retVal = new ExpressionsBasedModel();
final Variable x = Variable.make("x").integer(true);
final Variable y = Variable.make("y").integer(true);
retVal.addVariable(x);
retVal.addVariable(y);
int counter = 0;
for (final int[] coeff : P20150127a.getCoefficients()) {
final Expression tmpExpression = retVal.addExpression("inequality_" + ++counter);
// We want coeff[0] * x + coeff[1] * y < 0. Since our
// solutions are integer, we can do "<= -1".
tmpExpression.upper(BigDecimal.ONE.negate());
tmpExpression.set(x, coeff[0]);
tmpExpression.set(y, coeff[1]);
}
return retVal;
}
use of org.ojalgo.optimisation.Expression in project ojAlgo by optimatika.
the class P20150127b method getModel.
public static ExpressionsBasedModel getModel(final boolean branch, final boolean old) {
final ExpressionsBasedModel retVal = new ExpressionsBasedModel();
final Variable x = Variable.make("x").integer(true);
final Variable y = Variable.make("y").integer(true);
retVal.addVariable(x);
retVal.addVariable(y);
final List<int[]> coefficients = P20150127b.getCoefficients();
int counter = 0;
for (final int[] coeff : coefficients) {
final Expression c = retVal.addExpression("inequality_" + ++counter);
// We want coeff[0] * x + coeff[1] * y < 0. Since our
// solutions are integer, we can do "<= -1".
c.upper(BigDecimal.ONE.negate());
c.set(x, coeff[0]);
c.set(y, coeff[1]);
}
// the Branch&Bounde node which produces a wrong result uses.
if (branch) {
x.lower(1).upper(20);
y.upper(-1);
if (old) {
// The integer solver used to set this lower limit in place of "no limit"
y.lower(Integer.MIN_VALUE);
retVal.relax(true);
}
}
return retVal;
}
use of org.ojalgo.optimisation.Expression in project ojAlgo by optimatika.
the class DesignCase method testFacilityLocation.
/**
* http://www.ohio.edu/people/melkonia/math3050/slides/IPextendedintro.ppt Slide 8
*/
@Test
public void testFacilityLocation() {
final ArrayList<Variable> tmpVariables = new ArrayList<>();
tmpVariables.add(Variable.makeBinary("Factory in LA").weight(9));
tmpVariables.add(Variable.makeBinary("Factory in SF").weight(5));
tmpVariables.add(Variable.makeBinary("Warehouse in LA").weight(6));
tmpVariables.add(Variable.makeBinary("Warehouse in SF").weight(4));
final ExpressionsBasedModel tmpModel = new ExpressionsBasedModel();
tmpModel.addVariables(tmpVariables);
final Expression tmpBudgetCost = tmpModel.addExpression("Budget").upper(10);
tmpBudgetCost.set(tmpVariables.get(0), 6);
tmpBudgetCost.set(tmpVariables.get(1), 3);
tmpBudgetCost.set(tmpVariables.get(2), 5);
tmpBudgetCost.set(tmpVariables.get(3), 2);
// tmpModel.options.debug(GenericSolver.class);
final Result tmpResult = tmpModel.maximise();
TestUtils.assertStateNotLessThanOptimal(tmpResult);
TestUtils.assertEquals(15.0, tmpResult.getValue());
TestUtils.assertEquals(0.0, tmpResult.doubleValue(0));
TestUtils.assertEquals(1.0, tmpResult.doubleValue(1));
TestUtils.assertEquals(1.0, tmpResult.doubleValue(2));
TestUtils.assertEquals(1.0, tmpResult.doubleValue(3));
}
use of org.ojalgo.optimisation.Expression in project ojAlgo by optimatika.
the class IntegerProblems method testP20130409a.
/**
* apete's implementation of the original problem description.
* <a href="http://bugzilla.optimatika.se/show_bug.cgi?id=178">BugZilla</a>
*/
@Test
public void testP20130409a() {
final Variable[] tmpVariables = new Variable[] { new Variable("x1").lower(BigMath.ZERO).weight(BigMath.ONE), new Variable("x2013").lower(BigMath.ZERO).integer(true), new Variable("x2014").lower(BigMath.ZERO).integer(true) };
final ExpressionsBasedModel tmpModel = new ExpressionsBasedModel(tmpVariables);
final Expression tmpExpr1 = tmpModel.addExpression("Expr1");
tmpExpr1.set(0, -1);
tmpExpr1.set(1, 5100);
tmpExpr1.set(2, -5000);
tmpExpr1.upper(BigMath.ZERO);
final Expression tmpExpr2 = tmpModel.addExpression("Expr2");
tmpExpr2.set(0, 1);
tmpExpr2.set(1, 5100);
tmpExpr2.set(2, -5000);
tmpExpr2.lower(BigMath.ZERO);
final Expression tmpExpr3 = tmpModel.addExpression("Expr3");
tmpExpr3.set(1, 5000);
tmpExpr3.set(2, 5000);
tmpExpr3.level(new BigDecimal(19105000));
final BigArray tmpExpSol = BigArray.wrap(new BigDecimal[] { BigDecimal.valueOf(4200.000000000075), BigDecimal.valueOf(1892), BigDecimal.valueOf(1929) });
TestUtils.assertTrue("Expected Solution Not Valid", tmpModel.validate(tmpExpSol));
// tmpModel.options.debug(GenericSolver.class);
// tmpModel.options.problem = NumberContext.getGeneral(12);
final Result tmpResult = tmpModel.minimise();
// BasicLogger.debug(tmpResult.toString());
TestUtils.assertEquals("Solution Not Correct", tmpExpSol, tmpResult, new NumberContext(8, 8));
TestUtils.assertTrue("Solver State Not Optimal", tmpResult.getState().isOptimal());
}
Aggregations