Generated on for Gecode by doxygen 1.17.0
basic.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.dev>
5 * Vincent Barichard <Vincent.Barichard@univ-angers.fr>
6 *
7 * Copyright:
8 * Christian Schulte, 2005
9 * Vincent Barichard, 2012
10 *
11 * This file is part of Gecode, the generic constraint
12 * development environment:
13 * http://www.gecode.dev
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 "test/float.hh"
37
38#include <gecode/minimodel.hh>
39
40#include <cmath>
41#include <memory>
42
43namespace Test { namespace Float {
44
46 namespace Basic {
47
53
55 class Basic : public Test {
56 public:
59 : Test("Basic",3,-n,n,s,CPLT_ASSIGNMENT,false) {}
60
62 : Test("Basic",3,v,s,CPLT_ASSIGNMENT,false) {}
63
64 virtual MaybeType solution(const Assignment&) const {
65 return MT_TRUE;
66 }
67
69 }
70
72 // Reification for no constraint is not interesting
74 }
75 };
76
77 namespace {
78 Basic b1(3,1.5);
79 Basic b2(Gecode::FloatVal(-2,10),1.5);
80 }
81
83 class Median : public Base {
84 private:
86 static bool valid(const Gecode::FloatVal& x) {
87 const Gecode::FloatNum m = x.med();
88 return (m >= x.min()) && (m <= x.max()) &&
89 (x.tight() || ((m > x.min()) && (m < x.max())));
90 }
91 public:
93 Median(void) : Base("Float::Basic::Median") {}
95 virtual bool run(void) {
96 using namespace Gecode;
98 const FloatNum i = std::numeric_limits<FloatNum>::infinity();
99 const FloatNum d = std::numeric_limits<FloatNum>::denorm_min();
100 const FloatVal negative(-m, -m / 2.0);
101 const FloatVal positive(m / 2.0, m);
102 const FloatVal mixed(-m, m);
103 // The subtraction-based formula rounds this median one ULP upward.
104 const FloatVal closest(3.07187504409887e-53,
105 7.805365587017815e-45);
106 return valid(negative) && valid(positive) && valid(mixed) &&
107 (closest.med() == 3.902682808868283e-45) &&
108 (FloatVal(-i,-1.0).med() == -i) &&
109 (FloatVal(1.0,i).med() == i) &&
110 (FloatVal(-i,i).med() == 0.0) &&
111 (FloatVal(-i,-i).med() == -i) &&
112 (FloatVal(i,i).med() == i) &&
113 !std::signbit(FloatVal(-0.0,0.0).med()) &&
114 (FloatVal(0.0,2.0*d).med() == d) &&
115 (FloatVal(-2.0*d,0.0).med() == -d);
116 }
117 };
118
119 namespace {
120 Median median;
121 }
122
124 template<class Optimize>
125 class OptimizeSpace : public Optimize {
126 public:
130 OptimizeSpace(void) : x(*this,0.0,1.0) {}
132 OptimizeSpace(OptimizeSpace& s) : Optimize(s) {
133 x.update(*this,s.x);
134 }
135
136 virtual Gecode::Space* copy(void) {
137 return new OptimizeSpace(*this);
138 }
139
140 virtual Gecode::FloatVar cost(void) const {
141 return x;
142 }
143 };
144
146 class OptimizeCut : public Base {
147 private:
150 template<class Optimize>
151 static bool cut(Gecode::FloatVal& d) {
152 using namespace Gecode;
154 std::unique_ptr<OptimizeSpace<Optimize>>
155 best(static_cast<OptimizeSpace<Optimize>*>(root.clone()));
156 rel(*best, best->x, FRT_EQ, 0.5);
157 if (best->status() == SS_FAILED)
158 return false;
159 std::unique_ptr<OptimizeSpace<Optimize>>
160 s(static_cast<OptimizeSpace<Optimize>*>(root.clone()));
161 s->constrain(*best);
162 if ((s->status() == SS_FAILED) || s->x.assigned())
163 return false;
164 d = s->x.domain();
165 return true;
166 }
167 public:
169 OptimizeCut(void) : Base("Float::Basic::OptimizeCut") {}
171 virtual bool run(void) {
172 using namespace Gecode;
174 return cut<FloatMinimizeSpace>(min) && cut<FloatMaximizeSpace>(max) &&
175 (min.max() < 0.5) && (max.min() > 0.5);
176 }
177 };
178
179 namespace {
180 OptimizeCut optimize_cut;
181 }
183
184 }
185}}
186
187// STATISTICS: test-float
Float value type.
Definition float.hh:329
FloatNum med(void) const
Return median of float value.
Definition val.hpp:82
friend FloatVal max(const FloatVal &x, const FloatVal &y)
Definition val.hpp:387
friend FloatVal min(const FloatVal &x, const FloatVal &y)
Definition val.hpp:399
bool tight(void) const
Test whether float is tight.
Definition val.hpp:88
Float variable array.
Definition float.hh:1030
Float variables.
Definition float.hh:865
Reification specification.
Definition int.hh:1176
Computation spaces.
Definition core.hpp:1787
Base(std::string s)
Create and register a standard test with name s.
Definition test.cpp:125
Base class for assignments
Definition float.hh:94
Test whether testing infrastructure for float variables works
Definition basic.cpp:55
virtual void post(Gecode::Space &, Gecode::FloatVarArray &, Gecode::Reify)
Post reified constraint on x for r.
Definition basic.cpp:71
virtual void post(Gecode::Space &, Gecode::FloatVarArray &)
Post constraint on x.
Definition basic.cpp:68
Basic(Gecode::FloatNum n, Gecode::FloatNum s)
Initialize test.
Definition basic.cpp:58
Basic(Gecode::FloatVal v, Gecode::FloatNum s)
Initialize test.
Definition basic.cpp:61
virtual MaybeType solution(const Assignment &) const
Check whether x is a solution.
Definition basic.cpp:64
virtual bool run(void)
Perform test.
Definition basic.cpp:95
Median(void)
Create and register test.
Definition basic.cpp:93
virtual bool run(void)
Perform test.
Definition basic.cpp:171
OptimizeCut(void)
Create and register test.
Definition basic.cpp:169
Space minimizing or maximizing a single float variable.
Definition basic.cpp:125
virtual Gecode::Space * copy(void)
Copy during cloning.
Definition basic.cpp:136
Gecode::FloatVar x
The cost variable.
Definition basic.cpp:128
OptimizeSpace(OptimizeSpace &s)
Constructor for cloning s.
Definition basic.cpp:132
virtual Gecode::FloatVar cost(void) const
Return cost.
Definition basic.cpp:140
OptimizeSpace(void)
Create space with cost variable in [0,1].
Definition basic.cpp:130
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVar x1)
Post propagator for .
Definition rel.cpp:68
double FloatNum
Floating point number base type.
Definition float.hh:106
@ FRT_EQ
Equality ( ).
Definition float.hh:1071
@ SS_FAILED
Space is failed
Definition core.hpp:1727
const FloatNum max
Largest allowed float value.
Definition float.hh:839
Gecode toplevel namespace
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Tests for basic setup
Definition basic.cpp:46
Testing domain floats.
Definition float.cpp:43
@ CPLT_ASSIGNMENT
Definition float.hh:76
MaybeType
Type for comparisons and solutions.
Definition float.hh:65
General test support.
Definition afc.cpp:41
#define GECODE_NEVER
Assert that this command is never executed.
Definition macros.hpp:56