use of org.ojalgo.optimisation.Variable in project ojAlgo by optimatika.
the class FinancePortfolioProblem method buildModel.
private static ExpressionsBasedModel buildModel(final BasicMatrix covariances, final BasicMatrix returns, final BigDecimal riskAversion) {
ProgrammingError.throwIfNotSquare(covariances);
ProgrammingError.throwIfNotEqualRowDimensions(covariances, returns);
final int numberOfVariables = (int) returns.countRows();
final Variable[] tmpVariables = new Variable[numberOfVariables];
for (int i = 0; i < numberOfVariables; i++) {
tmpVariables[i] = Variable.make("Asset_" + Integer.toString(i)).lower(ZERO).upper(ONE).weight(-returns.doubleValue(i));
}
final ExpressionsBasedModel retVal = new ExpressionsBasedModel(tmpVariables);
final Expression tmp100P = retVal.addExpression("Balance");
for (int i = 0; i < numberOfVariables; i++) {
tmp100P.set(i, ONE);
}
tmp100P.level(ONE);
final Expression tmpVar = retVal.addExpression("Variance");
for (int i = 0; i < numberOfVariables; i++) {
for (int j = 0; j < numberOfVariables; j++) {
tmpVar.set(i, j, covariances.doubleValue(i, j));
}
}
tmpVar.weight(BigFunction.DIVIDE.invoke(riskAversion, TWO));
return retVal;
}
use of org.ojalgo.optimisation.Variable in project ojAlgo by optimatika.
the class OJAlgoTest3 method main.
/**
* @param args the command line arguments Objective: allocate the maximum qty, and try to keep
* proportionality between customer.
*/
public static void main(final String[] args) {
/*
* 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);
});
}
});
// - Maximise
// model.options.validate = true;
final Result result = model.maximise();
BasicLogger.debug(result);
// GOOD OPTIMAL 560300.4702803734
// WRONG OPTIMAL 560300.4015031556
BasicLogger.debug("");
BasicLogger.debug("");
variables.forEach(v -> BasicLogger.debug(v.getName() + " = " + v.getValue().doubleValue()));
/*
* WRONG ALLOCATION CUSTOMER_A_1|PRODUCT_2 = 236000.0 CUSTOMER_A_2|PRODUCT_2 = 0.0
* CUSTOMER_A_3|PRODUCT_2 = 0.0 CUSTOMER_A_4|PRODUCT_2 = 0.0 CUSTOMER_A_5|PRODUCT_2 = 0.0
* CUSTOMER_A_6|PRODUCT_2 = 0.0 CUSTOMER_A_7|PRODUCT_2 = 0.0 CUSTOMER_A_8|PRODUCT_2 = 0.0
* CUSTOMER_A_9|PRODUCT_2 = 0.0 CUSTOMER_A_10|PRODUCT_2 = 0.0 CUSTOMER_A_11|PRODUCT_2 = 0.0
* CUSTOMER_A_12|PRODUCT_2 = 0.0 CUSTOMER_A_13|PRODUCT_2 = 0.0 CUSTOMER_A_14|PRODUCT_2 = 0.0
* CUSTOMER_B_1|PRODUCT_2 = 24000.0 CUSTOMER_B_1|PRODUCT_3 = 0.0 CUSTOMER_B_2|PRODUCT_2 = 12000.0
* CUSTOMER_B_2|PRODUCT_3 = 0.0 CUSTOMER_B_3|PRODUCT_2 = 0.0 CUSTOMER_B_3|PRODUCT_3 = 300.0
* CUSTOMER_C_1|PRODUCT_2 = 24000.0 CUSTOMER_C_1|PRODUCT_3 = 0.0 CUSTOMER_D_1|PRODUCT_4 = 20000.0
* CUSTOMER_D_1|PRODUCT_5 = 0.0 CUSTOMER_D_1|PRODUCT_2 = 0.0 CUSTOMER_D_1|PRODUCT_3 = 0.0
* CUSTOMER_E_1|PRODUCT_1 = 12000.0 CUSTOMER_F_1|PRODUCT_4 = 12000.0 CUSTOMER_F_1|PRODUCT_5 = 0.0
* CUSTOMER_F_1|PRODUCT_2 = 0.0 CUSTOMER_F_1|PRODUCT_3 = 0.0 CUSTOMER_G_1|PRODUCT_4 = 72000.0
* CUSTOMER_G_1|PRODUCT_5 = 0.0 CUSTOMER_G_1|PRODUCT_2 = 0.0 CUSTOMER_G_1|PRODUCT_3 = 0.0
* CUSTOMER_H_1|PRODUCT_4 = 28000.0 CUSTOMER_H_1|PRODUCT_5 = 0.0 CUSTOMER_H_1|PRODUCT_2 = 0.0
* CUSTOMER_H_1|PRODUCT_3 = 0.0 CUSTOMER_I_1|PRODUCT_4 = 24000.0 CUSTOMER_I_1|PRODUCT_5 = 0.0
* CUSTOMER_I_1|PRODUCT_2 = 0.0 CUSTOMER_I_1|PRODUCT_3 = 0.0 CUSTOMER_J_1|PRODUCT_4 = 16000.0
* CUSTOMER_J_1|PRODUCT_5 = 0.0 CUSTOMER_J_1|PRODUCT_2 = 0.0 CUSTOMER_J_1|PRODUCT_3 = 0.0
* CUSTOMER_K_1|PRODUCT_4 = 24000.0 CUSTOMER_K_1|PRODUCT_5 = 0.0 CUSTOMER_K_1|PRODUCT_2 = 0.0
* CUSTOMER_K_1|PRODUCT_3 = 0.0 CUSTOMER_L_1|PRODUCT_1 = 56000.0
*/
/*
* GOOD ALLOCATION CUSTOMER_A_1|PRODUCT_2 = 80150.9434796 CUSTOMER_A_2|PRODUCT_2 = 48981.1320493
* CUSTOMER_A_3|PRODUCT_2 = 13358.4905589 CUSTOMER_A_4|PRODUCT_2 = 40075.4716767
* CUSTOMER_A_5|PRODUCT_2 = 0.0 CUSTOMER_A_6|PRODUCT_2 = 40075.4716767 CUSTOMER_A_7|PRODUCT_2 =
* 13358.4905589 CUSTOMER_A_8|PRODUCT_2 = 0.0 CUSTOMER_A_9|PRODUCT_2 = 0.0 CUSTOMER_A_10|PRODUCT_2 =
* 0.0 CUSTOMER_A_11|PRODUCT_2 = 0.0 CUSTOMER_A_12|PRODUCT_2 = 0.0 CUSTOMER_A_13|PRODUCT_2 = 0.0
* CUSTOMER_A_14|PRODUCT_2 = 0.0 CUSTOMER_B_1|PRODUCT_2 = 24000.0 CUSTOMER_B_1|PRODUCT_3 = 0.0
* CUSTOMER_B_2|PRODUCT_2 = 12000.0 CUSTOMER_B_2|PRODUCT_3 = 0.0 CUSTOMER_B_3|PRODUCT_2 = 0.0
* CUSTOMER_B_3|PRODUCT_3 = 300.0 CUSTOMER_C_1|PRODUCT_2 = 24000.0 CUSTOMER_C_1|PRODUCT_3 = 0.0
* CUSTOMER_D_1|PRODUCT_4 = 20000.0 CUSTOMER_D_1|PRODUCT_5 = 0.0 CUSTOMER_D_1|PRODUCT_2 = 0.0
* CUSTOMER_D_1|PRODUCT_3 = 0.0 CUSTOMER_E_1|PRODUCT_1 = 12000.0 CUSTOMER_F_1|PRODUCT_4 = 12000.0
* CUSTOMER_F_1|PRODUCT_5 = 0.0 CUSTOMER_F_1|PRODUCT_2 = 0.0 CUSTOMER_F_1|PRODUCT_3 = 0.0
* CUSTOMER_G_1|PRODUCT_4 = 72000.0 CUSTOMER_G_1|PRODUCT_5 = 0.0 CUSTOMER_G_1|PRODUCT_2 = 0.0
* CUSTOMER_G_1|PRODUCT_3 = 0.0 CUSTOMER_H_1|PRODUCT_4 = 28000.0 CUSTOMER_H_1|PRODUCT_5 = 0.0
* CUSTOMER_H_1|PRODUCT_2 = 0.0 CUSTOMER_H_1|PRODUCT_3 = 0.0 CUSTOMER_I_1|PRODUCT_4 = 24000.0
* CUSTOMER_I_1|PRODUCT_5 = 0.0 CUSTOMER_I_1|PRODUCT_2 = 0.0 CUSTOMER_I_1|PRODUCT_3 = 0.0
* CUSTOMER_J_1|PRODUCT_4 = 16000.0 CUSTOMER_J_1|PRODUCT_5 = 0.0 CUSTOMER_J_1|PRODUCT_2 = 0.0
* CUSTOMER_J_1|PRODUCT_3 = 0.0 CUSTOMER_K_1|PRODUCT_4 = 24000.0 CUSTOMER_K_1|PRODUCT_5 = 0.0
* CUSTOMER_K_1|PRODUCT_2 = 0.0 CUSTOMER_K_1|PRODUCT_3 = 0.0 CUSTOMER_L_1|PRODUCT_1 = 56000.0
*/
}
use of org.ojalgo.optimisation.Variable in project ojAlgo by optimatika.
the class P20150720 method buildModel2.
public static ExpressionsBasedModel buildModel2() {
// 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).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.upper(entry.getValue().doubleValue());
constraint.setLinearFactorsSimple(linked);
// 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.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) -
* (SupplyTotal/DemandTotal) * DemandCustomerX )^2
*/
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)) {
linked.forEach(v1 -> {
linked.forEach(v2 -> {
objective.set(v1, v2, -1);
});
objective.set(v1, ((2 * stockTotal) / demandTotal) * entry.getValue().doubleValue());
});
}
});
return model;
}
use of org.ojalgo.optimisation.Variable in project ojAlgo by optimatika.
the class StrategyMixer method testStratCombQuadraticExpressionModel.
/**
* This is test case using a reimplementation of the algorithm in {@link PortfolioMixer}.
*/
@Test
public void testStratCombQuadraticExpressionModel() {
final BigDecimal[] tmpTarget = new BigDecimal[] { THIRD, THIRD, THIRD };
final BigDecimal[] tmpStrat1 = new BigDecimal[] { HALF, HALF, ZERO };
final BigDecimal[] tmpStrat2 = new BigDecimal[] { HALF, ZERO, HALF };
final BigDecimal[] tmpStrat3 = new BigDecimal[] { ZERO, HALF, HALF };
final BigDecimal[][] tmpStrats = new BigDecimal[][] { tmpStrat1, tmpStrat2, tmpStrat3 };
final Variable[] tmpVars = new Variable[] { new Variable("S1"), new Variable("S2"), new Variable("S3"), Variable.makeBinary("B1"), Variable.makeBinary("B2"), Variable.makeBinary("B3") };
for (int s = 0; s < 3; s++) {
BigDecimal tmpVal = ZERO;
for (int i = 0; i < 3; i++) {
tmpVal = tmpVal.add(tmpTarget[i].multiply(tmpStrats[s][i]));
}
tmpVal = tmpVal.multiply(TWO).negate();
tmpVars[s].weight(tmpVal);
tmpVars[s].lower(ZERO);
tmpVars[s].upper(ONE);
}
final ExpressionsBasedModel tmpModel = new ExpressionsBasedModel(tmpVars);
// tmpModel.options.debug(IntegerSolver.class);
// tmpModel.options.validate = false;
final Expression tmpQuadObj = tmpModel.addExpression("Quadratic Objective Part");
tmpQuadObj.weight(ONE);
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
BigDecimal tmpVal = ZERO;
for (int i = 0; i < 3; i++) {
tmpVal = tmpVal.add(tmpStrats[row][i].multiply(tmpStrats[col][i]));
}
tmpQuadObj.set(row, col, tmpVal);
tmpQuadObj.set(3 + row, 3 + col, tmpVal.multiply(THOUSANDTH));
}
final Expression tmpActive = tmpModel.addExpression(tmpVars[row].getName() + " Active");
tmpActive.set(3 + row, ONE);
tmpActive.set(row, NEG);
tmpActive.lower(ZERO);
if (OptimisationIntegerTests.DEBUG) {
BasicLogger.debug(tmpActive.toString());
}
}
final Expression tmpHundredPercent = tmpModel.addExpression("100%");
tmpHundredPercent.level(ONE);
tmpHundredPercent.set(0, ONE);
tmpHundredPercent.set(1, ONE);
tmpHundredPercent.set(2, ONE);
if (OptimisationIntegerTests.DEBUG) {
BasicLogger.debug(tmpHundredPercent.toString());
}
final Expression tmpStrategyCount = tmpModel.addExpression("Strategy Count");
tmpStrategyCount.upper(TWO);
tmpStrategyCount.set(3, ONE);
tmpStrategyCount.set(4, ONE);
tmpStrategyCount.set(5, ONE);
if (OptimisationIntegerTests.DEBUG) {
BasicLogger.debug(tmpStrategyCount.toString());
}
tmpModel.minimise();
if (OptimisationIntegerTests.DEBUG) {
BasicLogger.debug(tmpModel.toString());
BasicLogger.debug(Arrays.toString(tmpVars));
}
int tmpUseCount = 0;
double tmpTotalWeight = 0D;
final Variable[] tmpSolution = new Variable[] { tmpVars[0], tmpVars[1], tmpVars[2] };
for (final Variable tmpWeight : tmpSolution) {
if (tmpWeight.getValue().signum() != 0) {
tmpUseCount++;
tmpTotalWeight += tmpWeight.getValue().doubleValue();
}
}
TestUtils.assertEquals(TWO.intValue(), tmpUseCount);
TestUtils.assertEquals(PrimitiveMath.ONE, tmpTotalWeight, 1E-14 / PrimitiveMath.THREE);
}
use of org.ojalgo.optimisation.Variable in project ojAlgo by optimatika.
the class TestOjAlgo method testBug1.
public static void testBug1() {
final Variable[] objective = new Variable[] { new Variable("X").weight(ONE), new Variable("Y").weight(ZERO), new Variable("Z").weight(ZERO) };
objective[1].setInteger(true);
final ExpressionsBasedModel model = new ExpressionsBasedModel(objective);
// c1: X =0
final Expression c1 = model.addExpression("c1");
c1.level(ZERO);
c1.set(0, ONE);
// c2: -X +5Y =0
final Expression c2 = model.addExpression("c2");
c2.level(ZERO);
c2.set(0, new BigDecimal(-1));
c2.set(1, ONE);
// c3: X -Z =0
final Expression c3 = model.addExpression("c3");
c3.level(ZERO);
// bugs with this constraint
c3.set(0, ONE);
c3.set(2, new BigDecimal(-1));
// but not with this one ???
// c3.setLinearFactor(0, new BigDecimal(-1));
// c3.setLinearFactor(2, ONE);
final Optimisation.Result tmpResult = model.minimise();
BasicLogger.debug(tmpResult.toString());
}
Aggregations