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