use of org.ojalgo.optimisation.Variable in project ojAlgo by optimatika.
the class ConvexProblems method testP20080118.
/**
* Ended up with a singular matrix (the equation system body generated by the LagrangeSolver) that
* resulted in a solution with NaN and Inf elements. This was not recognised and handled.
*/
@Test
public void testP20080118() {
// create expected returns matrix
final PrimitiveMatrix expectedReturnsMatrix = PrimitiveMatrix.FACTORY.rows(new double[][] { { 10.003264 }, { 9.989771 }, { 9.987513 }, { 9.988449 }, { 9.996579 }, { 9.990690 }, { 9.994904 }, { 9.994514 }, { 9.984064 }, { 9.987534 } });
// create covariance matrix
final PrimitiveMatrix covarianceMatrix = PrimitiveMatrix.FACTORY.rows(new double[][] { { 6.483565230120298E-4, -1.3344603795915894E-4, -4.610345510893708E-4, -7.334405624030001E-4, 1.1551383115707195E-5, -0.00104145662863434, -1.0725896685568462E-4, -1.221384153392056E-4, -4.173413644389791E-4, -2.4861043894946935E-4 }, { -1.3344603795915894E-4, 0.0026045957224784455, 0.0012394355327235707, 9.243919166568456E-4, -8.653805945112411E-5, 8.100239312410631E-4, 4.215960274481846E-4, 5.243272007211247E-4, 0.0013062718630332956, 1.4766450293395405E-4 }, { -4.610345510893708E-4, 0.0012394355327235707, 0.002361436913752224, 0.0020101714731002238, -1.4236763916609785E-5, 0.002120395905829043, 5.399158658928662E-4, 5.048790842067473E-4, 0.0014855261720730444, 4.841458106181396E-4 }, { -7.334405624030001E-4, 9.243919166568456E-4, 0.0020101714731002238, 0.0028542819089926895, -4.311102526746861E-6, 0.0028465650900869476, 6.242643883624462E-4, 4.086484048798765E-4, 0.001647437646316569, 7.58419663970477E-4 }, { 1.1551383115707195E-5, -8.653805945112411E-5, -1.4236763916609785E-5, -4.311102526746861E-6, 1.213366124417227E-4, -9.027529241741836E-5, 7.241389994693716E-6, -3.166855950737129E-5, -1.2445276374560802E-5, -5.3976919759028745E-5 }, { -0.00104145662863434, 8.100239312410631E-4, 0.002120395905829043, 0.0028465650900869476, -9.027529241741836E-5, 0.0064756879298965295, 2.8076277564885113E-4, 3.6082073553997553E-4, 0.001945238279500792, 0.0012421132342988626 }, { -1.0725896685568462E-4, 4.215960274481846E-4, 5.399158658928662E-4, 6.242643883624462E-4, 7.241389994693716E-6, 2.8076277564885113E-4, 0.0010121500024739688, 8.206099676659543E-5, 1.6129237403855146E-4, 7.550465994733837E-4 }, { -1.221384153392056E-4, 5.243272007211247E-4, 5.048790842067473E-4, 4.086484048798765E-4, -3.166855950737129E-5, 3.6082073553997553E-4, 8.206099676659543E-5, 4.504461842318998E-4, 4.7980942831718363E-4, -4.763223568683059E-5 }, { -4.173413644389791E-4, 0.0013062718630332956, 0.0014855261720730444, 0.001647437646316569, -1.2445276374560802E-5, 0.001945238279500792, 1.6129237403855146E-4, 4.7980942831718363E-4, 0.002228245076175045, 3.2083564921169634E-4 }, { -2.4861043894946935E-4, 1.4766450293395405E-4, 4.841458106181396E-4, 7.58419663970477E-4, -5.3976919759028745E-5, 0.0012421132342988626, 7.550465994733837E-4, -4.763223568683059E-5, 3.2083564921169634E-4, 0.0017093327832123186 } });
// create asset variables - cost and weighting constraints
final Variable[] tmpVariables = new Variable[(int) expectedReturnsMatrix.countRows()];
for (int i = 0; i < tmpVariables.length; i++) {
tmpVariables[i] = new Variable("VAR" + i);
final int row = i;
tmpVariables[i].weight(TypeUtils.toBigDecimal(expectedReturnsMatrix.get(row, 0)).negate());
// set the constraints on the asset weights
// require at least a 2% allocation to each asset
tmpVariables[i].lower(new BigDecimal("0.05"));
// require no more than 80% allocation to each asset
tmpVariables[i].upper(new BigDecimal("0.35"));
}
final RationalMatrix tmpExpected = RationalMatrix.FACTORY.rows(new double[][] { { 0.35 }, { 0.05 }, { 0.05 }, { 0.05 }, { 0.25 }, { 0.05 }, { 0.05 }, { 0.05 }, { 0.05 }, { 0.05 } });
ConvexProblems.doEarly2008(tmpVariables, covarianceMatrix, tmpExpected);
}
use of org.ojalgo.optimisation.Variable in project ojAlgo by optimatika.
the class ConvexProblems method builAndTestModel.
static void builAndTestModel(final PrimitiveDenseStore[] matrices, final PrimitiveDenseStore expectedSolution, final NumberContext modelValidationContext, final boolean testSolverDirectly) {
final MatrixStore<Double> tmpPartQ = expectedSolution.transpose().multiply(matrices[2].multiply(expectedSolution));
final MatrixStore<Double> tmpPartC = matrices[3].transpose().multiply(expectedSolution);
final double tmpExpectedValue = tmpPartQ.multiply(HALF.doubleValue()).subtract(tmpPartC).doubleValue(0);
final Optimisation.Result tmpExpectedResult = new Optimisation.Result(Optimisation.State.OPTIMAL, tmpExpectedValue, expectedSolution);
final ExpressionsBasedModel tmpModel = ConvexProblems.buildModel(matrices, expectedSolution);
OptimisationConvexTests.assertDirectAndIterativeEquals(tmpModel, modelValidationContext);
if (DEBUG) {
tmpModel.options.debug(ConvexSolver.class);
tmpModel.options.validate = false;
}
TestUtils.assertTrue("Expected solution not ok!", tmpModel.validate(tmpExpectedResult, modelValidationContext));
// The expected solution is written to the variables
TestUtils.assertTrue("Expected solution not ok!", tmpModel.validate(modelValidationContext));
// When/if the correct/optimal solution is used to kickStart ojAlgo should return that solution
final Result tmpInitialisedModelResult = tmpModel.minimise();
TestUtils.assertStateNotLessThanOptimal(tmpInitialisedModelResult);
TestUtils.assertEquals(tmpExpectedResult, tmpInitialisedModelResult, modelValidationContext);
TestUtils.assertEquals(tmpExpectedValue, tmpInitialisedModelResult.getValue(), modelValidationContext);
TestUtils.assertEquals(tmpExpectedValue, tmpModel.objective().evaluate(tmpInitialisedModelResult).doubleValue(), modelValidationContext);
TestUtils.assertEquals(tmpExpectedValue, tmpModel.objective().toFunction().invoke(expectedSolution).doubleValue(), modelValidationContext);
for (final Variable tmpVariable : tmpModel.getVariables()) {
tmpVariable.setValue(null);
}
// Initial variable values have been cleared
final Result tmpUninitialisedModelResult = tmpModel.minimise();
TestUtils.assertStateNotLessThanOptimal(tmpUninitialisedModelResult);
TestUtils.assertEquals(tmpExpectedResult, tmpUninitialisedModelResult, modelValidationContext);
TestUtils.assertEquals(tmpExpectedValue, tmpUninitialisedModelResult.getValue(), modelValidationContext);
TestUtils.assertEquals(tmpExpectedValue, tmpModel.objective().evaluate(tmpUninitialisedModelResult).doubleValue(), modelValidationContext);
TestUtils.assertEquals(tmpExpectedValue, tmpModel.objective().toFunction().invoke(expectedSolution).doubleValue(), modelValidationContext);
if (testSolverDirectly) {
final ConvexSolver.Builder tmpBuilder = new ConvexSolver.Builder(matrices);
final ConvexSolver tmpSolver = tmpBuilder.build();
// tmpSolver.options.debug(ConvexSolver.class);
// tmpSolver.options.validate = false;
final Optimisation.Result tmpResult = tmpSolver.solve();
TestUtils.assertStateNotLessThanOptimal(tmpResult);
TestUtils.assertEquals(tmpExpectedResult, tmpResult, NumberContext.getGeneral(2, 4));
TestUtils.assertEquals(tmpExpectedValue, tmpModel.objective().evaluate(tmpResult).doubleValue(), NumberContext.getGeneral(4, 8));
}
}
use of org.ojalgo.optimisation.Variable in project ojAlgo by optimatika.
the class ConvexProblems method testP20111129.
/**
* ojAlgo could not solve this, but LOQO (and others) could. I re-implemented the problem code just to
* verify there was no problem there. http://bugzilla.optimatika.se/show_bug.cgi?id=11
*/
@Test
public void testP20111129() {
final Variable x1 = new Variable("X1");
final Variable x2 = new Variable("X2").lower(BigMath.HUNDRED.negate()).upper(BigMath.HUNDRED);
final Variable x3 = new Variable("X3").lower(BigMath.ZERO);
final Variable x4 = new Variable("X4").lower(BigMath.ZERO);
final Variable[] tmpVariables = new Variable[] { x1, x2, x3, x4 };
final ExpressionsBasedModel tmpModel = new ExpressionsBasedModel(tmpVariables);
final Expression tmpObjExpr = tmpModel.addExpression("Objective");
tmpModel.setMinimisation();
tmpObjExpr.set(2, 2, BigMath.HALF);
tmpObjExpr.set(3, 3, BigMath.TWO);
tmpObjExpr.set(2, 3, BigMath.TWO.negate());
tmpObjExpr.set(0, BigMath.THREE);
tmpObjExpr.set(1, BigMath.TWO.negate());
tmpObjExpr.set(2, BigMath.ONE);
tmpObjExpr.set(3, BigMath.FOUR.negate());
tmpObjExpr.weight(BigMath.ONE);
Expression tmpConstrExpr;
tmpConstrExpr = tmpModel.addExpression("C1").lower(BigMath.FOUR);
tmpConstrExpr.set(0, BigMath.ONE);
tmpConstrExpr.set(1, BigMath.ONE);
tmpConstrExpr.set(2, BigMath.FOUR.negate());
tmpConstrExpr.set(3, BigMath.TWO);
tmpConstrExpr = tmpModel.addExpression("C2").upper(BigMath.SIX);
tmpConstrExpr.set(0, BigMath.THREE.negate());
tmpConstrExpr.set(1, BigMath.ONE);
tmpConstrExpr.set(2, BigMath.TWO.negate());
tmpConstrExpr = tmpModel.addExpression("C3").level(BigMath.NEG);
tmpConstrExpr.set(1, BigMath.ONE);
tmpConstrExpr.set(3, BigMath.NEG);
tmpConstrExpr = tmpModel.addExpression("C4").level(BigMath.ZERO);
tmpConstrExpr.set(0, BigMath.ONE);
tmpConstrExpr.set(1, BigMath.ONE);
tmpConstrExpr.set(2, BigMath.NEG);
// tmpModel.options.debug(ConvexSolver.class);
final Result tmpResult = tmpModel.minimise();
final double tmpObjFuncVal = tmpResult.getValue();
TestUtils.assertEquals(-5.281249989, tmpObjFuncVal, new NumberContext(7, 6));
final double[] tmpExpected = new double[] { -1.1875, 1.5625, 0.375, 2.5625 };
for (int i = 0; i < tmpExpected.length; i++) {
TestUtils.assertEquals(tmpExpected[i], tmpVariables[i].getValue().doubleValue(), new NumberContext(5, 4));
}
OptimisationConvexTests.assertDirectAndIterativeEquals(tmpModel, null);
}
use of org.ojalgo.optimisation.Variable in project ojAlgo by optimatika.
the class P20150720 method buildModel3.
/**
* Sent in 2015-09-03
*/
public static ExpressionsBasedModel buildModel3() {
/*
* 5 Products with associated Qty.
*/
final Map<String, Integer> constraintsProduct = new HashMap<>();
constraintsProduct.put("PRODUCT_1", 296_000);
constraintsProduct.put("PRODUCT_2", 888_000);
constraintsProduct.put("PRODUCT_3", 636_000);
constraintsProduct.put("PRODUCT_4", 220_000);
constraintsProduct.put("PRODUCT_5", 0);
final double stockTotal = constraintsProduct.values().stream().mapToDouble(e -> e.doubleValue()).sum();
// BasicLogger.debug("STOCK_TOTAL " + stockTotal);
/*
* Demand of each Customers
*/
final Map<String, Integer> constraintsCustomer = new LinkedHashMap<>();
constraintsCustomer.put("CUSTOMER_A_1", 72_000);
constraintsCustomer.put("CUSTOMER_A_2", 44_000);
constraintsCustomer.put("CUSTOMER_A_3", 12_000);
constraintsCustomer.put("CUSTOMER_A_4", 36_000);
constraintsCustomer.put("CUSTOMER_A_5", 0);
constraintsCustomer.put("CUSTOMER_A_6", 36_000);
constraintsCustomer.put("CUSTOMER_A_7", 12_000);
constraintsCustomer.put("CUSTOMER_A_8", 0);
constraintsCustomer.put("CUSTOMER_A_9", 0);
constraintsCustomer.put("CUSTOMER_A_10", 12_000);
constraintsCustomer.put("CUSTOMER_A_11", 12_000);
constraintsCustomer.put("CUSTOMER_A_12", 0);
constraintsCustomer.put("CUSTOMER_A_13", 0);
constraintsCustomer.put("CUSTOMER_A_14", 0);
constraintsCustomer.put("CUSTOMER_B_1", 24_000);
constraintsCustomer.put("CUSTOMER_B_2", 12_000);
constraintsCustomer.put("CUSTOMER_B_3", 300);
constraintsCustomer.put("CUSTOMER_C_1", 24_000);
constraintsCustomer.put("CUSTOMER_D_1", 20_000);
constraintsCustomer.put("CUSTOMER_E_1", 12_000);
constraintsCustomer.put("CUSTOMER_F_1", 12_000);
constraintsCustomer.put("CUSTOMER_G_1", 72_000);
constraintsCustomer.put("CUSTOMER_H_1", 28_000);
constraintsCustomer.put("CUSTOMER_I_1", 24_000);
constraintsCustomer.put("CUSTOMER_J_1", 16_000);
constraintsCustomer.put("CUSTOMER_K_1", 24_000);
constraintsCustomer.put("CUSTOMER_L_1", 56_000);
final double demandTotal = constraintsCustomer.values().stream().mapToDouble(e -> e.doubleValue()).sum();
// BasicLogger.debug("DEMAND_TOTAL " + demandTotal);
/*
* User constraint. In this case, user want to allocate 236_000 Qty on Customer_A_*
*/
final double userConstraintQty = 236_000;
final List<String> userConstraint = new ArrayList<>();
userConstraint.add("CUSTOMER_A_1|PRODUCT_2");
userConstraint.add("CUSTOMER_A_2|PRODUCT_2");
userConstraint.add("CUSTOMER_A_3|PRODUCT_2");
userConstraint.add("CUSTOMER_A_4|PRODUCT_2");
userConstraint.add("CUSTOMER_A_5|PRODUCT_2");
userConstraint.add("CUSTOMER_A_6|PRODUCT_2");
userConstraint.add("CUSTOMER_A_7|PRODUCT_2");
userConstraint.add("CUSTOMER_A_8|PRODUCT_2");
userConstraint.add("CUSTOMER_A_9|PRODUCT_2");
userConstraint.add("CUSTOMER_A_10|PRODUCT_2");
userConstraint.add("CUSTOMER_A_11|PRODUCT_2");
userConstraint.add("CUSTOMER_A_12|PRODUCT_2");
userConstraint.add("CUSTOMER_A_13|PRODUCT_2");
userConstraint.add("CUSTOMER_A_14|PRODUCT_2");
// List of Variable Names
final List<String> variablesName = new ArrayList<>();
variablesName.add("CUSTOMER_A_1|PRODUCT_2");
variablesName.add("CUSTOMER_A_2|PRODUCT_2");
variablesName.add("CUSTOMER_A_3|PRODUCT_2");
variablesName.add("CUSTOMER_A_4|PRODUCT_2");
variablesName.add("CUSTOMER_A_5|PRODUCT_2");
variablesName.add("CUSTOMER_A_6|PRODUCT_2");
variablesName.add("CUSTOMER_A_7|PRODUCT_2");
variablesName.add("CUSTOMER_A_8|PRODUCT_2");
variablesName.add("CUSTOMER_A_9|PRODUCT_2");
variablesName.add("CUSTOMER_A_10|PRODUCT_2");
variablesName.add("CUSTOMER_A_11|PRODUCT_2");
variablesName.add("CUSTOMER_A_12|PRODUCT_2");
variablesName.add("CUSTOMER_A_13|PRODUCT_2");
variablesName.add("CUSTOMER_A_14|PRODUCT_2");
variablesName.add("CUSTOMER_B_1|PRODUCT_2");
variablesName.add("CUSTOMER_B_1|PRODUCT_3");
variablesName.add("CUSTOMER_B_2|PRODUCT_2");
variablesName.add("CUSTOMER_B_2|PRODUCT_3");
variablesName.add("CUSTOMER_B_3|PRODUCT_2");
variablesName.add("CUSTOMER_B_3|PRODUCT_3");
variablesName.add("CUSTOMER_C_1|PRODUCT_2");
variablesName.add("CUSTOMER_C_1|PRODUCT_3");
variablesName.add("CUSTOMER_D_1|PRODUCT_4");
variablesName.add("CUSTOMER_D_1|PRODUCT_5");
variablesName.add("CUSTOMER_D_1|PRODUCT_2");
variablesName.add("CUSTOMER_D_1|PRODUCT_3");
variablesName.add("CUSTOMER_E_1|PRODUCT_1");
variablesName.add("CUSTOMER_F_1|PRODUCT_4");
variablesName.add("CUSTOMER_F_1|PRODUCT_5");
variablesName.add("CUSTOMER_F_1|PRODUCT_2");
variablesName.add("CUSTOMER_F_1|PRODUCT_3");
variablesName.add("CUSTOMER_G_1|PRODUCT_4");
variablesName.add("CUSTOMER_G_1|PRODUCT_5");
variablesName.add("CUSTOMER_G_1|PRODUCT_2");
variablesName.add("CUSTOMER_G_1|PRODUCT_3");
variablesName.add("CUSTOMER_H_1|PRODUCT_4");
variablesName.add("CUSTOMER_H_1|PRODUCT_5");
variablesName.add("CUSTOMER_H_1|PRODUCT_2");
variablesName.add("CUSTOMER_H_1|PRODUCT_3");
variablesName.add("CUSTOMER_I_1|PRODUCT_4");
variablesName.add("CUSTOMER_I_1|PRODUCT_5");
variablesName.add("CUSTOMER_I_1|PRODUCT_2");
variablesName.add("CUSTOMER_I_1|PRODUCT_3");
variablesName.add("CUSTOMER_J_1|PRODUCT_4");
variablesName.add("CUSTOMER_J_1|PRODUCT_5");
variablesName.add("CUSTOMER_J_1|PRODUCT_2");
variablesName.add("CUSTOMER_J_1|PRODUCT_3");
variablesName.add("CUSTOMER_K_1|PRODUCT_4");
variablesName.add("CUSTOMER_K_1|PRODUCT_5");
variablesName.add("CUSTOMER_K_1|PRODUCT_2");
variablesName.add("CUSTOMER_K_1|PRODUCT_3");
variablesName.add("CUSTOMER_L_1|PRODUCT_1");
/*
*
*/
final ExpressionsBasedModel model = new ExpressionsBasedModel();
final List<Variable> variables = new ArrayList<>();
/*
* Create All Variables:
*/
// BasicLogger.debug("---- Variable creation ------");
variablesName.forEach(name -> {
final Variable var = Variable.make(name).lower(0.0).weight(1.0);
model.addVariable(var);
variables.add(var);
// BasicLogger.debug(var);
});
// BasicLogger.debug("---- Constraints customers ------");
// Apply Customers constraints.
// CUSTOMER_A of Demand constraint because managed by user constraints.
constraintsCustomer.entrySet().stream().filter(e -> !e.getKey().startsWith("CUSTOMER_A")).forEach(entry -> {
final List<Variable> linked = variables.stream().filter(v -> v.getName().startsWith(entry.getKey())).collect(Collectors.toList());
final Expression constraint = model.addExpression("CONSTRAINTS_" + entry.getKey());
constraint.upper(entry.getValue().doubleValue());
constraint.setLinearFactorsSimple(linked);
// BasicLogger.debug(constraint);
});
// BasicLogger.debug("---- User Constraints Customers ------");
// Apply Product Type constraints.
final List<Variable> userLinked = variables.stream().filter(v -> userConstraint.contains(v.getName())).collect(Collectors.toList());
final Expression constraintUser = model.addExpression("CONSTRAINTS_USER_CUSTOMER_A");
constraintUser.level(userConstraintQty);
constraintUser.setLinearFactorsSimple(userLinked);
// BasicLogger.debug(constraintUser);
// BasicLogger.debug("---- Constraints Product ------");
// Apply Product Type constraints.
constraintsProduct.entrySet().forEach(entry -> {
final List<Variable> linked = variables.stream().filter(v -> v.getName().endsWith(entry.getKey())).collect(Collectors.toList());
final Expression constraint = model.addExpression("CONSTRAINTS_" + entry.getKey());
constraint.upper(entry.getValue().doubleValue());
constraint.setLinearFactorsSimple(linked);
// BasicLogger.debug(constraint);
});
/*
* Objective expression. - Maximize the Sum of all variables - Minimize the Sum of square error vs
* proportionality.
*/
// BasicLogger.debug("---- Objective ------");
final Expression objective = model.addExpression("OBJECTIVE").weight(-1.0);
// - Maximize the Sum of all variables
// objective.setLinearFactorsSimple(variables);
// BasicLogger.debug("---- Error formula ------");
/*
* Example: x1, x2, x3 linked variables for Customer X Error = ( (x1+x2+x3) / Target - 1 )^2 * ratio
*/
constraintsCustomer.entrySet().forEach(entry -> {
final List<Variable> linked = variables.stream().filter(v -> v.getName().startsWith(entry.getKey())).collect(Collectors.toList());
if (!linked.isEmpty() && (entry.getValue().doubleValue() > 0)) {
final double demand = entry.getValue();
final double ratio = demand / demandTotal;
final double target = stockTotal * ratio;
linked.forEach(v1 -> {
linked.forEach(v2 -> {
objective.set(v1, v2, ratio / (target * target));
});
objective.set(v1, (-2.0 * ratio) / target);
});
}
});
return model;
}
use of org.ojalgo.optimisation.Variable in project ojAlgo by optimatika.
the class P20150720 method buildModel1.
public static ExpressionsBasedModel buildModel1() {
// List of Variable Names
final List<String> variablesName = new ArrayList<>();
variablesName.add("CUSTOMER_1|PRODUCT_TYPE_1");
variablesName.add("CUSTOMER_2|PRODUCT_TYPE_1");
variablesName.add("CUSTOMER_3|PRODUCT_TYPE_1");
variablesName.add("CUSTOMER_3|PRODUCT_TYPE_2");
variablesName.add("CUSTOMER_3|PRODUCT_TYPE_3");
variablesName.add("CUSTOMER_3|PRODUCT_TYPE_4");
variablesName.add("CUSTOMER_3|PRODUCT_TYPE_5");
variablesName.add("CUSTOMER_4|PRODUCT_TYPE_1");
variablesName.add("CUSTOMER_4|PRODUCT_TYPE_2");
variablesName.add("CUSTOMER_5|PRODUCT_TYPE_2");
variablesName.add("CUSTOMER_5|PRODUCT_TYPE_3");
variablesName.add("CUSTOMER_5|PRODUCT_TYPE_4");
variablesName.add("CUSTOMER_6|PRODUCT_TYPE_2");
variablesName.add("CUSTOMER_6|PRODUCT_TYPE_3");
variablesName.add("CUSTOMER_6|PRODUCT_TYPE_4");
variablesName.add("CUSTOMER_6|PRODUCT_TYPE_5");
/*
* Constraints for each Customer = Demand of each Customer Sum of all variable linked to the customer
* <= Demand.
*/
final Map<String, Integer> constraintsCustomer = new LinkedHashMap<>();
constraintsCustomer.put("CUSTOMER_1", 40_000);
constraintsCustomer.put("CUSTOMER_2", 25_000);
constraintsCustomer.put("CUSTOMER_3", 15_000);
constraintsCustomer.put("CUSTOMER_4", 5_000);
constraintsCustomer.put("CUSTOMER_5", 2_000);
final double demandTotal = constraintsCustomer.values().stream().mapToDouble(e -> e.doubleValue()).sum();
/*
* Constraits for each product type = Stock per product type. Sum of all variable linked to this
* product type <=
*/
final Map<String, Integer> constraintsProduct = new HashMap<>();
constraintsProduct.put("PRODUCT_TYPE_1", 50_000);
constraintsProduct.put("PRODUCT_TYPE_2", 0);
constraintsProduct.put("PRODUCT_TYPE_3", 0);
constraintsProduct.put("PRODUCT_TYPE_4", 0);
constraintsProduct.put("PRODUCT_TYPE_5", 0);
final double stockTotal = constraintsProduct.values().stream().mapToDouble(e -> e.doubleValue()).sum();
/*
*
*/
final ExpressionsBasedModel model = new ExpressionsBasedModel();
final List<Variable> variables = new ArrayList<>();
// Create All Variables:
// BasicLogger.debug("---- Variable creation ------");
variablesName.forEach(name -> {
final Variable var = Variable.make(name).integer(true).lower(0.0);
model.addVariable(var);
variables.add(var);
// BasicLogger.debug(var);
});
// BasicLogger.debug("---- Constraints customers ------");
// Apply Customers constraints.
constraintsCustomer.entrySet().forEach(entry -> {
final List<Variable> linked = variables.stream().filter(v -> v.getName().startsWith(entry.getKey())).collect(Collectors.toList());
final Expression constraint = model.addExpression("CONSTRAINTS_" + entry.getKey());
constraint.lower(0.0).upper(entry.getValue().doubleValue());
constraint.setLinearFactorsSimple(linked);
linked.forEach(v -> v.upper(entry.getValue().doubleValue()));
// BasicLogger.debug(constraint);
});
// BasicLogger.debug("---- Constraints Product ------");
// Apply Product Type constraints.
constraintsProduct.entrySet().forEach(entry -> {
final List<Variable> linked = variables.stream().filter(v -> v.getName().endsWith(entry.getKey())).collect(Collectors.toList());
final Expression constraint = model.addExpression("CONSTRAINTS_" + entry.getKey());
constraint.lower(0.0).upper(entry.getValue().doubleValue());
constraint.setLinearFactorsSimple(linked);
// BasicLogger.debug(constraint);
});
/*
* Objective expression. - Maximize the Sum of all variables - Minimize the Sum of square error vs
* proportionality.
*/
// BasicLogger.debug("---- Objective ------");
final Expression objective = model.addExpression("OBJECTIVE").weight(1.0);
// - Maximize the Sum of all variables
objective.setLinearFactorsSimple(variables);
final List<Variable> errors = new ArrayList<>();
// BasicLogger.debug("---- Error formula ------");
constraintsCustomer.entrySet().forEach(entry -> {
final List<Variable> linked = variables.stream().filter(v -> v.getName().startsWith(entry.getKey())).collect(Collectors.toList());
if (!linked.isEmpty() && (entry.getValue().doubleValue() > 0)) {
final Variable error = Variable.make("ERROR:" + entry.getKey());
model.addVariable(error);
errors.add(error);
final Expression errorExp = model.addExpression("ERROR_EXP:" + entry.getKey()).level(stockTotal / demandTotal);
linked.forEach(v -> errorExp.set(v, 1.0 / entry.getValue().doubleValue()));
errorExp.set(error, -1.0);
// - Squared of error.
objective.set(error, error, -1.0);
}
});
return model;
}
Aggregations