-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmddGcc.cpp
More file actions
177 lines (148 loc) · 7.42 KB
/
mddGcc.cpp
File metadata and controls
177 lines (148 loc) · 7.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* mini-cp is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3
* as published by the Free Software Foundation.
*
* mini-cp is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with mini-cp. If not, see http://www.gnu.org/licenses/lgpl-3.0.en.html
*
* Copyright (c) 2018. by Laurent Michel, Pierre Schaus, Pascal Van Hentenryck
*/
#include "mddConstraints.hpp"
#include "mddnode.hpp"
#include <limits.h>
namespace Factory {
MDDCstrDesc::Ptr gccMDD(MDD::Ptr m,const Factory::Veci& vars,const std::map<int,int>& ub)
{
MDDSpec& spec = m->getSpec();
int sz = (int) vars.size();
auto udom = domRange(vars);
const int dz = udom.second - udom.first + 1; // size of union of all domains
const int minFDom = udom.first, minLDom = udom.second;
const int maxFDom = dz + udom.first,maxLDom = dz + udom.second;
ValueMap<int> values(udom.first, udom.second,0,ub);
auto desc = spec.makeConstraintDescriptor(vars,"gccMDD");
std::map<int,MDDPInt::Ptr> ps;
for(int i=minFDom; i <= maxLDom;++i)
ps[i] = spec.downIntState(desc,0,sz,i >= maxFDom ? MaxFun : MinFun);
spec.arcExist(desc,[=](const auto& parent,const auto& child,auto x,int v) {
return parent.down[ps.at(v)] < values[v];
});
for(int i=minFDom; i <= minLDom;++i)
spec.transitionDown(desc,ps[i],{ps[i]},{},[=](auto& out,const auto& parent,auto x, const auto& val) {
out[ps.at(i)] = parent.down[ps.at(i)] + (val.isSingleton() ? (val.singleton() == i) : 0);
});
for(int i=maxFDom; i <= maxLDom;++i)
spec.transitionDown(desc,ps[i],{ps[i]},{},[=](auto& out,const auto& parent,auto x, const auto& val) {
out[ps.at(i)] = parent.down[ps.at(i)] + (val.isSingleton() ? (val.singleton() == i - dz) : 0);
});
return desc;
}
MDDCstrDesc::Ptr atMostMDD(MDD::Ptr m,const Factory::Veci& vars,const std::map<int,int>& ub)
{
MDDSpec& spec = m->getSpec();
auto [minFDom,minLDom] = domRange(vars);
auto desc = spec.makeConstraintDescriptor(vars,"atMostMDD");
std::map<int,MDDPInt::Ptr> pd;
for(int i=minFDom; i <= minLDom;++i)
pd[i] = spec.downIntState(desc,0,INT_MAX,MinFun);
spec.arcExist(desc,[=](const auto& parent,const auto& child,auto x,int v) {
return parent.down[pd.at(v)] < ub.at(v);
});
for(int i=minFDom; i <= minLDom;++i)
spec.transitionDown(desc,pd[i],{pd[i]},{},[=](auto& out,const auto& parent,auto x, const auto& val) {
out[pd.at(i)] = parent.down[pd.at(i)] + (val.isSingleton() ? val.contains(i) : 0);
});
return desc;
}
MDDCstrDesc::Ptr atMostMDD2(MDD::Ptr m,const Factory::Veci& vars,const std::map<int,int>& ub)
{
MDDSpec& spec = m->getSpec();
auto udom = domRange(vars);
const int minFDom = udom.first, minLDom = udom.second;
ValueMap<int> values(udom.first, udom.second,0,ub);
auto desc = spec.makeConstraintDescriptor(vars,"atMostMDD");
std::map<int,MDDPInt::Ptr> pd;
for(int i=minFDom; i <= minLDom;++i)
pd[i] = spec.downIntState(desc,0,INT_MAX,MinFun);
std::map<int,MDDPInt::Ptr> pu;
for(int i=minFDom; i <= minLDom;++i)
pu[i] = spec.upIntState(desc,0,INT_MAX,MinFun);
spec.arcExist(desc,[=](const auto& parent,const auto& child,auto x,int v) {
return parent.down[pd.at(v)] + child.up[pu.at(v)] < values[v]; // we are given value v, use prefix/suffix counters and leave room (1) for this arc
});
for(int i=minFDom; i <= minLDom;++i)
spec.transitionDown(desc,pd[i],{pd[i]},{},[=](auto& out,const auto& parent,auto,const auto& val) {
out[pd.at(i)] = parent.down[pd.at(i)] + (val.isSingleton() ? val.contains(i) : 0);
});
for(int i=minFDom; i <= minLDom;++i)
spec.transitionUp(desc,pu[i],{pu[i]},{},[=](auto& out,const auto& child,auto,const auto& val) {
out[pu.at(i)] = child.up[pu.at(i)] + (val.isSingleton() ? val.contains(i) : 0);
});
return desc;
}
MDDCstrDesc::Ptr gccMDD2(MDD::Ptr m,const Factory::Veci& vars,const std::map<int,int>& lb,const std::map<int,int>& ub)
{
MDDSpec& spec = m->getSpec();
int sz = (int) vars.size();
auto udom = domRange(vars);
int dz = udom.second - udom.first + 1;
int minFDom = udom.first, minLDom = udom.second;
int maxFDom = dz+udom.first, maxLDom = dz+udom.second;
ValueMap<int> valuesLB(udom.first, udom.second,0,lb);
ValueMap<int> valuesUB(udom.first, udom.second,0,ub);
auto desc = spec.makeConstraintDescriptor(vars,"gccMDD");
std::map<int,MDDPInt::Ptr> dps,ups;
for(int i=minFDom;i <= maxLDom;++i)
dps[i] = spec.downIntState(desc,0,sz,i >=maxFDom ? MaxFun : MinFun);
for(int i=minFDom;i <= maxLDom;++i)
ups[i] = spec.upIntState(desc,0,sz,i >=maxFDom ? MaxFun : MinFun);
spec.arcExist(desc,[=](const auto& parent,const auto& child,auto x,int v) {
bool cond = true;
const int minIdx = v;
const int maxIdx = dz + v;
// check LB and UB thresholds when value v is assigned:
cond = cond && (parent.down[dps.at(minIdx)] + 1 + child.up[ups.at(minIdx)] <= valuesUB[v])
&& (parent.down[dps.at(maxIdx)] + 1 + child.up[ups.at(maxIdx)] >= valuesLB[v]);
// check LB and UB thresholds for other values, when they are not assigned:
for (int i=minFDom; i<=minLDom; i++) {
if (i==v) continue;
if (!cond) break;
cond = cond && (parent.down[dps.at(i)] + child.up[ups.at(i)] <= valuesUB[i])
&& (parent.down[dps.at(dz+i)] + child.up[ups.at(dz+i)] >= valuesLB[i]);
}
return cond;
});
spec.nodeExist([=](const auto& n) {
// check global validity: can we still satisfy all lower bounds?
int remainingLB=0;
int fixedValues=0;
for (int i=minFDom; i<=minLDom; i++) {
remainingLB += std::max(0, valuesLB[i] - (n.down[dps.at(i)] + n.up[ups.at(i)]));
fixedValues += n.down[dps.at(i)] + n.up[ups.at(i)];
}
return (fixedValues+remainingLB<=sz);
});
for(int i=minFDom;i <= minLDom;++i)
spec.transitionDown(desc,dps[i],{dps[i]},{},[=](auto& out,const auto& parent,auto x,const auto& val) {
out[dps.at(i)] = parent.down[dps.at(i)] + (val.isSingleton() && (val.singleton() == i));
});
for(int i=maxFDom;i <= maxLDom;++i)
spec.transitionDown(desc,dps[i],{dps[i]},{},[=](auto& out,const auto& parent,auto x,const auto& val) {
out[dps.at(i)] = parent.down[dps.at(i)] + val.contains(i - dz);
});
for(int i=minFDom;i <= minLDom;++i)
spec.transitionUp(desc,ups[i],{ups[i]},{},[=](auto& out,const auto& child,auto x,const auto& val) {
out[ups.at(i)] = child.up[ups.at(i)] + (val.isSingleton() && (val.singleton() == i));
});
for(int i=maxFDom;i <= maxLDom;++i)
spec.transitionUp(desc,ups[i],{ups[i]},{},[=](auto& out,const auto& child,auto x,const auto& val) {
out[ups.at(i)] = child.up[ups.at(i)] + val.contains(i - dz);
});
return desc;
}
}