Java Reference

Java Reference

CMakeTest.java
Go to the documentation of this file.
1 package com.google.ortools;
2 
3 import com.google.ortools.Loader;
4 import com.google.ortools.linearsolver.MPConstraint;
5 import com.google.ortools.linearsolver.MPObjective;
6 import com.google.ortools.linearsolver.MPSolver;
7 import com.google.ortools.linearsolver.MPVariable;
8 import org.junit.jupiter.api.Test;
9 
11 public class CMakeTest {
12  @Test
13  public void testLP() {
15  MPSolver solver =
17  MPVariable x = solver.makeNumVar(0.0, 1.0, "x");
18  MPVariable y = solver.makeNumVar(0.0, 2.0, "y");
19  System.out.println("Number of variables = " + solver.numVariables());
20  MPConstraint ct = solver.makeConstraint(0.0, 2.0, "ct");
21  ct.setCoefficient(x, 1);
22  ct.setCoefficient(y, 1);
23  System.out.println("Number of constraints = " + solver.numConstraints());
24  MPObjective objective = solver.objective();
25  objective.setCoefficient(x, 3);
26  objective.setCoefficient(y, 1);
27  objective.setMaximization();
28  solver.solve();
29  System.out.println("Solution:");
30  System.out.println("Objective value = " + objective.value());
31  System.out.println("x = " + x.solutionValue());
32  System.out.println("y = " + y.solutionValue());
33  }
34 }
Load native libraries needed for using ortools-java.
Definition: Loader.java:33
static void loadNativeLibraries()
Definition: Loader.java:102
The class for constraints of a Mathematical Programming (MP) model.
void setCoefficient(MPVariable var, double coeff)
Sets the coefficient of the variable on the constraint.
A class to express a linear objective.
void setMaximization()
Sets the optimization direction to maximize.
void setCoefficient(MPVariable var, double coeff)
Sets the coefficient of the variable in the objective.
double value()
Returns the objective value of the best solution found so far.
This mathematical programming (MP) solver class is the main class though which users build and solve...
Definition: MPSolver.java:17
int numVariables()
Returns the number of variables.
Definition: MPSolver.java:156
MPSolver.ResultStatus solve()
Solves the problem using the default parameter values.
Definition: MPSolver.java:306
MPVariable makeNumVar(double lb, double ub, String name)
Creates a continuous variable.
Definition: MPSolver.java:201
int numConstraints()
Returns the number of constraints.
Definition: MPSolver.java:225
MPObjective objective()
Returns the mutable objective object.
Definition: MPSolver.java:298
MPConstraint makeConstraint(double lb, double ub)
Creates a linear constraint with given bounds.
Definition: MPSolver.java:266
The class for variables of a Mathematical Programming (MP) model.
Definition: MPVariable.java:16
double solutionValue()
Returns the value of the variable in the current solution.
Definition: MPVariable.java:65
The type of problems (LP or MIP) that will be solved and the underlying solver (GLOP,...
Definition: MPSolver.java:611