Generated on for Gecode by doxygen 1.17.0
linear.cpp
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 * Vincent Barichard <Vincent.Barichard@univ-angers.fr>
6 *
7 * Copyright:
8 * Christian Schulte, 2005
9 * Vincent Barichard, 2012
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 "test/float.hh"
37
38#include <gecode/minimodel.hh>
39
40namespace Test { namespace Float {
41
43 namespace Linear {
44
46 bool one(const Gecode::FloatValArgs& a) {
47 for (int i=a.size(); i--; )
48 if (a[i] != 1)
49 return false;
50 return true;
51 }
52
58
59 class FloatFloat : public Test {
60 protected:
67 public:
69 FloatFloat(TestTags tags, const std::string& s,
70 const Gecode::FloatVal& d,
73 : Test(tags,"Linear::Float::"+
74 str(frt0)+"::"+s+"::"+str(c0)+"::"
75 +str(a0.size()),
76 a0.size(),d,st,CPLT_ASSIGNMENT,true),
77 a(a0), frt(frt0), c(c0) {
78 using namespace Gecode;
79 testfix = false;
80 if ((frt == FRT_NQ) || (frt == FRT_LE) || (frt == FRT_GR) || reified)
81 testsubsumed = false;
82 }
83
84 virtual MaybeType solution(const Assignment& x) const {
85 Gecode::FloatVal e = 0.0;
86 for (int i=x.size(); i--; )
87 e += a[i]*x[i];
88 switch (cmp(e, frt, Gecode::FloatVal(c))) {
89 case MT_FALSE: {
90 Gecode::FloatVal eError = e;
91 for (int i=x.size(); i--; )
92 eError -= a[i]*x[i];
93 if (cmp(e+eError, frt, Gecode::FloatVal(c)) == MT_FALSE)
94 return MT_FALSE;
95 else
96 return MT_MAYBE;
97 }
98 case MT_TRUE:
99 return MT_TRUE;
100 case MT_MAYBE:
101 return MT_MAYBE;
102 }
104 return MT_FALSE;
105 }
106
107 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
108 if (one(a))
109 Gecode::linear(home, x, frt, c);
110 else
111 Gecode::linear(home, a, x, frt, c);
112 }
113
116 if (one(a))
117 Gecode::linear(home, x, frt, c, r);
118 else
119 Gecode::linear(home, a, x, frt, c, r);
120 }
121 };
122
124 class FloatVar : public Test {
125 protected:
130 public:
132 FloatVar(TestTags tags, const std::string& s,
133 const Gecode::FloatVal& d,
135 : Test(tags,"Linear::Var::"+
136 str(frt0)+"::"+s+"::"+str(a0.size()),
137 a0.size()+1,d,st,CPLT_ASSIGNMENT,true),
138 a(a0), frt(frt0) {
139 using namespace Gecode;
140 testfix = false;
141 if ((frt == FRT_NQ) || (frt == FRT_LE) || (frt == FRT_GR) || reified)
142 testsubsumed = false;
143 }
144
145 virtual MaybeType solution(const Assignment& x) const {
146 Gecode::FloatVal e = 0.0;
147 for (int i=a.size(); i--; )
148 e += a[i]*x[i];
149 switch (cmp(e, frt, x[a.size()])) {
150 case MT_FALSE: {
151 Gecode::FloatVal eError = e;
152 for (int i=a.size(); i--; )
153 eError -= a[i]*x[i];
154 if (cmp(e+eError, frt, x[a.size()]) == MT_FALSE)
155 return MT_FALSE;
156 else
157 return MT_MAYBE;
158 }
159 case MT_TRUE:
160 return MT_TRUE;
161 case MT_MAYBE:
162 return MT_MAYBE;
163 }
165 return MT_FALSE;
166 }
167
168 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
169 int n = a.size();
171 for (int i=n; i--; )
172 y[i] = x[i];
173 if (one(a))
174 Gecode::linear(home, y, frt, x[n]);
175 else
176 Gecode::linear(home, a, y, frt, x[n]);
177 }
178
181 int n = a.size();
183 for (int i=n; i--; )
184 y[i] = x[i];
185 if (one(a))
186 Gecode::linear(home, y, frt, x[n], r);
187 else
188 Gecode::linear(home, a, y, frt, x[n], r);
189 }
190 };
191
193 class Create {
194 public:
196 Create(void) {
197 using namespace Gecode;
198 {
199 FloatNum step = 0.7;
200 FloatVal f1(-2,2);
201 FloatVal f2(-3,-1);
202 FloatVal f3(3,8);
203
204 FloatValArgs a1(1);
205 a1[0] = 0.0;
206
207 for (FloatRelTypes frts; frts(); ++frts) {
208 TestTags tags = frts.frt() == Gecode::FRT_EQ
210 (void) new FloatFloat(tags,"11",f1,a1,frts.frt(),0.0,step);
211 (void) new FloatVar(tags,"11",f1,a1,frts.frt(),step);
212 (void) new FloatFloat(TestTags::sweep(),"21",f2,a1,frts.frt(),0.0,step);
213 (void) new FloatVar(TestTags::sweep(),"21",f2,a1,frts.frt(),step);
214 (void) new FloatFloat(TestTags::sweep(),"31",f3,a1,frts.frt(),1.0,step);
215 }
216
217 const FloatVal av2[4] = {1.0,1.0,1.0,1.0};
218 const FloatVal av3[4] = {1.0,-1.0,-1.0,1.0};
219 const FloatVal av4[4] = {2.0,3.0,5.0,7.0};
220 const FloatVal av5[4] = {-2.0,3.0,-5.0,7.0};
221
222 for (int i=1; i<=4; i++) {
223 FloatValArgs a2(i, av2);
224 FloatValArgs a3(i, av3);
225 FloatValArgs a4(i, av4);
226 FloatValArgs a5(i, av5);
227 for (FloatRelTypes frts; frts(); ++frts) {
228 (void) new FloatFloat(TestTags::sweep(),"12",f1,a2,frts.frt(),0.0,step);
229 (void) new FloatFloat(TestTags::sweep(),"13",f1,a3,frts.frt(),0.0,step);
230 (void) new FloatFloat(TestTags::sweep(),"14",f1,a4,frts.frt(),0.0,step);
231 (void) new FloatFloat(TestTags::sweep(),"15",f1,a5,frts.frt(),0.0,step);
232 (void) new FloatFloat(TestTags::sweep(),"22",f2,a2,frts.frt(),0.0,step);
233 (void) new FloatFloat(TestTags::sweep(),"23",f2,a3,frts.frt(),0.0,step);
234 (void) new FloatFloat(TestTags::sweep(),"24",f2,a4,frts.frt(),0.0,step);
235 (void) new FloatFloat(TestTags::sweep(),"25",f2,a5,frts.frt(),0.0,step);
236 (void) new FloatFloat(TestTags::sweep(),"32",f3,a2,frts.frt(),1.0,step);
237 if (i < 4) {
238 (void) new FloatVar(TestTags::sweep(),"12",f1,a2,frts.frt(),step);
239 (void) new FloatVar(TestTags::sweep(),"13",f1,a3,frts.frt(),step);
240 (void) new FloatVar(TestTags::sweep(),"14",f1,a4,frts.frt(),step);
241 (void) new FloatVar(TestTags::sweep(),"15",f1,a5,frts.frt(),step);
242 (void) new FloatVar(TestTags::sweep(),"22",f2,a2,frts.frt(),step);
243 (void) new FloatVar(TestTags::sweep(),"23",f2,a3,frts.frt(),step);
244 (void) new FloatVar(TestTags::sweep(),"24",f2,a4,frts.frt(),step);
245 (void) new FloatVar(TestTags::sweep(),"25",f2,a5,frts.frt(),step);
246 }
247 }
248 }
249 }
250 }
251 };
252
255
256 }
257}}
258
259// STATISTICS: test-float
Passing float arguments.
Definition float.hh:948
Float value type.
Definition float.hh:329
FloatNum size(void) const
Return size of float value (distance between maximum and minimum).
Definition val.hpp:78
Passing float variables.
Definition float.hh:977
Float variable array.
Definition float.hh:1030
Reification specification.
Definition int.hh:1176
Computation spaces.
Definition core.hpp:1787
TestTags tags(void) const
Return tags for test.
Definition test.hpp:95
Base class for assignments
Definition float.hh:94
int size(void) const
Return number of variables.
Definition float.hpp:48
Iterator for float relation types.
Definition float.hh:362
Help class to create and register tests.
Definition linear.cpp:193
Create(void)
Perform creation and registration.
Definition linear.cpp:196
Test linear relation over float variables
Definition linear.cpp:59
virtual MaybeType solution(const Assignment &x) const
Test whether x is solution
Definition linear.cpp:84
FloatFloat(TestTags tags, const std::string &s, const Gecode::FloatVal &d, const Gecode::FloatValArgs &a0, Gecode::FloatRelType frt0, Gecode::FloatNum c0, Gecode::FloatNum st)
Create and register test.
Definition linear.cpp:69
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)
Post constraint on x.
Definition linear.cpp:107
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x, Gecode::Reify r)
Post reified constraint on x for r.
Definition linear.cpp:114
Gecode::FloatRelType frt
Float relation type to propagate.
Definition linear.cpp:64
Gecode::FloatValArgs a
Coefficients.
Definition linear.cpp:62
Gecode::FloatNum c
Result.
Definition linear.cpp:66
Test linear relation over float variables
Definition linear.cpp:124
Gecode::FloatRelType frt
Float relation type to propagate.
Definition linear.cpp:129
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)
Post constraint on x.
Definition linear.cpp:168
FloatVar(TestTags tags, const std::string &s, const Gecode::FloatVal &d, const Gecode::FloatValArgs &a0, Gecode::FloatRelType frt0, Gecode::FloatNum st)
Create and register test.
Definition linear.cpp:132
virtual MaybeType solution(const Assignment &x) const
Test whether x is solution
Definition linear.cpp:145
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x, Gecode::Reify r)
Post reified constraint on x for r.
Definition linear.cpp:179
Gecode::FloatValArgs a
Coefficients.
Definition linear.cpp:127
static MaybeType cmp(Gecode::FloatVal x, Gecode::FloatRelType r, Gecode::FloatVal y)
Compare x and y with respect to r.
Definition float.hpp:248
bool testsubsumed
Whether to test for subsumption.
Definition float.hh:278
bool testfix
Whether to perform fixpoint test.
Definition float.hh:276
static std::string str(Gecode::FloatRelType frt)
Map float relation to string.
Definition float.hpp:207
bool reified
Does the constraint also exist as reified constraint.
Definition float.hh:270
Set of test tags.
Definition test.hh:105
static TestTags sweep(void)
Expensive parameter sweeps.
Definition test.hpp:59
static TestTags standard(void)
Standard suite without expensive sweeps.
Definition test.hpp:55
void linear(Home home, const FloatVarArgs &x, FloatRelType frt, FloatVal c)
Post propagator for .
Definition linear.cpp:41
double FloatNum
Floating point number base type.
Definition float.hh:106
FloatRelType
Relation types for floats.
Definition float.hh:1070
@ FRT_EQ
Equality ( ).
Definition float.hh:1071
@ FRT_GR
Greater ( ).
Definition float.hh:1076
@ FRT_NQ
Disequality ( ).
Definition float.hh:1072
@ FRT_LE
Less ( ).
Definition float.hh:1074
Gecode toplevel namespace
Tests for linear constraints
Definition linear.cpp:43
bool one(const Gecode::FloatValArgs &a)
Check whether has only one coefficients.
Definition linear.cpp:46
Testing domain floats.
Definition float.cpp:43
@ CPLT_ASSIGNMENT
Definition float.hh:76
MaybeType
Type for comparisons and solutions.
Definition float.hh:65
General test support.
Definition afc.cpp:41
Region r
Definition region.cpp:65
#define GECODE_NEVER
Assert that this command is never executed.
Definition macros.hpp:56