use of org.ojalgo.optimisation.Variable 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.Variable 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.Variable 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.Variable 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());
}
use of org.ojalgo.optimisation.Variable in project ojAlgo by optimatika.
the class IntegerProblems method testP20130409b.
/**
* Test case sent in by the user / problem reporter
* <a href="http://bugzilla.optimatika.se/show_bug.cgi?id=178">BugZilla</a>
*/
@Test
public void testP20130409b() {
final Variable x1 = Variable.make("x1");
final Variable x2013 = Variable.make("x2013");
final Variable x2014 = Variable.make("x2014");
final Variable x2015 = Variable.make("x2015");
x2013.setInteger(true);
x2014.setInteger(true);
x2015.setInteger(true);
final ExpressionsBasedModel tmpModel = new ExpressionsBasedModel();
tmpModel.addVariable(x1);
tmpModel.addVariable(x2013);
tmpModel.addVariable(x2014);
tmpModel.addVariable(x2015);
final Expression obj = tmpModel.addExpression("obj");
obj.set(x1, 1);
obj.weight(BigDecimal.valueOf(1));
final Expression c1 = tmpModel.addExpression("c1");
c1.set(x1, 1);
c1.lower(BigDecimal.valueOf(0));
final Expression c2 = tmpModel.addExpression("c2");
c2.set(x2014, -5000);
c2.set(x2013, 5100);
c2.set(x1, -1);
c2.upper(BigDecimal.valueOf(0));
final Expression c3 = tmpModel.addExpression("c3");
c3.set(x2014, -5000);
c3.set(x2013, 5100);
c3.set(x1, 1);
c3.lower(BigDecimal.valueOf(0));
final Expression c4 = tmpModel.addExpression("c4");
c4.set(x2014, 150);
c4.set(x2013, 5100);
c4.set(x2015, -5000);
c4.set(x1, -1);
c4.upper(BigDecimal.valueOf(0));
final Expression c5 = tmpModel.addExpression("c5");
c5.set(x2014, 150);
c5.set(x2013, 5100);
c5.set(x2015, -5000);
c5.set(x1, 1);
c5.lower(BigDecimal.valueOf(0));
final Expression c6 = tmpModel.addExpression("c6");
c6.set(x2015, 5000);
c6.set(x2014, 5000);
c6.set(x2013, 5000);
c6.level(BigDecimal.valueOf(19105000));
final BigArray tmpExpSol = BigArray.wrap(new BigDecimal[] { BigDecimal.valueOf(4849.999999997941), BigDecimal.valueOf(1245), BigDecimal.valueOf(1269), BigDecimal.valueOf(1307) });
TestUtils.assertTrue("Expected Solution Not Valid", tmpModel.validate(tmpExpSol));
// tmpModel.options.debug(IntegerSolver.class);
// tmpModel.options.problem = NumberContext.getGeneral(8);
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