linear_solver: Cleanup examples

This commit is contained in:
Mizux Seiha
2020-12-07 14:57:58 +01:00
parent 8522d2cc2b
commit 9ca0a26ac2
9 changed files with 190 additions and 106 deletions

View File

@@ -26,6 +26,8 @@ public class LinearProgrammingExample
// [START variables]
Variable x = solver.MakeNumVar(0.0, double.PositiveInfinity, "x");
Variable y = solver.MakeNumVar(0.0, double.PositiveInfinity, "y");
Console.WriteLine("Number of variables = " + solver.NumVariables());
// [END variables]
// [START constraints]
@@ -43,6 +45,8 @@ public class LinearProgrammingExample
Constraint c2 = solver.MakeConstraint(double.NegativeInfinity, 2.0);
c2.SetCoefficient(x, 1);
c2.SetCoefficient(y, -1);
Console.WriteLine("Number of constraints = " + solver.NumConstraints());
// [END constraints]
// [START objective]
@@ -54,19 +58,28 @@ public class LinearProgrammingExample
// [END objective]
// [START solve]
solver.Solve();
Solver.ResultStatus resultStatus = solver.Solve();
// [END solve]
// [START print_solution]
Console.WriteLine("Number of variables = " + solver.NumVariables());
Console.WriteLine("Number of constraints = " + solver.NumConstraints());
// The value of each variable in the solution.
// Check that the problem has an optimal solution.
if (resultStatus != Solver.ResultStatus.OPTIMAL)
{
Console.WriteLine("The problem does not have an optimal solution!");
return;
}
Console.WriteLine("Solution:");
Console.WriteLine("Objective value = " + solver.Objective().Value());
Console.WriteLine("x = " + x.SolutionValue());
Console.WriteLine("y = " + y.SolutionValue());
// The objective value of the solution.
Console.WriteLine("Optimal objective value = " + solver.Objective().Value());
// [END print_solution]
// [START advanced]
Console.WriteLine("\nAdvanced usage:");
Console.WriteLine("Problem solved in " + solver.WallTime() + " milliseconds");
Console.WriteLine("Problem solved in " + solver.Iterations() + " iterations");
Console.WriteLine("Problem solved in " + solver.Nodes() + " branch-and-bound nodes");
// [END advanced]
}
}
// [END program]

View File

@@ -64,22 +64,25 @@ public class LinearProgrammingExample {
// [START solve]
final MPSolver.ResultStatus resultStatus = solver.solve();
// Check that the problem has an optimal solution.
if (resultStatus != MPSolver.ResultStatus.OPTIMAL) {
System.err.println("The problem does not have an optimal solution!");
return;
}
// [END solve]
// [START print_solution]
// The value of each variable in the solution.
System.out.println("Solution");
System.out.println("x = " + x.solutionValue());
System.out.println("y = " + y.solutionValue());
// The objective value of the solution.
System.out.println("Optimal objective value = " + solver.objective().value());
if (resultStatus == MPSolver.ResultStatus.OPTIMAL) {
System.out.println("Solution:");
System.out.println("Objective value = " + objective.value());
System.out.println("x = " + x.solutionValue());
System.out.println("y = " + y.solutionValue());
} else {
System.err.println("The problem does not have an optimal solution!");
}
// [END print_solution]
// [START advanced]
System.out.println("\nAdvanced usage:");
System.out.println("Problem solved in " + solver.wallTime() + " milliseconds");
System.out.println("Problem solved in " + solver.iterations() + " iterations");
System.out.println("Problem solved in " + solver.nodes() + " branch-and-bound nodes");
// [END advanced]
}
}
// [END program]

View File

