Generated on Sat Apr 10 2021 00:00:00 for Gecode by doxygen 1.9.1
exec.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  *
6  * Copyright:
7  * Christian Schulte, 2009
8  *
9  * This file is part of Gecode, the generic constraint
10  * development environment:
11  * http://www.gecode.org
12  *
13  * Permission is hereby granted, free of charge, to any person obtaining
14  * a copy of this software and associated documentation files (the
15  * "Software"), to deal in the Software without restriction, including
16  * without limitation the rights to use, copy, modify, merge, publish,
17  * distribute, sublicense, and/or sell copies of the Software, and to
18  * permit persons to whom the Software is furnished to do so, subject to
19  * the following conditions:
20  *
21  * The above copyright notice and this permission notice shall be
22  * included in all copies or substantial portions of the Software.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31  *
32  */
33 
34 #include "test/int.hh"
35 
36 #include <gecode/minimodel.hh>
37 
38 namespace Test { namespace Int {
39 
41  namespace Exec {
42 
49  class IntWait : public Test {
50  protected:
52  bool sf;
53  public:
55  IntWait(int n, bool sf0)
56  : Test("Wait::Int::"+str(n)+"::"+
57  (sf0 ? "std::function" : "funptr"),n,0,n,false), sf(sf0) {}
59  virtual bool solution(const Assignment& x) const {
60  for (int i=0; i<x.size(); i++)
61  for (int j=i+1; j<x.size(); j++)
62  if (x[i] == x[j])
63  return false;
64  return true;
65  }
67  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
68  using namespace Gecode;
69  auto f = static_cast<std::function<void(Space&)>>
70  ([](Space& home) { c(home); });
71  if (x.size() > 1) {
72  if (sf)
73  Gecode::wait(home, x, f);
74  else
75  Gecode::wait(home, x, &c);
76  } else {
77  if (sf)
78  Gecode::wait(home, x[0], f);
79  else
80  Gecode::wait(home, x[0], &c);
81  }
82  }
84  static void c(Gecode::Space& _home) {
85  TestSpace& home = static_cast<TestSpace&>(_home);
86  for (int i=0; i<home.x.size(); i++)
87  for (int j=i+1; j<home.x.size(); j++)
88  if (home.x[i].val() == home.x[j].val())
89  home.fail();
90  }
91  };
92 
94  class BoolWait : public Test {
95  protected:
97  bool sf;
98  public:
100  BoolWait(int n, bool sf0)
101  : Test("Wait::Bool::"+str(n)+"::"+
102  (sf0 ? "std::function" : "funptr"),n,0,1,false), sf(sf0) {}
104  virtual bool solution(const Assignment& x) const {
105  int t=0;
106  for (int i=0; i<x.size(); i++)
107  t += x[i];
108  return t==2;
109  }
111  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
112  using namespace Gecode;
113  BoolVarArgs b(x.size());
114  for (int i=b.size(); i--; )
115  b[i]=channel(home,x[i]);
116  auto f = static_cast<std::function<void(Space&)>>
117  ([](Space& home) { c(home); });
118  if (b.size() > 1) {
119  if (sf)
120  Gecode::wait(home, b, f);
121  else
122  Gecode::wait(home, b, &c);
123  } else {
124  if (sf)
125  Gecode::wait(home, b[0], f);
126  else
127  Gecode::wait(home, b[0], &c);
128  }
129  }
131  static void c(Gecode::Space& _home) {
132  TestSpace& home = static_cast<TestSpace&>(_home);
133  int t=0;
134  for (int i=0; i<home.x.size(); i++)
135  t += home.x[i].val();
136  if (t!=2)
137  home.fail();
138  }
139  };
140 
142  class When : public Test {
143  protected:
145  bool sf;
146  public:
148  When(bool sf0)
149  : Test(std::string("When::")+
150  (sf0 ? "std::function" : "funptr"),1,0,1,false), sf(sf0) {}
152  virtual bool solution(const Assignment& x) const {
153  return x[0]==0;
154  }
156  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
157  using namespace Gecode;
158  if (sf) {
159  auto sft = static_cast<std::function<void(Space&)>>
160  ([](Space& home) { t(home); });
161  auto sfe = static_cast<std::function<void(Space&)>>
162  ([](Space& home) { e(home); });
163  when(home, channel(home, x[0]), sft, sfe);
164  } else {
165  when(home, channel(home, x[0]), &t, &e);
166  }
167  }
169  static void t(Gecode::Space& home) {
170  home.fail();
171  }
173  static void e(Gecode::Space& home) {
174  (void) home;
175  }
176  };
177 
178  IntWait iw1t(1,true), iw2t(2,true), iw3t(3,true), iw4t(4,true);
179  IntWait iw1f(1,false), iw2f(2,false), iw3f(3,false), iw4f(4,false);
180  BoolWait bw1t(1,true), bw2t(2,true), bw3t(3,true), bw4t(4,true);
181  BoolWait bw1f(1,false), bw2f(2,false), bw3f(3,false), bw4f(4,false);
182 
183 
184  When whent(true);
185  When whenf(false);
187 
188  }
189 
190 }}
191 
192 // STATISTICS: test-int
struct Gecode::@602::NNF::@65::@66 b
For binary nodes (and, or, eqv)
NodeType t
Type of node.
Definition: bool-expr.cpp:230
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:234
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:249
Passing Boolean variables.
Definition: int.hh:712
Integer variable array.
Definition: int.hh:763
Computation spaces.
Definition: core.hpp:1742
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:926
Base class for assignments
Definition: int.hh:59
Simple test for wait (Boolean variables)
Definition: exec.cpp:94
bool sf
Whether to use std::function.
Definition: exec.cpp:97
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post wait on x.
Definition: exec.cpp:111
virtual bool solution(const Assignment &x) const
Check whether x is solution.
Definition: exec.cpp:104
static void c(Gecode::Space &_home)
Continuation to be executed.
Definition: exec.cpp:131
BoolWait(int n, bool sf0)
Create and register test.
Definition: exec.cpp:100
Simple test for wait (integer variables)
Definition: exec.cpp:49
IntWait(int n, bool sf0)
Create and register test.
Definition: exec.cpp:55
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post wait on x.
Definition: exec.cpp:67
bool sf
Whether to use std::function.
Definition: exec.cpp:52
virtual bool solution(const Assignment &x) const
Check whether x is solution.
Definition: exec.cpp:59
static void c(Gecode::Space &_home)
Continuation to be executed.
Definition: exec.cpp:84
Simple test for when.
Definition: exec.cpp:142
static void t(Gecode::Space &home)
Then-function to be executed.
Definition: exec.cpp:169
bool sf
Whether to use std::function.
Definition: exec.cpp:145
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post when on x.
Definition: exec.cpp:156
virtual bool solution(const Assignment &x) const
Check whether x is solution.
Definition: exec.cpp:152
When(bool sf0)
Create and register test.
Definition: exec.cpp:148
static void e(Gecode::Space &home)
Else-function to be executed.
Definition: exec.cpp:173
Space for executing tests.
Definition: int.hh:149
Gecode::IntVarArray x
Variables to be tested.
Definition: int.hh:154
static std::string str(Gecode::IntPropLevel ipl)
Map integer propagation level to string.
Definition: int.hpp:209
void channel(Home home, FloatVar x0, IntVar x1)
Post propagator for channeling a float and an integer variable .
Definition: channel.cpp:41
void wait(Home home, FloatVar x, std::function< void(Space &home)> c)
Execute c when x becomes assigned.
Definition: exec.cpp:39
void when(Home home, BoolVar x, std::function< void(Space &home)> t, std::function< void(Space &home)> e, IntPropLevel)
Execute t (then) when x is assigned one, and e (else) otherwise.
Definition: exec.cpp:70
void fail(void)
Fail space.
Definition: core.hpp:4030
Gecode::IntArgs i({1, 2, 3, 4})
BoolWait bw2f(2, false)
BoolWait bw2t(2, true)
IntWait iw3f(3, false)
IntWait iw2f(2, false)
BoolWait bw1f(1, false)
IntWait iw1f(1, false)
IntWait iw1t(1, true)
IntWait iw4f(4, false)
BoolWait bw4f(4, false)
IntWait iw3t(3, true)
When whenf(false)
IntWait iw4t(4, true)
IntWait iw2t(2, true)
BoolWait bw3t(3, true)
BoolWait bw1t(1, true)
When whent(true)
BoolWait bw3f(3, false)
BoolWait bw4t(4, true)
General test support.
Definition: afc.cpp:39
Definition: flatzinc.cpp:52