this repo has no description
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, 2013
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
34namespace Gecode {
35
36 /**
37 * \brief Class for AFC (accumulated failure count) management
38 *
39 */
40 class AFC {
41 protected:
42 /// Number of views
43 int n;
44 public:
45 /// \name Constructors and initialization
46 //@{
47 /**
48 * \brief Construct as not yet intialized
49 *
50 * The only member functions that can be used on a constructed but not
51 * yet initialized AFC storage is init and the assignment operator.
52 *
53 */
54 AFC(void);
55 /// Copy constructor
56 AFC(const AFC& a);
57 /// Assignment operator
58 AFC& operator =(const AFC& a);
59 /** \brief Initialize for variables \a x and decay factor \a d
60 *
61 * If several AFC objects are created for a space or its clones,
62 * the AFC values are shared between spaces. If the values should
63 * not be shared, \a share should be false.
64 */
65 template<class Var>
66 AFC(Home home, const VarArgArray<Var>& x, double d, bool share=true);
67 /** \brief Initialize for variables \a x and decay factor \a d
68 *
69 * If several AFC objects are created for a space or its clones,
70 * the AFC values are shared between spaces. If the values should
71 * not be shared, \a share should be false.
72 */
73 template<class Var>
74 void init(Home home, const VarArgArray<Var>& x, double d, bool share=true);
75 /// Test whether already initialized
76 operator bool(void) const;
77 /// Default (empty) AFC information
78 GECODE_KERNEL_EXPORT static const AFC def;
79 //@}
80
81 /// Destructor
82 ~AFC(void);
83
84 /// \name Information access
85 //@{
86 /// Return number of AFC values
87 int size(void) const;
88 //@}
89
90 /// \name Decay factor for aging
91 //@{
92 /// Set decay factor to \a d
93 void decay(Space& home, double d);
94 /// Return decay factor
95 double decay(const Space& home) const;
96 //@}
97 };
98
99 /**
100 * \brief Print AFC information (prints nothing)
101 * \relates AFC
102 */
103 template<class Char, class Traits>
104 std::basic_ostream<Char,Traits>&
105 operator <<(std::basic_ostream<Char,Traits>& os,
106 const AFC& a);
107
108 /*
109 * AFC
110 *
111 */
112 forceinline int
113 AFC::size(void) const {
114 assert(n >= 0);
115 return n;
116 }
117
118 forceinline
119 AFC::AFC(void) : n(-1) {}
120
121 forceinline
122 AFC::operator bool(void) const {
123 return n >= 0;
124 }
125
126 template<class Var>
127 forceinline
128 AFC::AFC(Home home, const VarArgArray<Var>& x, double d, bool share)
129 : n(x.size()) {
130 if ((d < 0.0) || (d > 1.0))
131 throw IllegalDecay("AFC");
132 static_cast<Space&>(home).afc_decay(d);
133 if (!share)
134 static_cast<Space&>(home).afc_unshare();
135 }
136 template<class Var>
137 forceinline void
138 AFC::init(Home home, const VarArgArray<Var>& x, double d, bool share) {
139 n = x.size();
140 if ((d < 0.0) || (d > 1.0))
141 throw IllegalDecay("AFC");
142 static_cast<Space&>(home).afc_decay(d);
143 if (!share)
144 static_cast<Space&>(home).afc_unshare();
145 }
146
147
148 forceinline
149 AFC::AFC(const AFC& a)
150 : n(a.n) {}
151 forceinline AFC&
152 AFC::operator =(const AFC& a) {
153 n=a.n;
154 return *this;
155 }
156 forceinline
157 AFC::~AFC(void) {}
158
159 forceinline void
160 AFC::decay(Space& home, double d) {
161 if ((d < 0.0) || (d > 1.0))
162 throw IllegalDecay("AFC");
163 home.afc_decay(d);
164 }
165
166 forceinline double
167 AFC::decay(const Space& home) const {
168 return home.afc_decay();
169 }
170
171
172 template<class Char, class Traits>
173 std::basic_ostream<Char,Traits>&
174 operator <<(std::basic_ostream<Char,Traits>& os,
175 const AFC& a) {
176 (void)a;
177 return os << "AFC(no information available)";
178 }
179
180}
181
182// STATISTICS: kernel-branch