@@ -27,39 +27,50 @@ public class SimpleLpProgram
// [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");
Variable x = solver.MakeNumVar(0.0, double.PositiveInfinity, "x");
Variable y = solver.MakeNumVar(0.0, double.PositiveInfinity, "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);
// x + 7 * y <= 17.5.
solver.Add(x + 7 * y <= 17.5);
// x <= 3.5.
solver.Add(x <= 3.5);
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();
// Maximize x + 10 * y.
solver.Maximize(x + 10 * y);
// [END objective]
// [START solve]
solver.Solve();
Solver.ResultStatus resultStatus = solver.Solve();
// [END solve]
// [START print_solution]
// Check that the problem has an optimal solution.
if (resultStatus != Solver.ResultStatus.OPTIMAL)
{
Console.WriteLine("The problem does not have an optimal solution!");
return;
}
Console.WriteLine("Solution:");
Console.WriteLine("Objective value = " + solver.Objective().Value());
Console.WriteLine("x = " + x.SolutionValue());
Console.WriteLine("y = " + y.SolutionValue());
// [END print_solution]
// [START advanced]
Console.WriteLine("\nAdvanced usage:");
Console.WriteLine("Problem solved in " + solver.WallTime() + " milliseconds");
Console.WriteLine("Problem solved in " + solver.Iterations() + " iterations");
Console.WriteLine("Problem solved in " + solver.Nodes() + " branch-and-bound nodes");
// [END advanced]
}
}
// [END program]

View File

@@ -29,43 +29,64 @@ public class SimpleLpProgram {
// [START solver]
// Create the linear solver with the GLOP backend.
MPSolver solver = MPSolver.createSolver("GLOP");
if (solver == null) {
System.out.println("Could not create solver SCIP");
return;
}
// [END solver]
// [START variables]
double infinity = java.lang.Double.POSITIVE_INFINITY;
// Create the variables x and y.
MPVariable x = solver.makeNumVar(0.0, 1.0, "x");
MPVariable y = solver.makeNumVar(0.0, 2.0, "y");
MPVariable x = solver.makeNumVar(0.0, infinity, "x");
MPVariable y = solver.makeNumVar(0.0, infinity, "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);
// x + 7 * y <= 17.5.
MPConstraint c0 = solver.makeConstraint(-infinity, 17.5, "c0");
c0.setCoefficient(x, 1);
c0.setCoefficient(y, 7);
// x <= 3.5.
MPConstraint c1 = solver.makeConstraint(-infinity, 3.5, "c1");
c1.setCoefficient(x, 1);
c1.setCoefficient(y, 0);
System.out.println("Number of constraints = " + solver.numConstraints());
// [END constraints]
// [START objective]
// Create the objective function, 3 * x + y.
// Maximize x + 10 * y.
MPObjective objective = solver.objective();
objective.setCoefficient(x, 3);
objective.setCoefficient(y, 1);
objective.setCoefficient(x, 1);
objective.setCoefficient(y, 10);
objective.setMaximization();
// [END objective]
// [START solve]
solver.solve();
final MPSolver.ResultStatus resultStatus = 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());
if (resultStatus == MPSolver.ResultStatus.OPTIMAL) {
System.out.println("Solution:");
System.out.println("Objective value = " + objective.value());
System.out.println("x = " + x.solutionValue());
System.out.println("y = " + y.solutionValue());
} else {
System.err.println("The problem does not have an optimal solution!");
}
// [END print_solution]
// [START advanced]
System.out.println("\nAdvanced usage:");
System.out.println("Problem solved in " + solver.wallTime() + " milliseconds");
System.out.println("Problem solved in " + solver.iterations() + " iterations");
System.out.println("Problem solved in " + solver.nodes() + " branch-and-bound nodes");
// [END advanced]
}
}
// [END program]

View File

@@ -76,17 +76,17 @@ public class SimpleMipProgram {
System.out.println("Objective value = " + objective.value());
System.out.println("x = " + x.solutionValue());
System.out.println("y = " + y.solutionValue());
// [END print_solution]
// [START advanced]
System.out.println("\nAdvanced usage:");
System.out.println("Problem solved in " + solver.wallTime() + " milliseconds");
System.out.println("Problem solved in " + solver.iterations() + " iterations");
System.out.println("Problem solved in " + solver.nodes() + " branch-and-bound nodes");
// [END advanced]
} else {
System.err.println("The problem does not have an optimal solution!");
}
// [END print_solution]
// [START advanced]
System.out.println("\nAdvanced usage:");
System.out.println("Problem solved in " + solver.wallTime() + " milliseconds");
System.out.println("Problem solved in " + solver.iterations() + " iterations");
System.out.println("Problem solved in " + solver.nodes() + " branch-and-bound nodes");
// [END advanced]
}
}
// [END program]

View File

