Files
ortools-clone/examples/python/rabbit_pheasant.py

58 lines
1.7 KiB
Python
Raw Normal View History

2017-10-17 13:08:10 +02:00
# Copyright 2010-2017 Google
2010-09-15 12:42:33 +00: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
2010-09-15 12:42:33 +00:00
#
# 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.
"""Rabbit + Pheasant puzzle.
This example is the same one described in
util/constraint/constraint_solver/constraint_solver.h
rewritten using the SWIG generated python wrapper.
Its purpose it to demonstrate how a simple example can be written in all the
flavors of constraint programming interfaces.
"""
2016-01-15 00:18:32 +01:00
from __future__ import print_function
from ortools.constraint_solver import pywrapcp
from ortools.constraint_solver import solver_parameters_pb2
2010-09-15 12:42:33 +00:00
def main():
parameters = pywrapcp.Solver.DefaultSolverParameters()
parameters.trace_search = True
2010-09-15 12:42:33 +00:00
# Create the solver.
solver = pywrapcp.Solver('rabbit+pheasant', parameters)
2010-09-15 12:42:33 +00:00
# Create the variables.
2014-07-09 11:17:29 +00:00
pheasant = solver.IntVar(0, 100, 'pheasant')
rabbit = solver.IntVar(0, 100, 'rabbit')
2010-09-15 12:42:33 +00:00
# Create the constraints.
solver.Add(pheasant + rabbit == 20)
solver.Add(pheasant * 2 + rabbit * 4 == 56)
# Create the search phase.
2018-06-11 11:51:18 +02:00
db = solver.Phase([rabbit, pheasant], solver.INT_VAR_DEFAULT,
2010-09-15 12:42:33 +00:00
solver.INT_VALUE_DEFAULT)
# And solve.
solver.NewSearch(db)
solver.NextSolution()
2010-09-15 12:42:33 +00:00
# Display output.
2016-01-15 00:18:32 +01:00
print(pheasant)
print(rabbit)
solver.EndSearch()
2016-01-15 00:18:32 +01:00
print(solver)
2010-09-15 12:42:33 +00:00
2018-06-11 11:51:18 +02:00
2014-07-09 11:17:29 +00:00
if __name__ == '__main__':
2010-09-15 12:42:33 +00:00
main()