Generated on Tue Mar 24 2020 14:04:04 for Gecode by doxygen 1.8.17
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.org>
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.org
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 
40 namespace 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  class FloatFloat : public Test {
60  protected:
67  public:
69  FloatFloat(const std::string& s, const Gecode::FloatVal& d,
72  : Test("Linear::Float::"+
73  str(frt0)+"::"+s+"::"+str(c0)+"::"
74  +str(a0.size()),
75  a0.size(),d,st,CPLT_ASSIGNMENT,true),
76  a(a0), frt(frt0), c(c0) {
77  using namespace Gecode;
78  testfix = false;
79  if ((frt == FRT_NQ) || (frt == FRT_LE) || (frt == FRT_GR) || reified)
80  testsubsumed = false;
81  }
83  virtual MaybeType solution(const Assignment& x) const {
84  Gecode::FloatVal e = 0.0;
85  for (int i=x.size(); i--; )
86  e += a[i]*x[i];
87  switch (cmp(e, frt, Gecode::FloatVal(c))) {
88  case MT_FALSE: {
89  Gecode::FloatVal eError = e;
90  for (int i=x.size(); i--; )
91  eError -= a[i]*x[i];
92  if (cmp(e+eError, frt, Gecode::FloatVal(c)) == MT_FALSE)
93  return MT_FALSE;
94  else
95  return MT_MAYBE;
96  }
97  case MT_TRUE:
98  return MT_TRUE;
99  case MT_MAYBE:
100  return MT_MAYBE;
101  }
102  GECODE_NEVER;
103  return MT_FALSE;
104  }
106  virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
107  if (one(a))
108  Gecode::linear(home, x, frt, c);
109  else
110  Gecode::linear(home, a, x, frt, c);
111  }
114  Gecode::Reify r) {
115  if (one(a))
116  Gecode::linear(home, x, frt, c, r);
117  else
118  Gecode::linear(home, a, x, frt, c, r);
119  }
120  };
121 
123  class FloatVar : public Test {
124  protected:
129  public:
131  FloatVar(const std::string& s, const Gecode::FloatVal& d,
133  : Test("Linear::Var::"+
134  str(frt0)+"::"+s+"::"+str(a0.size()),
135  a0.size()+1,d,st,CPLT_ASSIGNMENT,true),
136  a(a0), frt(frt0) {
137  using namespace Gecode;
138  testfix = false;
139  if ((frt == FRT_NQ) || (frt == FRT_LE) || (frt == FRT_GR) || reified)
140  testsubsumed = false;
141  }
143  virtual MaybeType solution(const Assignment& x) const {
144  Gecode::FloatVal e = 0.0;
145  for (int i=a.size(); i--; )
146  e += a[i]*x[i];
147  switch (cmp(e, frt, x[a.size()])) {
148  case MT_FALSE: {
149  Gecode::FloatVal eError = e;
150  for (int i=a.size(); i--; )
151  eError -= a[i]*x[i];
152  if (cmp(e+eError, frt, x[a.size()]) == MT_FALSE)
153  return MT_FALSE;
154  else
155  return MT_MAYBE;
156  }
157  case MT_TRUE:
158  return MT_TRUE;
159  case MT_MAYBE:
160  return MT_MAYBE;
161  }
162  GECODE_NEVER;
163  return MT_FALSE;
164  }
166  virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
167  int n = a.size();
169  for (int i=n; i--; )
170  y[i] = x[i];
171  if (one(a))
172  Gecode::linear(home, y, frt, x[n]);
173  else
174  Gecode::linear(home, a, y, frt, x[n]);
175  }
178  Gecode::Reify r) {
179  int n = a.size();
181  for (int i=n; i--; )
182  y[i] = x[i];
183  if (one(a))
184  Gecode::linear(home, y, frt, x[n], r);
185  else
186  Gecode::linear(home, a, y, frt, x[n], r);
187  }
188  };
189 
191  class Create {
192  public:
194  Create(void) {
195  using namespace Gecode;
196  {
197  FloatNum step = 0.7;
198  FloatVal f1(-2,2);
199  FloatVal f2(-3,-1);
200  FloatVal f3(3,8);
201 
202  FloatValArgs a1(1);
203  a1[0] = 0.0;
204 
205  for (FloatRelTypes frts; frts(); ++frts) {
206  (void) new FloatFloat("11",f1,a1,frts.frt(),0.0,step);
207  (void) new FloatVar("11",f1,a1,frts.frt(),step);
208  (void) new FloatFloat("21",f2,a1,frts.frt(),0.0,step);
209  (void) new FloatVar("21",f2,a1,frts.frt(),step);
210  (void) new FloatFloat("31",f3,a1,frts.frt(),1.0,step);
211  }
212 
213  const FloatVal av2[4] = {1.0,1.0,1.0,1.0};
214  const FloatVal av3[4] = {1.0,-1.0,-1.0,1.0};
215  const FloatVal av4[4] = {2.0,3.0,5.0,7.0};
216  const FloatVal av5[4] = {-2.0,3.0,-5.0,7.0};
217 
218  for (int i=1; i<=4; i++) {
219  FloatValArgs a2(i, av2);
220  FloatValArgs a3(i, av3);
221  FloatValArgs a4(i, av4);
222  FloatValArgs a5(i, av5);
223  for (FloatRelTypes frts; frts(); ++frts) {
224  (void) new FloatFloat("12",f1,a2,frts.frt(),0.0,step);
225  (void) new FloatFloat("13",f1,a3,frts.frt(),0.0,step);
226  (void) new FloatFloat("14",f1,a4,frts.frt(),0.0,step);
227  (void) new FloatFloat("15",f1,a5,frts.frt(),0.0,step);
228  (void) new FloatFloat("22",f2,a2,frts.frt(),0.0,step);
229  (void) new FloatFloat("23",f2,a3,frts.frt(),0.0,step);
230  (void) new FloatFloat("24",f2,a4,frts.frt(),0.0,step);
231  (void) new FloatFloat("25",f2,a5,frts.frt(),0.0,step);
232  (void) new FloatFloat("32",f3,a2,frts.frt(),1.0,step);
233  if (i < 4) {
234  (void) new FloatVar("12",f1,a2,frts.frt(),step);
235  (void) new FloatVar("13",f1,a3,frts.frt(),step);
236  (void) new FloatVar("14",f1,a4,frts.frt(),step);
237  (void) new FloatVar("15",f1,a5,frts.frt(),step);
238  (void) new FloatVar("22",f2,a2,frts.frt(),step);
239  (void) new FloatVar("23",f2,a3,frts.frt(),step);
240  (void) new FloatVar("24",f2,a4,frts.frt(),step);
241  (void) new FloatVar("25",f2,a5,frts.frt(),step);
242  }
243  }
244  }
245  }
246  }
247  };
248 
251 
252  }
253 }}
254 
255 // STATISTICS: test-float
@ FRT_LE
Less ( )
Definition: float.hh:1072
Passing float variables.
Definition: float.hh:979
Region r
Definition: region.cpp:65
Post propagator for SetVar SetOpType SetVar y
Definition: set.hh:767
@ FRT_NQ
Disequality ( )
Definition: float.hh:1070
@ MT_MAYBE
Definition: float.hh:54
Gecode::FloatRelType frt
Float relation type to propagate.
Definition: linear.cpp:64
@ MT_FALSE
Definition: float.hh:52
bool testfix
Whether to perform fixpoint test.
Definition: float.hh:261
unsigned int size(I &i)
Size of all ranges of range iterator i.
Create(void)
Perform creation and registration.
Definition: linear.cpp:194
bool reified
Does the constraint also exist as reified constraint.
Definition: float.hh:255
Passing float arguments.
Definition: float.hh:950
Computation spaces.
Definition: core.hpp:1742
@ FRT_GR
Greater ( )
Definition: float.hh:1074
@ MT_TRUE
Definition: float.hh:53
FloatRelType
Relation types for floats.
Definition: float.hh:1068
virtual MaybeType solution(const Assignment &x) const
Test whether x is solution
Definition: linear.cpp:83
Gecode::FloatValArgs a
Coefficients.
Definition: linear.cpp:62
FloatFloat(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
Gecode::FloatRelType frt
Float relation type to propagate.
Definition: linear.cpp:128
Gecode toplevel namespace
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:249
Reification specification.
Definition: int.hh:876
Gecode::FloatNum c
Result.
Definition: linear.cpp:66
virtual MaybeType solution(const Assignment &x) const
Test whether x is solution
Definition: linear.cpp:143
Help class to create and register tests.
Definition: linear.cpp:191
double FloatNum
Floating point number base type.
Definition: float.hh:106
bool one(const Gecode::FloatValArgs &a)
Check whether has only one coefficients.
Definition: linear.cpp:46
@ CPLT_ASSIGNMENT
Definition: float.hh:62
FloatVar(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:131
#define GECODE_NEVER
Assert that this command is never executed.
Definition: macros.hpp:56
struct Gecode::@602::NNF::@65::@67 a
For atomic nodes.
void linear(Home home, const FloatVarArgs &x, FloatRelType frt, FloatVal c)
Post propagator for .
Definition: linear.cpp:41
Float value type.
Definition: float.hh:334
static MaybeType cmp(Gecode::FloatVal x, Gecode::FloatRelType r, Gecode::FloatVal y)
Compare x and y with respect to r.
Definition: float.hpp:235
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)
Post constraint on x.
Definition: linear.cpp:166
Gecode::IntSet d(v, 7)
Gecode::FloatNum step
Step for going to next solution.
Definition: float.hh:251
static std::string str(Gecode::FloatRelType frt)
Map float relation to string.
Definition: float.hpp:194
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x, Gecode::Reify r)
Post reified constraint on x for r.
Definition: linear.cpp:177
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)
Post constraint on x.
Definition: linear.cpp:106
MaybeType
Type for comparisons and solutions.
Definition: float.hh:51
Float variable array.
Definition: float.hh:1030
General test support.
Definition: afc.cpp:39
Base class for assignments
Definition: float.hh:80
Iterator for float relation types.
Definition: float.hh:339
Gecode::FloatValArgs a
Coefficients.
Definition: linear.cpp:126
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x, Gecode::Reify r)
Post reified constraint on x for r.
Definition: linear.cpp:113
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:234
Gecode::IntArgs i({1, 2, 3, 4})
Test linear relation over float variables
Definition: linear.cpp:123
bool testsubsumed
Whether to test for subsumption.
Definition: float.hh:263