@@ -28,6 +28,8 @@ def LinearProgrammingExample():
# [START variables]
x = solver.NumVar(0, solver.infinity(), 'x')
y = solver.NumVar(0, solver.infinity(), 'y')
print('Number of variables =', solver.NumVariables())
# [END variables]
# [START constraints]
@@ -45,6 +47,8 @@ def LinearProgrammingExample():
constraint2 = solver.Constraint(-solver.infinity(), 2)
constraint2.SetCoefficient(x, 1)
constraint2.SetCoefficient(y, -1)
print('Number of constraints =', solver.NumConstraints())
# [END constraints]
# [START objective]
@@ -57,20 +61,26 @@ def LinearProgrammingExample():
# Solve the system.
# [START solve]
solver.Solve()
status = solver.Solve()
# [END solve]
# [START print_solution]
opt_solution = 3 * x.solution_value() + 4 * y.solution_value()
print('Number of variables =', solver.NumVariables())
print('Number of constraints =', solver.NumConstraints())
# The value of each variable in the solution.
print('Solution:')
print('x = ', x.solution_value())
print('y = ', y.solution_value())
# The objective value of the solution.
print('Optimal objective value =', opt_solution)
if status == pywraplp.Solver.OPTIMAL:
print('Solution:')
print('Objective value =', solver.Objective().Value())
print('x =', x.solution_value())
print('y =', y.solution_value())
else:
print('The problem does not have an optimal solution.')
# [END print_solution]
# [START advanced]
print('\nAdvanced usage:')
print('Problem solved in %f milliseconds' % solver.wall_time())
print('Problem solved in %d iterations' % solver.iterations())
print('Problem solved in %d branch-and-bound nodes' % solver.nodes())
# [END advanced]
LinearProgrammingExample()
# [END program]

View File

@@ -21,36 +21,46 @@ namespace operations_research {
void SimpleLpProgram() {
// [START solver]
// Create the linear solver with the GLOP backend.
MPSolver solver("simple_lp_program", MPSolver::GLOP_LINEAR_PROGRAMMING);
MPSolver* solver = MPSolver.CreateSolver("GLOP");
// [END solver]
// [START variables]
const double infinity = solver->infinity();
// Create the variables x and y.
MPVariable* const x = solver.MakeNumVar(0.0, 1, "x");
MPVariable* const y = solver.MakeNumVar(0.0, 2, "y");
MPVariable* const x = solver->MakeNumVar(0.0, infinity, "x");
MPVariable* const y = solver->MakeNumVar(0.0, infinity, "y");
LOG(INFO) << "Number of variables = " << solver.NumVariables();
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);
// x + 7 * y <= 17.5.
MPConstraint* const c0 = solver->MakeRowConstraint(-infinity, 17.5, "c0");
c0->SetCoefficient(x, 1);
c0->SetCoefficient(y, 7);
LOG(INFO) << "Number of constraints = " << solver.NumConstraints();
// x <= 3.5.
MPConstraint* const c1 = solver->MakeRowConstraint(-infinity, 3.5, "c1");
c1->SetCoefficient(x, 1);
c1->SetCoefficient(y, 0);
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);
// Maximize x + 10 * y.
MPObjective* const objective = solver->MutableObjective();
objective->SetCoefficient(x, 1);
objective->SetCoefficient(y, 10);
objective->SetMaximization();
// [END objective]
// [START solve]
solver.Solve();
const MPSolver::ResultStatus result_status = solver->Solve();
// Check that the problem has an optimal solution.
if (result_status != MPSolver::OPTIMAL) {
LOG(FATAL) << "The problem does not have an optimal solution!";
}
// [END solve]
// [START print_solution]
@@ -59,6 +69,14 @@ void SimpleLpProgram() {
LOG(INFO) << "x = " << x->solution_value();
LOG(INFO) << "y = " << y->solution_value();
// [END print_solution]
// [START advanced]
LOG(INFO) << "\nAdvanced usage:";
LOG(INFO) << "Problem solved in " << solver->wall_time() << " milliseconds";
LOG(INFO) << "Problem solved in " << solver->iterations() << " iterations";
LOG(INFO) << "Problem solved in " << solver->nodes()
<< " branch-and-bound nodes";
// [END advanced]
}
} // namespace operations_research

View File

