# Copyright 2011 Hakan Kjellerstrand hakank@bonetmail.com
#
# 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.
"""
Best host (PuzzlOr problem) in MiniZinc.
From The PuzzlOr, February | Volume 38 | Number 1
http://www.informs.org/ORMS-Today/Public-Articles/February-Volume-38-Number-1/THE-PUZZLOR
'''
By John Toczek
Hosting a dinner party requires several skills to pull off a successful
evening. One of your duties, aside from preparing dinner and selecting
the drinks, is to make sure your guests enjoy themselves.
Figure 1 shows a dinner table with six seats for your guests. Some
guests, however, do not get along with each other. If two guests who
do not get along are seated next to each other, it will create conflict
at dinner. As host, you must arrange the guests in a seating order
that minimizes conflict.
Andrew will only sit next to Dave and Frank;
Betty will only sit next to Cara and Erica;
Cara will only sit next to Betty and Frank;
Dave will only sit next to Andrew and Erica;
Erica will only sit next to Betty and Dave;
Frank will only sit next to Andrew and Cara.
[
Figure 1 shows the following arrangement:
Andrew
Frank Betty
Erica Cara
Dave
]
In the example seating arrangement above, there are three conflicts
(Andrew and Betty, Cara and Dave, Erica and Frank).
Question:
What seating arrangement will minimize the conflict at dinner?
'''
Answer: There are 12 possible solutions with no conflict at all.
By placing Andrew at position 1 (as symmetry breaking) there are
2 possible solutions, where the 2nd solution is the mirror of the 1st.
1) Andrew Frank Cara Betty Erica Dave
Andrew
Dave Frank
Erica Cara
Betty
2) Andrew Dave Erica Betty Cara Frank
Andrew
Frank Dave
Cara Erica
Betty
This model was created by Hakan Kjellerstrand (hakank@bonetmail.com)
Also see my other Google CP Solver models: http://www.hakank.org/google_or_tools/
"""
import sys, string
from constraint_solver import pywrapcp
def main():
# Create the solver.
solver = pywrapcp.Solver('Best Host')
#
# data
#
n = 6
Andrew = 0
Betty = 1
Cara = 2
Dave = 3
Erica = 4
Frank = 5
name_str = ["Andrew", "Betty", "Cara", "Dave", "Erica", "Frank"]
prefs = [
[Dave, Frank], # Andrew
[Cara, Erica], # Betty
[Betty, Frank], # Cara
[Andrew, Erica], # Dave
[Betty, Dave], # Erica
[Andrew, Cara] # Frank
];
# declare variables
x = [solver.IntVar(0,n-1, 'x%i' % i) for i in range(n)]
#
# constraints
#
solver.Add(solver.AllDifferent(x, True))
# symmetry breaking
solver.Add(x[0] == Andrew)
for i in range(n):
solver.Add(solver.MemberCt(x[(i-1) % n], solver.Element(prefs, x[i])))
# solver.Add(solver.MemberCt(x[(i+1) % n], prefs[x[i]]))
#
# solution and search
#
# db = solver.Phase(winter_sum + x + bin_code,
# # solver.CHOOSE_MIN_SIZE_LOWEST_MAX,
# solver.CHOOSE_FIRST_UNBOUND,
# # solver.ASSIGN_MIN_VALUE
# solver.ASSIGN_CENTER_VALUE
# )
# solver.NewSearch(db, [objective])
parameters = pywrapcp.DefaultPhaseParameters()
parameters.heuristic_period = 200000
# parameters.var_selection_schema = parameters.CHOOSE_MAX_SUM_IMPACT
# parameters.var_selection_schema = parameters.CHOOSE_MAX_AVERAGE_IMPACT
parameters.var_selection_schema = parameters.CHOOSE_MAX_VALUE_IMPACT
parameters.value_selection_schema = parameters.SELECT_MIN_IMPACT
# parameters.value_selection_schema = parameters.SELECT_MAX_IMPACT
# parameters.restart_log_size = 20
db = solver.DefaultPhase(x, parameters)
solver.NewSearch(db)
num_solutions = 0
while solver.NextSolution():
num_solutions += 1
print "\nSolution %i" % num_solutions
print "x:", [x[i].Value() for i in range(m)]
print
solver.EndSearch()
if num_solutions == 0:
print "No solution found"
print
print "num_solutions:", num_solutions
print "failures:", solver.failures()
print "branches:", solver.branches()
print "wall_time:", solver.wall_time()
if __name__ == '__main__':
main()