sat: Update SimpleSatProgram

This commit is contained in:
Corentin Le Molgat
2021-10-18 14:24:28 +02:00
parent ab24ec1a62
commit cdcea7c275
4 changed files with 31 additions and 5 deletions

View File

@@ -12,8 +12,10 @@
// limitations under the License.
// [START program]
// [START import]
using System;
using Google.OrTools.Sat;
// [END import]
public class SimpleSatProgram
{
@@ -44,12 +46,18 @@ public class SimpleSatProgram
CpSolverStatus status = solver.Solve(model);
// [END solve]
if (status == CpSolverStatus.Optimal)
// [START print_solution]
if (status == CpSolverStatus.Optimal || status == CpSolverStatus.Feasible)
{
Console.WriteLine("x = " + solver.Value(x));
Console.WriteLine("y = " + solver.Value(y));
Console.WriteLine("z = " + solver.Value(z));
}
else
{
Console.WriteLine("No solution found.");
}
// [END print_solution]
}
}
// [END program]

View File

@@ -12,6 +12,7 @@
// limitations under the License.
// [START program]
// [START import]
package com.google.ortools.sat.samples;
import com.google.ortools.Loader;
@@ -19,6 +20,7 @@ import com.google.ortools.sat.CpModel;
import com.google.ortools.sat.CpSolver;
import com.google.ortools.sat.CpSolverStatus;
import com.google.ortools.sat.IntVar;
// [END import]
/** Minimal CP-SAT example to showcase calling the solver. */
public class SimpleSatProgram {
@@ -49,11 +51,15 @@ public class SimpleSatProgram {
CpSolverStatus status = solver.solve(model);
// [END solve]
if (status == CpSolverStatus.OPTIMAL) {
// [START print_solution]
if (status == CpSolverStatus.OPTIMAL || status == CpSolverStatus.FEASIBLE) {
System.out.println("x = " + solver.value(x));
System.out.println("y = " + solver.value(y));
System.out.println("z = " + solver.value(z));
} else {
System.out.println("No solution found.");
}
// [END print_solution]
}
}
// [END program]

View File

@@ -12,7 +12,9 @@
// limitations under the License.
// [START program]
// [START import]
#include "ortools/sat/cp_model.h"
// [END import]
namespace operations_research {
namespace sat {
@@ -39,12 +41,16 @@ void SimpleSatProgram() {
LOG(INFO) << CpSolverResponseStats(response);
// [END solve]
if (response.status() == CpSolverStatus::OPTIMAL) {
// [START print_solution]
if (response.status() == CpSolverStatus::OPTIMAL || response.status() == CpSolverStatus::FEASIBLE) {
// Get the value of x in the solution.
LOG(INFO) << "x = " << SolutionIntegerValue(response, x);
LOG(INFO) << "y = " << SolutionIntegerValue(response, y);
LOG(INFO) << "z = " << SolutionIntegerValue(response, z);
} else {
LOG(INFO) << "No solution found.";
}
// [END print_solution]
}
} // namespace sat

View File

@@ -11,10 +11,12 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Simple solve."""
# [START program]
"""Simple solve."""
# [START import]
from ortools.sat.python import cp_model
# [END import]
def SimpleSatProgram():
@@ -43,10 +45,14 @@ def SimpleSatProgram():
status = solver.Solve(model)
# [END solve]
if status == cp_model.OPTIMAL:
# [START print_solution]
if status == cp_model.OPTIMAL || status == cp_model.FEASIBLE:
print('x = %i' % solver.Value(x))
print('y = %i' % solver.Value(y))
print('z = %i' % solver.Value(z))
else:
print('No solution found.')
# [END print_solution]
SimpleSatProgram()