@@ -24,41 +24,50 @@ def main():
# [END solver]
# [START variables]
infinity = solver.infinity()
# Create the variables x and y.
x = solver.NumVar(0, 1, 'x')
y = solver.NumVar(0, 2, 'y')
x = solver.NumVar(0.0, infinity, 'x')
y = solver.NumVar(0.0, infinity, 'y')
print('Number of variables =', 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)
# x + 7 * y <= 17.5.
solver.Add(x + 7 * y <= 17.5)
# x <= 3.5.
solver.Add(x <= 3.5)
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()
# Maximize x + 10 * y.
solver.Maximize(x + 10 * y)
# [END objective]
# [START solve]
solver.Solve()
status = solver.Solve()
# [END solve]
# [START print_solution]
print('Solution:')
print('Objective value =', objective.Value())
print('x =', x.solution_value())
print('y =', y.solution_value())
if status == pywraplp.Solver.OPTIMAL:
print('Solution:')
print('Objective value =', solver.Objective().Value())
print('x =', x.solution_value())
print('y =', y.solution_value())
else:
print('The problem does not have an optimal solution.')
# [END print_solution]
# [START advanced]
print('\nAdvanced usage:')
print('Problem solved in %f milliseconds' % solver.wall_time())
print('Problem solved in %d iterations' % solver.iterations())
print('Problem solved in %d branch-and-bound nodes' % solver.nodes())
# [END advanced]
if __name__ == '__main__':
main()

View File

@@ -21,43 +21,42 @@ namespace operations_research {
void SimpleMipProgram() {
// [START solver]
// Create the mip solver with the SCIP backend.
MPSolver solver("simple_mip_program",
MPSolver::SCIP_MIXED_INTEGER_PROGRAMMING);
MPSolver* solver = MPSolver.CreateSolver("SCIP");
// [END solver]
// [START variables]
const double infinity = solver.infinity();
const double infinity = solver->infinity();
// x and y are integer non-negative variables.
MPVariable* const x = solver.MakeIntVar(0.0, infinity, "x");
MPVariable* const y = solver.MakeIntVar(0.0, infinity, "y");
MPVariable* const x = solver->MakeIntVar(0.0, infinity, "x");
MPVariable* const y = solver->MakeIntVar(0.0, infinity, "y");
LOG(INFO) << "Number of variables = " << solver.NumVariables();
LOG(INFO) << "Number of variables = " << solver->NumVariables();
// [END variables]
// [START constraints]
// x + 7 * y <= 17.5.
MPConstraint* const c0 = solver.MakeRowConstraint(-infinity, 17.5, "c0");
MPConstraint* const c0 = solver->MakeRowConstraint(-infinity, 17.5, "c0");
c0->SetCoefficient(x, 1);
c0->SetCoefficient(y, 7);
// x <= 3.5.
MPConstraint* const c1 = solver.MakeRowConstraint(-infinity, 3.5, "c1");
MPConstraint* const c1 = solver->MakeRowConstraint(-infinity, 3.5, "c1");
c1->SetCoefficient(x, 1);
c1->SetCoefficient(y, 0);
LOG(INFO) << "Number of constraints = " << solver.NumConstraints();
LOG(INFO) << "Number of constraints = " << solver->NumConstraints();
// [END constraints]
// [START objective]
// Maximize x + 10 * y.
MPObjective* const objective = solver.MutableObjective();
MPObjective* const objective = solver->MutableObjective();
objective->SetCoefficient(x, 1);
objective->SetCoefficient(y, 10);
objective->SetMaximization();
// [END objective]
// [START solve]
const MPSolver::ResultStatus result_status = solver.Solve();
const MPSolver::ResultStatus result_status = solver->Solve();
// Check that the problem has an optimal solution.
if (result_status != MPSolver::OPTIMAL) {
LOG(FATAL) << "The problem does not have an optimal solution!";
@@ -73,9 +72,9 @@ void SimpleMipProgram() {
// [START advanced]
LOG(INFO) << "\nAdvanced usage:";
LOG(INFO) << "Problem solved in " << solver.wall_time() << " milliseconds";
LOG(INFO) << "Problem solved in " << solver.iterations() << " iterations";
LOG(INFO) << "Problem solved in " << solver.nodes()
LOG(INFO) << "Problem solved in " << solver->wall_time() << " milliseconds";
LOG(INFO) << "Problem solved in " << solver->iterations() << " iterations";
LOG(INFO) << "Problem solved in " << solver->nodes()
<< " branch-and-bound nodes";
// [END advanced]
}