1# Copyright 2010 Hakan Kjellerstrand hakank@gmail.com
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""
15
16  Ski assignment in Google CP Solver.
17
18  From   Jeffrey Lee Hellrung, Jr.:
19  PIC 60, Fall 2008 Final Review, December 12, 2008
20  http://www.math.ucla.edu/~jhellrun/course_files/Fall%25202008/PIC%252060%2520-%2520Data%2520Structures%2520and%2520Algorithms/final_review.pdf
21  '''
22  5. Ski Optimization! Your job at Snapple is pleasant but in the winter
23  you've decided to become a ski bum. You've hooked up with the Mount
24  Baldy Ski Resort. They'll let you ski all winter for free in exchange
25  for helping their ski rental shop with an algorithm to assign skis to
26  skiers. Ideally, each skier should obtain a pair of skis whose height
27  matches his or her own height exactly. Unfortunately, this is generally
28  not possible. We define the disparity between a skier and his or her
29  skis to be the absolute value of the difference between the height of
30  the skier and the pair of skis. Our objective is to find an assignment
31  of skis to skiers that minimizes the sum of the disparities.
32  ...
33  Illustrate your algorithm by explicitly filling out the A[i, j] table
34  for the following sample data:
35    * Ski heights: 1, 2, 5, 7, 13, 21.
36    * Skier heights: 3, 4, 7, 11, 18.
37  '''
38
39  Compare with the following models:
40  * Comet   : http://www.hakank.org/comet/ski_assignment.co
41  * MiniZinc: http://hakank.org/minizinc/ski_assignment.mzn
42  * ECLiPSe : http://www.hakank.org/eclipse/ski_assignment.ecl
43  * SICStus: http://hakank.org/sicstus/ski_assignment.pl
44  * Gecode: http://hakank.org/gecode/ski_assignment.cpp
45
46  This model was created by Hakan Kjellerstrand (hakank@gmail.com)
47  Also see my other Google CP Solver models:
48  http://www.hakank.org/google_or_tools/
49"""
50import sys
51
52from ortools.constraint_solver import pywrapcp
53
54
55def main():
56
57  # Create the solver.
58  solver = pywrapcp.Solver('Ski assignment')
59
60  #
61  # data
62  #
63  num_skis = 6
64  num_skiers = 5
65  ski_heights = [1, 2, 5, 7, 13, 21]
66  skier_heights = [3, 4, 7, 11, 18]
67
68  #
69  # variables
70  #
71
72  # which ski to choose for each skier
73  x = [solver.IntVar(0, num_skis - 1, 'x[%i]' % i) for i in range(num_skiers)]
74  z = solver.IntVar(0, sum(ski_heights), 'z')
75
76  #
77  # constraints
78  #
79  solver.Add(solver.AllDifferent(x))
80
81  z_tmp = [
82      abs(solver.Element(ski_heights, x[i]) - skier_heights[i])
83      for i in range(num_skiers)
84  ]
85  solver.Add(z == sum(z_tmp))
86
87  # objective
88  objective = solver.Minimize(z, 1)
89
90  #
91  # search and result
92  #
93  db = solver.Phase(x, solver.INT_VAR_DEFAULT, solver.INT_VALUE_DEFAULT)
94
95  solver.NewSearch(db, [objective])
96
97  num_solutions = 0
98  while solver.NextSolution():
99    num_solutions += 1
100    print('total differences:', z.Value())
101    for i in range(num_skiers):
102      x_val = x[i].Value()
103      ski_height = ski_heights[x[i].Value()]
104      diff = ski_height - skier_heights[i]
105      print('Skier %i: Ski %i with length %2i (diff: %2i)' %\
106            (i, x_val, ski_height, diff))
107    print()
108
109  solver.EndSearch()
110
111  print()
112  print('num_solutions:', num_solutions)
113  print('failures:', solver.Failures())
114  print('branches:', solver.Branches())
115  print('WallTime:', solver.WallTime())
116
117
118if __name__ == '__main__':
119  main()
120