Generated on Sat Feb 7 2015 02:01:16 for Gecode by doxygen 1.8.9.1
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  * Last modified:
12  * $Date: 2013-01-29 17:43:05 +0100 (Tue, 29 Jan 2013) $ by $Author: schulte $
13  * $Revision: 13241 $
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 #include "test/float.hh"
41 
42 #include <gecode/minimodel.hh>
43 
44 namespace Test { namespace Float {
45 
47  namespace Linear {
48 
50  bool one(const Gecode::FloatValArgs& a) {
51  for (int i=a.size(); i--; )
52  if (a[i] != 1)
53  return false;
54  return true;
55  }
56 
62  class FloatFloat : public Test {
64  protected:
71  public:
73  FloatFloat(const std::string& s, const Gecode::FloatVal& d,
76  : Test("Linear::Float::"+
77  str(frt0)+"::"+s+"::"+str(c0)+"::"
78  +str(a0.size()),
79  a0.size(),d,st,CPLT_ASSIGNMENT,true),
80  a(a0), frt(frt0), c(c0) {
81  using namespace Gecode;
82  testfix = false;
83  if ((frt == FRT_NQ) || (frt == FRT_LE) || (frt == FRT_GR) || reified)
84  testsubsumed = false;
85  }
87  virtual MaybeType solution(const Assignment& x) const {
88  Gecode::FloatVal e = 0.0;
89  for (int i=x.size(); i--; )
90  e += a[i]*x[i];
91  switch (cmp(e, frt, Gecode::FloatVal(c))) {
92  case MT_FALSE: {
93  Gecode::FloatVal eError = e;
94  for (int i=x.size(); i--; )
95  eError -= a[i]*x[i];
96  if (cmp(e+eError, frt, Gecode::FloatVal(c)) == MT_FALSE)
97  return MT_FALSE;
98  else
99  return MT_MAYBE;
100  }
101  case MT_TRUE:
102  return MT_TRUE;
103  case MT_MAYBE:
104  return MT_MAYBE;
105  }
106  GECODE_NEVER;
107  return MT_FALSE;
108  }
110  virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
111  if (one(a))
112  Gecode::linear(home, x, frt, c);
113  else
114  Gecode::linear(home, a, x, frt, c);
115  }
118  Gecode::Reify r) {
119  if (one(a))
120  Gecode::linear(home, x, frt, c, r);
121  else
122  Gecode::linear(home, a, x, frt, c, r);
123  }
124  };
125 
127  class FloatVar : public Test {
128  protected:
133  public:
135  FloatVar(const std::string& s, const Gecode::FloatVal& d,
137  : Test("Linear::Var::"+
138  str(frt0)+"::"+s+"::"+str(a0.size()),
139  a0.size()+1,d,st,CPLT_ASSIGNMENT,true),
140  a(a0), frt(frt0) {
141  using namespace Gecode;
142  testfix = false;
143  if ((frt == FRT_NQ) || (frt == FRT_LE) || (frt == FRT_GR) || reified)
144  testsubsumed = false;
145  }
147  virtual MaybeType solution(const Assignment& x) const {
148  Gecode::FloatVal e = 0.0;
149  for (int i=a.size(); i--; )
150  e += a[i]*x[i];
151  switch (cmp(e, frt, x[a.size()])) {
152  case MT_FALSE: {
153  Gecode::FloatVal eError = e;
154  for (int i=a.size(); i--; )
155  eError -= a[i]*x[i];
156  if (cmp(e+eError, frt, x[a.size()]) == MT_FALSE)
157  return MT_FALSE;
158  else
159  return MT_MAYBE;
160  }
161  case MT_TRUE:
162  return MT_TRUE;
163  case MT_MAYBE:
164  return MT_MAYBE;
165  }
166  GECODE_NEVER;
167  return MT_FALSE;
168  }
170  virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
171  int n = a.size();
173  for (int i=n; i--; )
174  y[i] = x[i];
175  if (one(a))
176  Gecode::linear(home, y, frt, x[n]);
177  else
178  Gecode::linear(home, a, y, frt, x[n]);
179  }
182  Gecode::Reify r) {
183  int n = a.size();
185  for (int i=n; i--; )
186  y[i] = x[i];
187  if (one(a))
188  Gecode::linear(home, y, frt, x[n], r);
189  else
190  Gecode::linear(home, a, y, frt, x[n], r);
191  }
192  };
193 
195  class Create {
196  public:
198  Create(void) {
199  using namespace Gecode;
200  {
201  FloatNum step = 0.7;
202  FloatVal f1(-2,2);
203  FloatVal f2(-3,-1);
204  FloatVal f3(3,8);
205 
206  FloatValArgs a1(1, 0.0);
207 
208  for (FloatRelTypes frts; frts(); ++frts) {
209  (void) new FloatFloat("11",f1,a1,frts.frt(),0.0,step);
210  (void) new FloatVar("11",f1,a1,frts.frt(),step);
211  (void) new FloatFloat("21",f2,a1,frts.frt(),0.0,step);
212  (void) new FloatVar("21",f2,a1,frts.frt(),step);
213  (void) new FloatFloat("31",f3,a1,frts.frt(),1.0,step);
214  }
215 
216  const FloatVal av2[4] = {1.0,1.0,1.0,1.0};
217  const FloatVal av3[4] = {1.0,-1.0,-1.0,1.0};
218  const FloatVal av4[4] = {2.0,3.0,5.0,7.0};
219  const FloatVal av5[4] = {-2.0,3.0,-5.0,7.0};
220 
221  for (int i=1; i<=4; i++) {
222  FloatValArgs a2(i, av2);
223  FloatValArgs a3(i, av3);
224  FloatValArgs a4(i, av4);
225  FloatValArgs a5(i, av5);
226  for (FloatRelTypes frts; frts(); ++frts) {
227  (void) new FloatFloat("12",f1,a2,frts.frt(),0.0,step);
228  (void) new FloatFloat("13",f1,a3,frts.frt(),0.0,step);
229  (void) new FloatFloat("14",f1,a4,frts.frt(),0.0,step);
230  (void) new FloatFloat("15",f1,a5,frts.frt(),0.0,step);
231  (void) new FloatFloat("22",f2,a2,frts.frt(),0.0,step);
232  (void) new FloatFloat("23",f2,a3,frts.frt(),0.0,step);
233  (void) new FloatFloat("24",f2,a4,frts.frt(),0.0,step);
234  (void) new FloatFloat("25",f2,a5,frts.frt(),0.0,step);
235  (void) new FloatFloat("32",f3,a2,frts.frt(),1.0,step);
236  if (i < 4) {
237  (void) new FloatVar("12",f1,a2,frts.frt(),step);
238  (void) new FloatVar("13",f1,a3,frts.frt(),step);
239  (void) new FloatVar("14",f1,a4,frts.frt(),step);
240  (void) new FloatVar("15",f1,a5,frts.frt(),step);
241  (void) new FloatVar("22",f2,a2,frts.frt(),step);
242  (void) new FloatVar("23",f2,a3,frts.frt(),step);
243  (void) new FloatVar("24",f2,a4,frts.frt(),step);
244  (void) new FloatVar("25",f2,a5,frts.frt(),step);
245  }
246  }
247  }
248  }
249  }
250  };
251 
254 
255  }
256 }}
257 
258 // STATISTICS: test-float
virtual MaybeType solution(const Assignment &x) const
Test whether x is solution
Definition: linear.cpp:147
Passing float arguments.
Definition: float.hh:937
Gecode::FloatNum step
Step for going to next solution.
Definition: float.hh:249
void linear(Home home, const FloatVarArgs &x, FloatRelType frt, FloatNum c)
Post propagator for .
Definition: linear.cpp:45
Test linear relation over float variables
Definition: linear.cpp:127
bool one(const Gecode::FloatValArgs &a)
Check whether has only one coefficients.
Definition: linear.cpp:50
Disequality ( )
Definition: float.hh:1056
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1662
Gecode::FloatValArgs a
Coefficients.
Definition: linear.cpp:66
Passing float variables.
Definition: float.hh:966
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:73
Create(void)
Perform creation and registration.
Definition: linear.cpp:198
bool testfix
Whether to perform fixpoint test.
Definition: float.hh:259
static MaybeType cmp(Gecode::FloatVal x, Gecode::FloatRelType r, Gecode::FloatVal y)
Compare x and y with respect to r.
Definition: float.hpp:239
bool testsubsumed
Whether to test for subsumption.
Definition: float.hh:261
Less ( )
Definition: float.hh:1058
static std::string str(Gecode::FloatRelType frt)
Map float relation to string.
Definition: float.hpp:198
Float variable array.
Definition: float.hh:1016
Computation spaces.
Definition: core.hpp:1362
Gecode::FloatRelType frt
Float relation type to propagate.
Definition: linear.cpp:132
Gecode::FloatNum c
Result.
Definition: linear.cpp:70
Gecode::IntSet d(v, 7)
Iterator for float relation types.
Definition: float.hh:337
Gecode::IntArgs i(4, 1, 2, 3, 4)
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
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:135
NNF * r
Right subtree.
Definition: bool-expr.cpp:246
FloatRelType
Relation types for floats.
Definition: float.hh:1054
unsigned int size(I &i)
Size of all ranges of range iterator i.
Reification specification.
Definition: int.hh:854
Help class to create and register tests.
Definition: linear.cpp:195
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)
Post constraint on x.
Definition: linear.cpp:170
Greater ( )
Definition: float.hh:1060
General test support.
Definition: afc.cpp:43
Float value type.
Definition: float.hh:321
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:253
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x, Gecode::Reify r)
Post reified constraint on x for r.
Definition: linear.cpp:181
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x, Gecode::Reify r)
Post reified constraint on x for r.
Definition: linear.cpp:117
Gecode::FloatValArgs a
Coefficients.
Definition: linear.cpp:130
MaybeType
Type for comparisons and solutions.
Definition: float.hh:55
virtual MaybeType solution(const Assignment &x) const
Test whether x is solution
Definition: linear.cpp:87
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)
Post constraint on x.
Definition: linear.cpp:110
int size(void) const
Return number of variables.
Definition: float.hpp:52
Gecode toplevel namespace
bool reified
Does the constraint also exist as reified constraint.
Definition: float.hh:253
double FloatNum
Floating point number base type.
Definition: float.hh:108
#define GECODE_NEVER
Assert that this command is never executed.
Definition: macros.hpp:60
struct Gecode::@518::NNF::@57::@59 a
For atomic nodes.
Gecode::FloatRelType frt
Float relation type to propagate.
Definition: linear.cpp:68
Base class for assignments
Definition: float.hh:84