use of org.ojalgo.optimisation.ExpressionsBasedModel in project ojAlgo by optimatika.
the class NetlibCase method testSc50b.
/**
* OK! 2010-04-19 lp_solve => -70.00000000
*/
@Test
public void testSc50b() {
final File tmpFile = new File(PATH + "sc50b.mps");
final MathProgSysModel tmpMPS = MathProgSysModel.make(tmpFile);
final ExpressionsBasedModel tmpModel = tmpMPS.getExpressionsBasedModel();
this.assertMinMaxVal(tmpModel, new BigDecimal("-7.0000000000E+01"), null);
}
use of org.ojalgo.optimisation.ExpressionsBasedModel in project ojAlgo by optimatika.
the class NetlibCase method testBlend.
/**
* OK! 2010-04-19 lp_solve => -30.81214985
*/
@Test
public void testBlend() {
final File tmpFile = new File(PATH + "blend.mps");
final MathProgSysModel tmpMPS = MathProgSysModel.make(tmpFile);
final ExpressionsBasedModel tmpModel = tmpMPS.getExpressionsBasedModel();
// tmpModel.options.problem = new NumberContext(32, 8, RoundingMode.HALF_EVEN);
// tmpModel.options.solution = new NumberContext(16, 10, RoundingMode.HALF_EVEN);
// tmpModel.options.debug(LinearSolver.class);
this.assertMinMaxVal(tmpModel, new BigDecimal("-3.0812149846E+01"), null);
}
use of org.ojalgo.optimisation.ExpressionsBasedModel in project ojAlgo by optimatika.
the class MarketShareCase method testRedundant.
private void testRedundant(final String constraint) {
final ExpressionsBasedModel tmpModel = MarketShareCase.makeModel();
final Expression tmpExpression = tmpModel.getExpression(constraint);
if (DEBUG) {
BasicLogger.debug("Fix count: {}", tmpExpression.getLinearKeySet().size());
}
for (final IntIndex tmpIndex : tmpExpression.getLinearKeySet()) {
final Variable tmpVariable = tmpModel.getVariable(tmpIndex.index);
final String tmpName = tmpVariable.getName();
tmpVariable.level(SOLUTION.get(tmpName));
}
final Result tmpResult = tmpModel.minimise();
final NumberContext tmpContext = new NumberContext(8, 13);
TestUtils.assertEquals("OBJECTIVE_MIP", OBJECTIVE_MIP.doubleValue(), tmpResult.getValue(), tmpContext);
for (final Variable tmpVariable : tmpModel.getVariables()) {
TestUtils.assertEquals(tmpVariable.getName(), SOLUTION.get(tmpVariable.getName()).doubleValue(), tmpVariable.getValue().doubleValue(), tmpContext);
}
}
use of org.ojalgo.optimisation.ExpressionsBasedModel in project ojAlgo by optimatika.
the class P20160701 method main.
public static void main(final String[] arg) {
final int n = 6;
final double[][] c = new double[n][n];
c[0][0] = 1.7976931348623157E308;
c[0][1] = 141.4213562373095;
c[0][2] = 223.60679774997897;
c[0][3] = 223.60679774997897;
c[0][4] = 141.4213562373095;
c[0][5] = 156.63604262201076;
c[1][0] = 141.4213562373095;
c[1][1] = 1.7976931348623157E308;
c[1][2] = 100.0;
c[1][3] = 223.60679774997897;
c[1][4] = 200.0;
c[1][5] = 219.25609608009617;
c[2][0] = 223.60679774997897;
c[2][1] = 100.0;
c[2][2] = 1.7976931348623157E308;
c[2][3] = 200.0;
c[2][4] = 223.60679774997897;
c[2][5] = 319.2543607976003;
c[3][0] = 223.60679774997897;
c[3][1] = 223.60679774997897;
c[3][2] = 200.0;
c[3][3] = 1.7976931348623157E308;
c[3][4] = 100.0;
c[3][5] = 377.5537017276938;
c[4][0] = 141.4213562373095;
c[4][1] = 200.0;
c[4][2] = 223.60679774997897;
c[4][3] = 100.0;
c[4][4] = 1.7976931348623157E308;
c[4][5] = 297.81988930943544;
c[5][0] = 156.63604262201076;
c[5][1] = 219.25609608009617;
c[5][2] = 319.2543607976003;
c[5][3] = 377.5537017276938;
c[5][4] = 297.81988930943544;
c[5][5] = 1.7976931348623157E308;
final ExpressionsBasedModel model = new ExpressionsBasedModel();
// DECISION VARIABLES
final Variable[][] x = new Variable[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
x[i][j] = Variable.make("x" + i + "_" + j).binary().weight(c[i][j]);
model.addVariable(x[i][j]);
}
}
final Variable[] u = new Variable[n];
for (int i = 1; i < n; i++) {
u[i] = new Variable("u" + i);
model.addVariable(u[i]);
}
// sum(j in cities : i!=j) x[i][j]==1;
for (int i = 0; i < n; i++) {
final Expression constraint_line = model.addExpression("constraint_line" + i).lower(1).upper(1);
for (int j = 0; j < n; j++) {
if (i != j) {
constraint_line.set(x[i][j], 1);
}
}
}
// sum(i in cities : i!=j) x[i][j]==1;
for (int j = 0; j < n; j++) {
final Expression constraint_column = model.addExpression("constraint_column" + j).lower(1).upper(1);
for (int i = 0; i < n; i++) {
if (i != j) {
constraint_column.set(x[i][j], 1);
}
}
}
// u[i]-u[j]+n*x[i][j] <= n-1;
for (int i = 1; i < n; i++) {
for (int j = 1; j < n; j++) {
if (i != j) {
final Expression constraint_subroute = model.addExpression("constraint_subroute" + i + "_" + j).upper(n - 1);
constraint_subroute.set(u[i], 1);
constraint_subroute.set(u[j], -1);
constraint_subroute.set(x[i][j], n);
}
}
}
final Optimisation.Result result = model.minimise();
if (OptimisationIntegerTests.DEBUG) {
System.out.print("u=\n\t ");
for (int i = 1; i < n; i++) {
System.out.print(u[i].getValue().intValue() + " ");
}
System.out.print("\nx=\n\t");
for (int i = 0; i < n; i++) {
System.out.print(i + " ");
}
System.out.println();
for (int i = 0; i < n; i++) {
System.out.print(i + "\t");
for (int j = 0; j < n; j++) {
System.out.print(x[i][j].getValue().intValue() + " ");
}
System.out.println();
}
System.out.println("\nResult = " + result);
}
TestUtils.assertStateNotLessThanOptimal(result);
TestUtils.assertEquals(917.3134949394164, result.getValue());
}
use of org.ojalgo.optimisation.ExpressionsBasedModel in project ojAlgo by optimatika.
the class SamplePerformanceIssueSolvingILP method solve.
private void solve(final long maxContainerSize, final List<Container> containers) {
final ExpressionsBasedModel expressionsBasedModel = new ExpressionsBasedModel();
expressionsBasedModel.options.time_suffice = 60 * 1000;
expressionsBasedModel.options.iterations_suffice = 64;
expressionsBasedModel.options.mip_gap = 0.01;
final double firstContainerSize = maxContainerSize * FIRST_CONTAINER_SIZE_PERC;
final Expression firstContainerExpression = expressionsBasedModel.addExpression("expression:first").lower(firstContainerSize * (1 - DEVIATION)).upper(firstContainerSize * (1 + DEVIATION));
final double secondContainerSize = maxContainerSize * SECOND_CONTAINER_SIZE_PERC;
final Expression secondContainerExpression = expressionsBasedModel.addExpression("expression:second").lower(secondContainerSize * (1 - DEVIATION)).upper(secondContainerSize * (1 + DEVIATION));
final double thridContainerSize = maxContainerSize * THIRD_CONTAINER_SIZE_PERC;
final Expression thirdContainerExpression = expressionsBasedModel.addExpression("expression:third").lower(thridContainerSize * (1 - DEVIATION)).upper(thridContainerSize * (1 + DEVIATION));
for (int cIndex = 0; cIndex < containers.size(); cIndex++) {
final Container container = containers.get(cIndex);
final Variable variable = Variable.make("v" + cIndex).weight(RANDOM.nextInt(MAX_OBJECTIVE_VALUE) * container.getSize()).binary();
expressionsBasedModel.addVariable(variable);
if (container.getType().equals("first")) {
firstContainerExpression.set(variable, container.getSize());
}
if (container.getType().equals("second")) {
secondContainerExpression.set(variable, container.getSize());
}
if (container.getType().equals("third")) {
thirdContainerExpression.set(variable, container.getSize());
}
}
// expressionsBasedModel.options.debug(IntegerSolver.class);
// expressionsBasedModel.relax(true);
final Optimisation.Result result = expressionsBasedModel.maximise();
System.out.println("RESULT: " + result);
expressionsBasedModel.validate();
expressionsBasedModel.validate(result);
final List<Container> resultContainer = new ArrayList<>();
for (int cIndex = 0; cIndex < containers.size(); cIndex++) {
final BigDecimal bigDecimal = result.get(cIndex);
if (bigDecimal.compareTo(BigDecimal.ZERO) > 0) {
resultContainer.add(containers.get(cIndex));
}
}
System.out.println("RESULT: " + resultContainer.size());
}
Aggregations