use of org.ojalgo.optimisation.Variable in project ojAlgo by optimatika.
the class MarketShareCase method testRelaxedButAllConstrainedToOptimal.
@Test
public void testRelaxedButAllConstrainedToOptimal() {
final ExpressionsBasedModel tmpModel = MarketShareCase.makeModel();
tmpModel.relax(true);
for (final Variable tmpVariable : tmpModel.getVariables()) {
tmpVariable.level(SOLUTION.get(tmpVariable.getName()));
}
final Result tmpResult = tmpModel.minimise();
TestUtils.assertEquals("OBJECTIVE_MIP", OBJECTIVE_MIP.doubleValue(), tmpResult.getValue(), 1E-14 / PrimitiveMath.THREE);
for (final Variable tmpVariable : tmpModel.getVariables()) {
TestUtils.assertEquals(tmpVariable.getName(), SOLUTION.get(tmpVariable.getName()).doubleValue(), tmpVariable.getValue().doubleValue(), 1E-14 / PrimitiveMath.THREE);
}
}
use of org.ojalgo.optimisation.Variable in project ojAlgo by optimatika.
the class MarketShareCase method testFullMIP.
@Test
@Disabled("Underscored before JUnit 5")
public void testFullMIP() {
final ExpressionsBasedModel tmpModel = MarketShareCase.makeModel();
// tmpModel.options.debug_stream = BasicLogger.DEBUG;
// tmpModel.options.debug_solver = IntegerSolver.class;
// tmpModel.options.validate = true;
final Result tmpResult = tmpModel.minimise();
TestUtils.assertEquals("OBJECTIVE_MIP", OBJECTIVE_MIP.doubleValue(), tmpResult.getValue(), 1E-14 / PrimitiveMath.THREE);
for (final Variable tmpVariable : tmpModel.getVariables()) {
TestUtils.assertEquals(tmpVariable.getName(), SOLUTION.get(tmpVariable.getName()).doubleValue(), tmpVariable.getValue().doubleValue(), 1E-14 / PrimitiveMath.THREE);
}
}
use of org.ojalgo.optimisation.Variable in project ojAlgo by optimatika.
the class MipLibCase method assertMinMaxVal.
protected static void assertMinMaxVal(final String modelName, final BigDecimal expMinVal, final BigDecimal expMaxVal, final boolean relax, final Map<String, BigDecimal> solution) {
if (DEBUG) {
BasicLogger.DEBUG.println();
BasicLogger.DEBUG.println();
BasicLogger.DEBUG.println(modelName);
BasicLogger.DEBUG.println();
}
final File tmpFile = new File(PATH + modelName);
final MathProgSysModel tmpMPS = MathProgSysModel.make(tmpFile);
final ExpressionsBasedModel tmpModel = tmpMPS.getExpressionsBasedModel();
if (relax) {
tmpModel.relax(true);
}
if (solution != null) {
for (final Variable tmpVariable : tmpModel.getVariables()) {
final BigDecimal tmpValue = solution.get(tmpVariable.getName());
if (tmpValue != null) {
tmpVariable.setValue(tmpValue);
} else {
tmpVariable.setValue(BigMath.ZERO);
}
}
if (!tmpModel.validate(PRECISION.newScale(4))) {
TestUtils.fail(SOLUTION_NOT_VALID);
}
}
tmpModel.options.mip_gap = 0.001;
tmpModel.options.time_suffice = 5L * CalendarDateUnit.MINUTE.size();
tmpModel.options.time_abort = 15L * CalendarDateUnit.MINUTE.size();
tmpModel.options.progress(IntegerSolver.class);
TestUtils.assertTrue(tmpModel.validate());
if (expMinVal != null) {
final double tmpMinimum = tmpModel.minimise().getValue();
if (!tmpModel.validate(PRECISION)) {
TestUtils.fail(SOLUTION_NOT_VALID);
}
final double tmpExpected = expMinVal.doubleValue();
final double tmpError = expMinVal.ulp().doubleValue();
TestUtils.assertEquals(tmpExpected, tmpMinimum, tmpError);
}
if (expMaxVal != null) {
final double tmpMaximum = tmpModel.maximise().getValue();
if (!tmpModel.validate(PRECISION)) {
TestUtils.fail(SOLUTION_NOT_VALID);
}
final double tmpExpected = expMaxVal.doubleValue();
final double tmpError = expMaxVal.ulp().doubleValue();
TestUtils.assertEquals(tmpExpected, tmpMaximum, tmpError);
}
}
use of org.ojalgo.optimisation.Variable in project ojAlgo by optimatika.
the class OptimisationIntegerData method buildModelForP20100412.
public static ExpressionsBasedModel buildModelForP20100412() {
final KnapsackItem[] tmpItems = { new KnapsackItem(20, 2), new KnapsackItem(30, 4) };
final Variable[] tmpVariables = new Variable[tmpItems.length];
for (int i = 0; i < tmpVariables.length; i++) {
tmpVariables[i] = new Variable("Var" + String.valueOf(i));
tmpVariables[i].lower(ZERO).upper(ONE).weight(tmpItems[i].value).integer(true);
}
final ExpressionsBasedModel retVal = new ExpressionsBasedModel(tmpVariables);
final Expression tmpTotalWeightExpr = retVal.addExpression("Total Weight");
for (int i = 0; i < tmpItems.length; i++) {
tmpTotalWeightExpr.set(i, tmpItems[i].weight);
}
tmpTotalWeightExpr.lower(ZERO).upper(THREE);
retVal.setMaximisation();
return retVal;
}
use of org.ojalgo.optimisation.Variable 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;
}
Aggregations