Generated on for Gecode by doxygen 1.17.0
opt-prop.hpp
Go to the documentation of this file.
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main authors:
4 * Christian Schulte <schulte@gecode.dev>
5 * Guido Tack <tack@gecode.dev>
6 *
7 * Copyright:
8 * Christian Schulte, 2009
9 * Guido Tack, 2010
10 *
11 * This file is part of Gecode, the generic constraint
12 * development environment:
13 * http://www.gecode.dev
14 *
15 * Permission is hereby granted, free of charge, to any person obtaining
16 * a copy of this software and associated documentation files (the
17 * "Software"), to deal in the Software without restriction, including
18 * without limitation the rights to use, copy, modify, merge, publish,
19 * distribute, sublicense, and/or sell copies of the Software, and to
20 * permit persons to whom the Software is furnished to do so, subject to
21 * the following conditions:
22 *
23 * The above copyright notice and this permission notice shall be
24 * included in all copies or substantial portions of the Software.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 *
34 */
35
36#include <algorithm>
37
38namespace Gecode { namespace Int { namespace Cumulative {
39
40 template<class OptTask, class Cap, class PL>
43 : TaskProp<OptTask,PL>(home,t), c(c0) {
44 c.subscribe(home,*this,PC_INT_BND);
45 }
46
47 template<class OptTask, class Cap, class PL>
50 : TaskProp<OptTask,PL>(home,p) {
51 c.update(home,p.c);
52 }
53
54 template<class OptTask, class Cap, class PL>
57 // Capacity must be nonnegative
58 GECODE_ME_CHECK(c.gq(home, 0));
59 // Check for overload by single task and remove excluded tasks
60 int n=t.size(), m=0;
61 for (int i=n; i--; ) {
62 if (t[i].c() > c.max())
63 GECODE_ME_CHECK(t[i].excluded(home));
64 if (t[i].excluded())
65 t[i]=t[--n];
66 else if (t[i].mandatory())
67 m++;
68 }
69 t.size(n);
70 if (t.size() < 2) {
71 if (t.size() == 1) {
72 if (t[0].mandatory()) {
73 GECODE_ME_CHECK(c.gq(home, t[0].c()));
74 return ES_OK;
75 } else if (c.min() >= t[0].c()) {
76 return ES_OK;
77 }
78 } else {
79 return ES_OK;
80 }
81 }
82 if (c.assigned() && (c.val() == 1)) {
84 for (int i=0; i<t.size(); i++)
85 mt[i]=t[i];
87 ::post(home,mt);
88 }
89 if (m == t.size()) {
91 for (int i=0; i<m; i++)
92 mt[i].init(t[i]);
94 ::post(home,c,mt);
95 }
96 (void) new (home) OptProp<OptTask,Cap,PL>(home,c,t);
97 return ES_OK;
98 }
99
100 template<class OptTask, class Cap, class PL>
103 const ModEventDelta& med) const {
104 if (PL::advanced && SupportsKnapsackOverload<OptTask>::value &&
105 (c.max() >= 2) && (c.max() <= 127)) {
106 int mandatory = 0;
107 for (int i=0; i<t.size(); i++)
108 mandatory += t[i].mandatory();
109 if ((mandatory >= 4) && (mandatory <= 64))
110 return PropCost::quadratic(PropCost::HI,mandatory);
111 }
112 return TaskProp<OptTask,PL>::cost(home,med);
113 }
114
115 template<class OptTask, class Cap, class PL>
116 Actor*
118 return new (home) OptProp<OptTask,Cap,PL>(home,*this);
119 }
120
121 template<class OptTask, class Cap, class PL>
122 forceinline size_t
125 c.cancel(home,*this,PC_INT_BND);
126 return sizeof(*this);
127 }
128
129 template<class OptTask, class Cap, class PL>
132 // Did one of the Boolean views change?
134 GECODE_ES_CHECK((purge<OptTask,PL>(home,*this,t,c)));
135
136 // Only bounds changes?
137 if (IntView::me(med) != ME_INT_DOM)
138 GECODE_ES_CHECK(overload(home,c.max(),t));
139
140 if (PL::basic)
141 GECODE_ES_CHECK(timetabling(home,*this,c,t));
142
143 if (PL::advanced) {
144 // Partition into mandatory and optional activities
145 int n = t.size();
146 int i=0, j=n-1;
147 while (true) {
148 while ((i < n) && t[i].mandatory()) i++;
149 while ((j >= 0) && !t[j].mandatory()) j--;
150 if (i >= j) break;
151 std::swap(t[i],t[j]);
152 }
153
154 if (i > 1) {
155 // Truncate array to only contain mandatory tasks
156 t.size(i);
157 if (IntView::me(med) != ME_INT_DOM)
159 GECODE_ES_CHECK(edgefinding(home,c.max(),t));
160 // Restore to also include optional tasks
161 t.size(n);
162 }
163 }
164
165 if (Cap::varderived() && c.assigned() && c.val()==1) {
166 // Check that tasks do not overload resource
167 for (int i=0; i<t.size(); i++)
168 if (t[i].c() > 1)
169 GECODE_ME_CHECK(t[i].excluded(home));
170 // Rewrite to unary resource constraint
172 for (int i=0; i<t.size(); i++)
173 ut[i]=t[i];
174 GECODE_REWRITE(*this,
176 ::post(home(*this),ut)));
177 }
178
179 if (!PL::basic && c.assigned())
180 GECODE_ES_CHECK(subsumed(home,*this,c.val(),t));
181
182 return ES_NOFIX;
183 }
184
185}}}
186
187// STATISTICS: int-prop
Base-class for both propagators and branchers.
Definition core.hpp:635
Home class for posting propagators
Definition core.hpp:863
Scheduling propagator for cumulative resource with mandatory tasks.
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition opt-prop.hpp:131
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition opt-prop.hpp:123
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Return propagation cost.
Definition opt-prop.hpp:102
OptProp(Home home, Cap c, TaskArray< OptTask > &t)
Constructor for creation.
Definition opt-prop.hpp:42
static ExecStatus post(Home home, Cap c, TaskArray< OptTask > &t)
Post propagator that schedules tasks on cumulative resource.
Definition opt-prop.hpp:56
virtual Actor * copy(Space &home)
Perform copying during cloning.
Definition opt-prop.hpp:117
Task array.
Definition task.hh:165
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition prop.hpp:64
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function (defined as high linear).
Definition prop.hpp:52
TaskProp(Home home, TaskArray< OptTask > &t)
Definition prop.hpp:38
Traits class for mapping tasks to task views.
Definition task.hh:157
Scheduling propagator for unary resource with optional tasks
Definition unary.hh:814
Propagation cost.
Definition core.hpp:493
static PropCost quadratic(PropCost::Mod m, unsigned int n)
Quadratic complexity for modifier m and size measure n.
Definition core.hpp:5071
@ HI
Expensive.
Definition core.hpp:521
friend class Space
Definition core.hpp:1075
ModEventDelta med
A set of modification events (used during propagation).
Definition core.hpp:1084
static ModEvent me(const ModEventDelta &med)
Definition view.hpp:552
int ModEventDelta
Modification event deltas.
Definition core.hpp:94
#define GECODE_ME_CHECK(me)
Check whether modification event me is failed, and forward failure.
Definition macros.hpp:52
#define GECODE_REWRITE(prop, post)
Rewrite propagator by executing post function.
Definition macros.hpp:116
#define GECODE_ES_CHECK(es)
Check whether execution status es is failed or subsumed, and forward failure or subsumption.
Definition macros.hpp:91
Scheduling for cumulative resources
ExecStatus knapsack_overload(Space &, int, TaskArray< Task > &)
Perform knapsack-augmented overload checking.
ExecStatus timetabling(Space &home, Propagator &p, Cap c, TaskArray< Task > &t)
Perform time-tabling propagation.
ExecStatus subsumed(Space &home, Propagator &p, int c, TaskArray< Task > &t)
Check for subsumption (all tasks must be assigned).
ExecStatus edgefinding(Space &home, int c, TaskArray< Task > &t)
Propagate by edge-finding.
ExecStatus overload(Space &home, int c, TaskArray< ManTask > &t)
Check mandatory tasks t for overload.
Definition overload.hpp:41
Finite domain integers.
Definition lastval.hh:52
ExecStatus purge(Space &home, Propagator &p, TaskArray< OptTask > &t)
Purge optional tasks that are excluded and possibly rewrite propagator.
Definition purge.hpp:38
const Gecode::PropCond PC_INT_BND
Propagate when minimum or maximum of a view changes.
Definition var-type.hpp:99
const Gecode::ModEvent ME_BOOL_VAL
Domain operation has resulted in a value (assigned variable).
Definition var-type.hpp:124
const Gecode::ModEvent ME_INT_DOM
Domain operation has changed the domain.
Definition var-type.hpp:80
Gecode toplevel namespace
TFE post(PropagatorGroup g)
Only post functions (but not propagators) from g are considered.
Definition filter.cpp:138
ExecStatus
Definition core.hpp:479
@ ES_OK
Execution is okay.
Definition core.hpp:483
@ ES_NOFIX
Propagation has not computed fixpoint.
Definition core.hpp:482
#define forceinline
Definition config.hpp:144