2021-04-01 21:00:53 +02:00
|
|
|
# Copyright 2010-2021 Google LLC
|
2018-07-16 18:40:14 -07:00
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
|
#
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
#
|
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
# 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.
|
2018-11-16 05:02:48 -08:00
|
|
|
"""Simple solve."""
|
2018-07-16 18:40:14 -07:00
|
|
|
|
2018-11-16 05:02:48 -08:00
|
|
|
# [START program]
|
2018-07-16 18:40:14 -07:00
|
|
|
from ortools.sat.python import cp_model
|
|
|
|
|
|
|
|
|
|
|
2018-11-15 14:32:20 -08:00
|
|
|
def SimpleSatProgram():
|
2018-11-16 05:02:48 -08:00
|
|
|
"""Minimal CP-SAT example to showcase calling the solver."""
|
|
|
|
|
# Creates the model.
|
|
|
|
|
# [START model]
|
2018-11-11 09:39:59 +01:00
|
|
|
model = cp_model.CpModel()
|
2018-11-16 05:02:48 -08:00
|
|
|
# [END model]
|
|
|
|
|
|
|
|
|
|
# Creates the variables.
|
|
|
|
|
# [START variables]
|
|
|
|
|
num_vals = 3
|
|
|
|
|
x = model.NewIntVar(0, num_vals - 1, 'x')
|
|
|
|
|
y = model.NewIntVar(0, num_vals - 1, 'y')
|
|
|
|
|
z = model.NewIntVar(0, num_vals - 1, 'z')
|
|
|
|
|
# [END variables]
|
|
|
|
|
|
|
|
|
|
# Creates the constraints.
|
|
|
|
|
# [START constraints]
|
|
|
|
|
model.Add(x != y)
|
|
|
|
|
# [END constraints]
|
|
|
|
|
|
|
|
|
|
# Creates a solver and solves the model.
|
|
|
|
|
# [START solve]
|
|
|
|
|
solver = cp_model.CpSolver()
|
|
|
|
|
status = solver.Solve(model)
|
|
|
|
|
# [END solve]
|
|
|
|
|
|
2020-09-10 10:39:29 +02:00
|
|
|
if status == cp_model.OPTIMAL:
|
2018-11-16 05:02:48 -08:00
|
|
|
print('x = %i' % solver.Value(x))
|
|
|
|
|
print('y = %i' % solver.Value(y))
|
|
|
|
|
print('z = %i' % solver.Value(z))
|
2018-07-16 18:40:14 -07:00
|
|
|
|
|
|
|
|
|
2018-11-15 14:32:20 -08:00
|
|
|
SimpleSatProgram()
|
2018-11-16 05:02:48 -08:00
|
|
|
# [END program]
|