Add region tag to linear solver samples

This commit is contained in:
Corentin Le Molgat
2019-05-13 11:05:42 +02:00
parent ff28ad2f63
commit 583b8bc01e
5 changed files with 82 additions and 12 deletions

View File

@@ -20,29 +20,46 @@ public class SimpleLpProgram
{
static void Main()
{
// [START solver]
// Create the linear solver with the GLOP backend.
Solver solver = Solver.CreateSolver("SimpleLpProgram", "GLOP_LINEAR_PROGRAMMING");
// [END solver]
// [START variables]
// Create the variables x and y.
Variable x = solver.MakeNumVar(0.0, 1.0, "x");
Variable y = solver.MakeNumVar(0.0, 2.0, "y");
Console.WriteLine("Number of variables = " + solver.NumVariables());
// [END variables]
// [START constraints]
// Create a linear constraint, 0 <= x + y <= 2.
Constraint ct = solver.MakeConstraint(0.0, 2.0, "ct");
ct.SetCoefficient(x, 1);
ct.SetCoefficient(y, 1);
Console.WriteLine("Number of constraints = " + solver.NumConstraints());
// [END constraints]
// [START objective]
// Create the objective function, 3 * x + y.
Objective objective = solver.Objective();
objective.SetCoefficient(x, 3);
objective.SetCoefficient(y, 1);
objective.SetMaximization();
// [END objective]
// Call the solver and display the results.
// [START solve]
solver.Solve();
// [END solve]
// [START print_solution]
Console.WriteLine("Solution:");
Console.WriteLine("Objective value = " + solver.Objective().Value());
Console.WriteLine("x = " + x.SolutionValue());
Console.WriteLine("y = " + y.SolutionValue());
// [END print_solution]
}
}
// [END program]

View File

@@ -13,10 +13,12 @@
// Minimal example to call the GLOP solver.
// [START program]
// [START import]
import com.google.ortools.linearsolver.MPConstraint;
import com.google.ortools.linearsolver.MPObjective;
import com.google.ortools.linearsolver.MPSolver;
import com.google.ortools.linearsolver.MPVariable;
// [END import]
/** Minimal Linear Programming example to showcase calling the solver.*/
public class SimpleLpProgram {
@@ -25,30 +27,47 @@ public class SimpleLpProgram {
}
public static void main(String[] args) throws Exception {
// [START solver]
// Create the linear solver with the GLOP backend.
MPSolver solver =
new MPSolver("SimpleLpProgram", MPSolver.OptimizationProblemType.GLOP_LINEAR_PROGRAMMING);
MPSolver solver = new MPSolver(
"SimpleLpProgram", MPSolver.OptimizationProblemType.GLOP_LINEAR_PROGRAMMING);
// [END solver]
// [START variables]
// Create the variables x and y.
MPVariable x = solver.makeNumVar(0.0, 1.0, "x");
MPVariable y = solver.makeNumVar(0.0, 2.0, "y");
System.out.println("Number of variables = " + solver.numVariables());
// [END variables]
// [START constraints]
// Create a linear constraint, 0 <= x + y <= 2.
MPConstraint ct = solver.makeConstraint(0.0, 2.0, "ct");
ct.setCoefficient(x, 1);
ct.setCoefficient(y, 1);
System.out.println("Number of constraints = " + solver.numConstraints());
// [END constraints]
// [START objective]
// Create the objective function, 3 * x + y.
MPObjective objective = solver.objective();
objective.setCoefficient(x, 3);
objective.setCoefficient(y, 1);
objective.setMaximization();
// [END objective]
// Call the solver and display the results.
// [START solve]
solver.solve();
// [END solve]
// [START print_solution]
System.out.println("Solution:");
System.out.println("Objective value = " + objective.value());
System.out.println("x = " + x.solutionValue());
System.out.println("y = " + y.solutionValue());
// [END print_solution]
}
}
// [END program]

View File

@@ -13,34 +13,52 @@
// Minimal example to call the GLOP solver.
// [START program]
#include <iostream>
// [START import]
#include "ortools/linear_solver/linear_solver.h"
// [END import]
namespace operations_research {
void run() {
// [START solver]
// Create the linear solver with the GLOP backend.
MPSolver solver("simple_lp_program", MPSolver::GLOP_LINEAR_PROGRAMMING);
// [END solver]
// [START variables]
// Create the variables x and y.
MPVariable* const x = solver.MakeNumVar(0.0, 1, "x");
MPVariable* const y = solver.MakeNumVar(0.0, 2, "y");
LOG(INFO) << "Number of variables = " << solver.NumVariables();
// [END variables]
// [START constraints]
// Create a linear constraint, 0 <= x + y <= 2.
MPConstraint* const ct = solver.MakeRowConstraint(0.0, 2.0, "ct");
ct->SetCoefficient(x, 1);
ct->SetCoefficient(y, 1);
LOG(INFO) << "Number of constraints = " << solver.NumConstraints();
// [END constraints]
// [START objective]
// Create the objective function, 3 * x + y.
MPObjective* const objective = solver.MutableObjective();
objective->SetCoefficient(x, 3);
objective->SetCoefficient(y, 1);
objective->SetMaximization();
// [END objective]
// Call the solver and display the results.
// [START solve]
solver.Solve();
// [END solve]
// [START print_solution]
std::cout << "Solution:" << std::endl;
std::cout << "x = " << x->solution_value() << std::endl;
std::cout << "y = " << y->solution_value() << std::endl;
LOG(INFO) << "Objective value = " << objective->Value();
LOG(INFO) << "x = " << x->solution_value();
LOG(INFO) << "y = " << y->solution_value();
// [END print_solution]
}
} // namespace operations_research

View File

@@ -11,39 +11,55 @@
# See the License for the specific language governing permissions and
# limitations under the License.
"""Minimal example to call the GLOP solver."""
# [START program]
# [START import]
from __future__ import print_function
from ortools.linear_solver import pywraplp
# [END import]
def main():
# [START solver]
# Create the linear solver with the GLOP backend.
solver = pywraplp.Solver('simple_lp_program',
pywraplp.Solver.GLOP_LINEAR_PROGRAMMING)
# [END solver]
# [START variables]
# Create the variables x and y.
x = solver.NumVar(0, 1, 'x')
y = solver.NumVar(0, 2, 'y')
print(('Number of variables = %d' % solver.NumVariables()))
# [END variables]
# [START constraints]
# Create a linear constraint, 0 <= x + y <= 2.
ct = solver.Constraint(0, 2, 'ct')
ct.SetCoefficient(x, 1)
ct.SetCoefficient(y, 1)
print(('Number of constraints = ', solver.NumConstraints()))
# [END constraints]
# [START objective]
# Create the objective function, 3 * x + y.
objective = solver.Objective()
objective.SetCoefficient(x, 3)
objective.SetCoefficient(y, 1)
objective.SetMaximization()
# [END objective]
# Call the solver and display the results.
# [START solve]
solver.Solve()
# [END solve]
# [START print_solution]
print('Solution:')
print('Objective value = ', objective.Value())
print('x = ', x.solution_value())
print('y = ', y.solution_value())
# [END print_solution]
if __name__ == '__main__':

View File

@@ -66,9 +66,9 @@ void simple_mip_program() {
// [START print_solution]
LOG(INFO) << "Solution:";
LOG(INFO) << "Objective value = " << objective->Value();
LOG(INFO) << "x = " << x->solution_value();
LOG(INFO) << "y = " << y->solution_value();
LOG(INFO) << "Optimal objective value = " << objective->Value();
// [END print_solution]
// [START advanced]