Generated on Sat Feb 7 2015 02:01:11 for Gecode by doxygen 1.8.9.1
crew.cpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Guido Tack <tack@gecode.org>
5  * Christian Schulte <schulte@gecode.org>
6  *
7  * Copyright:
8  * Guido Tack, 2004
9  * Christian Schulte, 2004
10  *
11  * Last modified:
12  * $Date: 2013-07-08 14:22:40 +0200 (Mon, 08 Jul 2013) $ by $Author: schulte $
13  * $Revision: 13820 $
14  *
15  * This file is part of Gecode, the generic constraint
16  * development environment:
17  * http://www.gecode.org
18  *
19  * Permission is hereby granted, free of charge, to any person obtaining
20  * a copy of this software and associated documentation files (the
21  * "Software"), to deal in the Software without restriction, including
22  * without limitation the rights to use, copy, modify, merge, publish,
23  * distribute, sublicense, and/or sell copies of the Software, and to
24  * permit persons to whom the Software is furnished to do so, subject to
25  * the following conditions:
26  *
27  * The above copyright notice and this permission notice shall be
28  * included in all copies or substantial portions of the Software.
29  *
30  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
31  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
32  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
33  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
34  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
35  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
36  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37  *
38  */
39 
40 
41 #include <gecode/driver.hh>
42 #include <gecode/int.hh>
43 #include <gecode/set.hh>
44 
45 using namespace Gecode;
46 
47 namespace {
49  typedef enum {
50  Tom, David, Jeremy, Ron,
51  Joe, Bill, Fred,
52  Bob, Mario, Ed,
53  Carol, Janet, Tracy,
54  Marilyn, Carolyn, Cathy,
55  Inez, Jean, Heather, Juliet
56  } Employee;
57  const int noOfEmployees = Juliet+1;
58 
60  struct Flight {
61  int staff;
62  int stewards;
63  int hostesses;
64  int french;
65  int spanish;
66  int german;
67  };
68 
69  const char* employeeToName(Employee e);
70  extern const int stewards[];
71  extern const int noOfStewards;
72  extern const int hostesses[];
73  extern const int noOfHostesses;
74  extern const int spanishSpeaking[];
75  extern const int noOfSpanishSpeaking;
76  extern const int frenchSpeaking[];
77  extern const int noOfFrenchSpeaking;
78  extern const int germanSpeaking[];
79  extern const int noOfGermanSpeaking;
80  extern const Flight requiredCrew[];
81  extern const int noOfFlights;
82 }
83 
94 class Crew : public Script {
95 public:
98 
100  Crew(const Options&) :
101  flight(*this,noOfFlights,IntSet::empty,0,noOfEmployees-1)
102  {
103  IntSet stewardsDS(stewards,noOfStewards);
104  IntSet hostessesDS(hostesses,noOfHostesses);
105  IntSet spanishDS(spanishSpeaking, noOfSpanishSpeaking);
106  IntSet frenchDS(frenchSpeaking, noOfFrenchSpeaking);
107  IntSet germanDS(germanSpeaking, noOfGermanSpeaking);
108 
109  for (int i=0; i<noOfFlights; i++) {
110  // The flight has staff as required by the specification
111  rel(*this, cardinality(flight[i]) == requiredCrew[i].staff);
112 
113  // Enough members of different categories are on board
114  rel(*this, cardinality(flight[i] & stewardsDS) >=
115  requiredCrew[i].stewards);
116  rel(*this, cardinality(flight[i] & hostessesDS) >=
117  requiredCrew[i].hostesses);
118  rel(*this, cardinality(flight[i] & spanishDS) >=
119  requiredCrew[i].spanish);
120  rel(*this, cardinality(flight[i] & frenchDS) >=
121  requiredCrew[i].french);
122  rel(*this, cardinality(flight[i] & germanDS) >=
123  requiredCrew[i].german);
124  }
125 
126  // No crew member of flight i works on flights i+1 and i+2
127  for (int i=0; i<noOfFlights-2; i++) {
128  rel(*this, flight[i] || flight[i+1]);
129  rel(*this, flight[i] || flight[i+2]);
130  }
131  rel(*this, flight[noOfFlights-2] || flight[noOfFlights-1]);
132 
133  branch(*this, flight, SET_VAR_NONE(), SET_VAL_MIN_INC());
134  }
135 
137  virtual void
138  print(std::ostream& os) const {
139  for (int i=0; i<noOfFlights; i++) {
140  os << "\tFlight " << i+1 << ":" << std::endl;
141  os << "\t\tCrew\tStew.\tHost.\tFrench\tSpanish\tGerman"
142  << std::endl << "\t";
143  os << "\t" << requiredCrew[i].staff << "\t" << requiredCrew[i].stewards
144  << "\t" << requiredCrew[i].hostesses << "\t"
145  << requiredCrew[i].spanish
146  << "\t" << requiredCrew[i].french << "\t" << requiredCrew[i].german
147  << std::endl;
148 
149  os << "\t\tSchedule:" << std::endl << "\t\t";
150  if (flight[i].assigned()) {
151  for (SetVarGlbValues d(flight[i]);d();++d) {
152  os << employeeToName(static_cast<Employee>(d.val())) << " ";
153  }
154  } else {
155  os << "\tRequired: ";
156  for (SetVarGlbValues d(flight[i]);d();++d) {
157  os << employeeToName(static_cast<Employee>(d.val())) << " ";
158  }
159  os << std::endl << "\t\t\tPossible: ";
160  for (SetVarUnknownValues d(flight[i]);d();++d) {
161  os << employeeToName(static_cast<Employee>(d.val())) << " ";
162  }
163  }
164  os << std::endl << std::endl;
165  }
166  }
167 
169  Crew(bool share, Crew& s)
170  : Script(share,s) {
171  flight.update(*this,share,s.flight);
172  }
174  virtual
175  Space *copy(bool share) {
176  return new Crew(share,*this);
177  }
178 
179 };
180 
184 int
185 main(int argc, char* argv[]) {
186  Options o("Crew");
187  o.iterations(100);
188  o.parse(argc,argv);
189  Script::run<Crew,DFS,Options>(o);
190  return 0;
191 }
192 
193 namespace {
194 
196  const char*
197  employeeToName(Employee e) {
198  switch(e) {
199  case Tom : return "Tom";
200  case David : return "David";
201  case Jeremy: return "Jeremy";
202  case Ron: return "Ron";
203  case Joe: return "Joe";
204  case Bill: return "Bill";
205  case Fred: return "Fred";
206  case Bob: return "Bob";
207  case Mario: return "Mario";
208  case Ed: return "Ed";
209  case Carol: return "Carol";
210  case Janet: return "Janet";
211  case Tracy: return "Tracy";
212  case Marilyn: return "Marilyn";
213  case Carolyn: return "Carolyn";
214  case Cathy: return "Cathy";
215  case Inez: return "Inez";
216  case Jean: return "Jean";
217  case Heather: return "Heather";
218  case Juliet: return "Juliet";
219  default: GECODE_NEVER; return "";
220  }
221  }
222 
223  // these have to be sorted!
225  const int stewards[] =
226  {Tom, David, Jeremy, Ron, Joe, Bill, Fred, Bob, Mario, Ed};
228  const int noOfStewards = sizeof(stewards) / sizeof(int);
230  const int hostesses[] =
231  { Carol, Janet, Tracy, Marilyn, Carolyn, Cathy, Inez,
232  Jean, Heather, Juliet };
234  const int noOfHostesses = sizeof(hostesses) / sizeof(int);
236  const int frenchSpeaking[] =
237  { Bill, Inez, Jean, Juliet };
239  const int noOfFrenchSpeaking = sizeof(frenchSpeaking) / sizeof(int);
241  const int germanSpeaking[] =
242  { Tom, Jeremy, Mario, Cathy, Juliet };
244  const int noOfGermanSpeaking = sizeof(germanSpeaking) / sizeof(int);
246  const int spanishSpeaking[] =
247  { Joe, Bill, Fred, Mario, Marilyn, Inez, Heather };
249  const int noOfSpanishSpeaking = sizeof(spanishSpeaking) / sizeof(int);
250 
252  const Flight requiredCrew[] =
253  { {4,1,1,1,1,1},
254  {5,1,1,1,1,1},
255  {5,1,1,1,1,1},
256  {6,2,2,1,1,1},
257  {7,3,3,1,1,1},
258  {4,1,1,1,1,1},
259  {5,1,1,1,1,1},
260  {6,1,1,1,1,1},
261  {6,2,2,1,1,1},
262  {7,3,3,1,1,1} };
263 
265  const int noOfFlights = sizeof(requiredCrew) / sizeof(Flight);
266 }
267 
268 // STATISTICS: example-any
269 
virtual Space * copy(bool share)
Copy during cloning.
Definition: crew.cpp:175
SetVarBranch SET_VAR_NONE(void)
Select first unassigned variable.
Definition: var.hpp:91
SetVarArray flight
The crew for each flight.
Definition: crew.cpp:97
virtual void print(std::ostream &os) const
Print solution.
Definition: crew.cpp:138
SetValBranch SET_VAL_MIN_INC(void)
Include smallest element.
Definition: val.hpp:59
Example: Airline crew allocation
Definition: crew.cpp:94
Computation spaces.
Definition: core.hpp:1362
Parametric base-class for scripts.
Definition: driver.hh:622
void iterations(unsigned int i)
Set default number of iterations.
Definition: options.hpp:385
Employee
The airline employees.
Definition: crew.cpp:49
int main(int argc, char *argv[])
Main-function.
Definition: crew.cpp:185
Gecode::IntSet d(v, 7)
Iterator for the values in the unknown set of a set variable.
Definition: set.hh:424
void update(Space &, bool share, VarArray< Var > &a)
Update array to be a clone of array a.
Definition: array.hpp:1072
Gecode::IntArgs i(4, 1, 2, 3, 4)
Integer sets.
Definition: int.hh:171
LinIntExpr cardinality(const SetExpr &e)
Cardinality of set expression.
Definition: set-expr.cpp:815
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc)
Definition: options.cpp:331
bool assigned(View x, int v)
Whether x is assigned to value v.
Definition: single.hpp:47
Iterator for the values in the greatest lower bound of a set variable.
Definition: set.hh:368
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVal n)
Propagates .
Definition: rel.cpp:47
Crew(bool share, Crew &s)
Constructor for cloning s.
Definition: crew.cpp:169
Set variable array
Definition: set.hh:571
Gecode toplevel namespace
BrancherHandle branch(Home home, const FloatVarArgs &x, FloatVarBranch vars, FloatValBranch vals, FloatBranchFilter bf, FloatVarValPrint vvp)
Branch over x with variable selection vars and value selection vals.
Definition: branch.cpp:43
Crew(const Options &)
The actual model.
Definition: crew.cpp:100
#define GECODE_NEVER
Assert that this command is never executed.
Definition: macros.hpp:60
Options for scripts
Definition: driver.hh:326