main page
modules
namespaces
classes
files
Gecode home
Generated on Tue Mar 24 2020 14:04:04 for Gecode by
doxygen
1.8.17
gecode
kernel
branch
function.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
* 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 <
gecode/kernel.hh
>
37
38
namespace
Gecode
{
namespace
Kernel {
39
41
class
FunctionBranch
:
public
Brancher
{
42
protected
:
44
class
Description
:
public
Choice
{
45
public
:
47
Description
(
const
Brancher
&
b
,
unsigned
int
a
);
49
virtual
void
archive
(
Archive
& e)
const
;
50
};
52
SharedData
<std::function<void(
Space
& home)>>
f
;
54
bool
done
;
56
FunctionBranch
(
Home
home, std::function<
void
(
Space
& home)> f0);
58
FunctionBranch
(
Space
& home,
FunctionBranch
&
b
);
59
public
:
61
virtual
bool
status
(
const
Space
& home)
const
;
63
virtual
const
Choice
*
choice
(
Space
& home);
65
virtual
const
Choice
*
choice
(
const
Space
& home,
Archive
&
a
);
67
virtual
ExecStatus
commit
(
Space
& home,
const
Choice
& ch,
unsigned
int
a
);
69
virtual
void
print
(
const
Space
&,
const
Choice
&,
unsigned
int
,
70
std::ostream& o)
const
;
72
virtual
Actor
*
copy
(
Space
& home);
74
static
void
post
(
Home
home, std::function<
void
(
Space
& home)>
f
);
76
virtual
size_t
dispose
(
Space
& home);
77
};
78
79
forceinline
80
FunctionBranch::Description::Description
(
const
Brancher
&
b
,
unsigned
int
a
)
81
:
Choice
(
b
,
a
) {}
82
void
83
FunctionBranch::Description::archive
(
Archive
& e)
const
{
84
Choice::archive
(e);
85
}
86
87
forceinline
88
FunctionBranch::FunctionBranch
(
Home
home,
89
std::function<
void
(
Space
& home)> f0)
90
:
Brancher
(home),
f
(f0),
done
(false) {
91
if
(!
f
())
92
throw
InvalidFunction
(
"FunctionBranch::FunctionBranch"
);
93
home.
notice
(*
this
,
AP_DISPOSE
);
94
}
95
forceinline
96
FunctionBranch::FunctionBranch
(
Space
& home,
FunctionBranch
&
b
)
97
:
Brancher
(home,
b
),
f
(
b
.
f
), done(
b
.done) {
98
}
99
bool
100
FunctionBranch::status
(
const
Space
&)
const
{
101
return
!
done
;
102
}
103
const
Choice
*
104
FunctionBranch::choice
(
Space
&) {
105
assert(!
done
);
106
return
new
Description
(*
this
,1);
107
}
108
const
Choice
*
109
FunctionBranch::choice
(
const
Space
&,
Archive
&) {
110
return
new
Description
(*
this
,1);
111
}
112
ExecStatus
113
FunctionBranch::commit
(
Space
& home,
const
Choice
&,
unsigned
int
) {
114
done
=
true
;
115
GECODE_VALID_FUNCTION
(
f
());
116
f
()(home);
117
return
home.
failed
() ?
ES_FAILED
:
ES_OK
;
118
}
119
void
120
FunctionBranch::print
(
const
Space
&,
const
Choice
&,
unsigned
int
,
121
std::ostream& o)
const
{
122
o <<
"FunctionBranch()"
;
123
}
124
Actor
*
125
FunctionBranch::copy
(
Space
& home) {
126
return
new
(home)
FunctionBranch
(home,*
this
);
127
}
128
forceinline
void
129
FunctionBranch::post
(
Home
home, std::function<
void
(
Space
& home)>
f
) {
130
if
(!
f
)
131
throw
InvalidFunction
(
"FunctionBranch::post"
);
132
(void)
new
(home)
FunctionBranch
(home,
f
);
133
}
134
size_t
135
FunctionBranch::dispose
(
Space
& home) {
136
home.
ignore
(*
this
,
AP_DISPOSE
);
137
f
.~SharedData<std::function<void(
Space
& home)>>();
138
(void)
Brancher::dispose
(home);
139
return
sizeof
(*this);
140
}
141
142
}}
143
144
namespace
Gecode
{
145
146
void
147
branch
(
Home
home, std::function<
void
(
Space
& home)>
f
) {
148
Kernel::FunctionBranch::post
(home,
f
);
149
}
150
151
}
152
153
// STATISTICS: kernel-branch
kernel.hh
Gecode::Kernel::FunctionBranch::choice
virtual const Choice * choice(Space &home)
Return choice.
Definition:
function.cpp:104
Gecode::SharedData
Class for sharing data between spaces.
Definition:
shared-data.hpp:38
Gecode::Archive
Archive representation
Definition:
archive.hpp:42
Gecode::Kernel::FunctionBranch::Description::Description
Description(const Brancher &b, unsigned int a)
Initialize description for brancher b, number of alternatives a.
Definition:
function.cpp:80
Gecode::Space
Computation spaces.
Definition:
core.hpp:1742
Gecode::branch
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:39
Gecode::Actor
Base-class for both propagators and branchers.
Definition:
core.hpp:628
Gecode::Kernel::FunctionBranch::FunctionBranch
FunctionBranch(Home home, std::function< void(Space &home)> f0)
Construct brancher.
Definition:
function.cpp:88
Gecode::Kernel::FunctionBranch::copy
virtual Actor * copy(Space &home)
Copy brancher.
Definition:
function.cpp:125
GECODE_VALID_FUNCTION
#define GECODE_VALID_FUNCTION(f)
Assert that a function is valid.
Definition:
macros.hpp:94
Gecode::Kernel::FunctionBranch::dispose
virtual size_t dispose(Space &home)
Dispose brancher.
Definition:
function.cpp:135
Gecode::Kernel::FunctionBranch::commit
virtual ExecStatus commit(Space &home, const Choice &ch, unsigned int a)
Perform commit.
Definition:
function.cpp:113
Gecode
Gecode toplevel namespace
Gecode::Kernel::FunctionBranch::done
bool done
Call function just once.
Definition:
function.cpp:54
Gecode::Brancher
Base-class for branchers.
Definition:
core.hpp:1442
Gecode::Kernel::FunctionBranch::f
SharedData< std::function< void(Space &home)> > f
Function to call.
Definition:
function.cpp:52
Gecode::Home
Home class for posting propagators
Definition:
core.hpp:856
Gecode::AP_DISPOSE
@ AP_DISPOSE
Actor must always be disposed.
Definition:
core.hpp:562
Gecode::Kernel::FunctionBranch::status
virtual bool status(const Space &home) const
Check status of brancher, return true if alternatives left.
Definition:
function.cpp:100
b
struct Gecode::@602::NNF::@65::@66 b
For binary nodes (and, or, eqv)
a
struct Gecode::@602::NNF::@65::@67 a
For atomic nodes.
Gecode::Kernel::FunctionBranch::Description
Minimal brancher description storing no information.
Definition:
function.cpp:44
Gecode::Kernel::FunctionBranch::post
static void post(Home home, std::function< void(Space &home)> f)
Post brancher.
Definition:
function.cpp:129
Gecode::Space::failed
bool failed(void) const
Check whether space is failed.
Definition:
core.hpp:4044
Gecode::InvalidFunction
Exception: invalid function
Definition:
exception.hpp:114
Gecode::Kernel::FunctionBranch
Brancher for calling a function
Definition:
function.cpp:41
Gecode::Kernel::FunctionBranch::print
virtual void print(const Space &, const Choice &, unsigned int, std::ostream &o) const
Print explanation.
Definition:
function.cpp:120
forceinline
#define forceinline
Definition:
config.hpp:185
Gecode::Space::ignore
void ignore(Actor &a, ActorProperty p, bool duplicate=false)
Ignore actor property.
Definition:
core.hpp:4074
Gecode::Actor::dispose
virtual size_t dispose(Space &home)
Delete actor and return its size.
Definition:
core.hpp:3252
Gecode::f
Post propagator for f(x \diamond_{\mathit{op}} y) \sim_r z \f$ void rel(Home home
Gecode::Kernel::FunctionBranch::Description::archive
virtual void archive(Archive &e) const
Archive into e.
Definition:
function.cpp:83
Gecode::Choice
Choice for performing commit
Definition:
core.hpp:1412
Gecode::Space::notice
void notice(Actor &a, ActorProperty p, bool duplicate=false)
Notice actor property.
Definition:
core.hpp:4059
Gecode::ES_FAILED
@ ES_FAILED
Execution has resulted in failure.
Definition:
core.hpp:474
Gecode::ES_OK
@ ES_OK
Execution is okay.
Definition:
core.hpp:476
Gecode::Choice::archive
virtual void archive(Archive &e) const
Archive into e.
Definition:
core.cpp:891
Gecode::ExecStatus
ExecStatus
Definition:
core.hpp:472