Generated on Tue Jan 28 2020 00:00:00 for Gecode by doxygen 1.8.17
branch.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  * Mikael Lagerkvist <lagerkvist@gecode.org>
6  *
7  * Copyright:
8  * Christian Schulte, 2008
9  * Mikael Lagerkvist, 2008
10  *
11  * Last modified:
12  * $Date: 2017-03-01 04:28:36 +0100 (Wed, 01 Mar 2017) $ by $Author: schulte $
13  * $Revision: 15541 $
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 <gecode/kernel.hh>
41 
42 namespace Gecode {
43 
45  class FunctionBranch : public Brancher {
46  protected:
48  class Description : public Choice {
49  public:
51  Description(const Brancher& b, unsigned int a);
53  virtual size_t size(void) const;
55  virtual void archive(Archive& e) const;
56  };
58  SharedData<std::function<void(Space& home)>> f;
60  bool done;
62  FunctionBranch(Home home, std::function<void(Space& home)> f0);
64  FunctionBranch(Space& home, bool share, FunctionBranch& b);
65  public:
67  virtual bool status(const Space& home) const;
69  virtual const Choice* choice(Space& home);
71  virtual const Choice* choice(const Space& home, Archive& a);
73  virtual ExecStatus commit(Space& home, const Choice& ch, unsigned int a);
75  virtual void print(const Space&, const Choice&, unsigned int,
76  std::ostream& o) const;
78  virtual Actor* copy(Space& home, bool share);
80  static void post(Home home, std::function<void(Space& home)> f);
82  virtual size_t dispose(Space& home);
83  };
84 
87  : Choice(b,a) {}
88  size_t
90  return sizeof(Description);
91  }
92  void
94  Choice::archive(e);
95  }
96 
99  std::function<void(Space& home)> f0)
100  : Brancher(home), f(f0), done(false) {
101  if (!f())
102  throw InvalidFunction("FunctionBranch::FunctionBranch");
103  home.notice(*this,AP_DISPOSE);
104  }
107  : Brancher(home,share,b), done(b.done) {
108  f.update(home,share,b.f);
109  }
110  bool
112  return !done;
113  }
114  const Choice*
116  assert(!done);
117  return new Description(*this,1);
118  }
119  const Choice*
121  return new Description(*this,1);
122  }
123  ExecStatus
124  FunctionBranch::commit(Space& home, const Choice&, unsigned int) {
125  done = true;
127  f()(home);
128  return home.failed() ? ES_FAILED : ES_OK;
129  }
130  void
131  FunctionBranch::print(const Space&, const Choice&, unsigned int,
132  std::ostream& o) const {
133  o << "FunctionBranch()";
134  }
135  Actor*
136  FunctionBranch::copy(Space& home, bool share) {
137  return new (home) FunctionBranch(home,share,*this);
138  }
139  forceinline void
140  FunctionBranch::post(Home home, std::function<void(Space& home)> f) {
141  if (!f)
142  throw InvalidFunction("FunctionBranch::post");
143  (void) new (home) FunctionBranch(home,f);
144  }
145  size_t
147  home.ignore(*this,AP_DISPOSE);
148  f.~SharedData<std::function<void(Space& home)>>();
149  (void) Brancher::dispose(home);
150  return sizeof(*this);
151  }
152 
153  void
154  branch(Home home, std::function<void(Space& home)> f) {
155  FunctionBranch::post(home,f);
156  }
157 
158 }
159 
160 // STATISTICS: kernel-branch
virtual void print(const Space &, const Choice &, unsigned int, std::ostream &o) const
Print explanation.
Definition: branch.cpp:131
#define forceinline
Definition: config.hpp:173
virtual Actor * copy(Space &home, bool share)
Copy brancher.
Definition: branch.cpp:136
Class for sharing data between spaces.
Definition: shared-data.hpp:42
Description(const Brancher &b, unsigned int a)
Initialize description for brancher b, number of alternatives a.
Definition: branch.cpp:86
virtual const Choice * choice(Space &home)
Return choice.
Definition: branch.cpp:115
virtual ExecStatus commit(Space &home, const Choice &ch, unsigned int a)
Perform commit.
Definition: branch.cpp:124
Archive representation
Definition: archive.hpp:45
Computation spaces.
Definition: core.hpp:1748
void branch(Home home, const FloatVarArgs &x, FloatVarBranch vars, FloatValBranch vals, FloatBranchFilter bf, FloatVarValPrint vvp)
Branch over x with variable selection vars and value selection vals.
Definition: branch.cpp:43
Base-class for both propagators and branchers.
Definition: core.hpp:696
static void post(Home home, std::function< void(Space &home)> f)
Post brancher.
Definition: branch.cpp:140
virtual size_t dispose(Space &home)
Dispose brancher.
Definition: branch.cpp:146
FunctionBranch(Home home, std::function< void(Space &home)> f0)
Construct brancher.
Definition: branch.cpp:98
#define GECODE_VALID_FUNCTION(f)
Assert that a function is valid.
Definition: macros.hpp:98
Gecode toplevel namespace
Minimal brancher description storing no information.
Definition: branch.cpp:48
Base-class for branchers.
Definition: core.hpp:1446
Home class for posting propagators
Definition: core.hpp:922
struct Gecode::@579::NNF::@61::@63 a
For atomic nodes.
struct Gecode::@579::NNF::@61::@62 b
For binary nodes (and, or, eqv)
@ AP_DISPOSE
Actor must always be disposed.
Definition: core.hpp:630
virtual void archive(Archive &e) const
Archive into e.
Definition: branch.cpp:93
bool failed(void) const
Check whether space is failed.
Definition: core.hpp:4095
SharedData< std::function< void(Space &home)> > f
Function to call.
Definition: branch.cpp:58
Exception: invalid function
Definition: exception.hpp:118
Brancher for calling a function
Definition: branch.cpp:45
void ignore(Actor &a, ActorProperty p, bool duplicate=false)
Ignore actor property.
Definition: core.hpp:4141
virtual size_t dispose(Space &home)
Delete actor and return its size.
Definition: core.hpp:3354
Post propagator for f(x \diamond_{\mathit{op}} y) \sim_r z \f$ void rel(Home home
Choice for performing commit
Definition: core.hpp:1414
void notice(Actor &a, ActorProperty p, bool duplicate=false)
Notice actor property.
Definition: core.hpp:4123
@ ES_FAILED
Execution has resulted in failure.
Definition: core.hpp:542
bool done
Call function just once.
Definition: branch.cpp:60
virtual size_t size(void) const
Report size occupied.
Definition: branch.cpp:89
@ ES_OK
Execution is okay.
Definition: core.hpp:544
virtual void archive(Archive &e) const
Archive into e.
Definition: core.cpp:854
ExecStatus
Definition: core.hpp:540
virtual bool status(const Space &home) const
Check status of brancher, return true if alternatives left.
Definition: branch.cpp:111