1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  *  Main authors:
4  *     Guido Tack <tack@gecode.org>
5  *
6  *  Copyright:
7  *     Guido Tack, 2005
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 
34 #include "test/set.hh"
35 
36 using namespace Gecode;
37 
38 namespace Test { namespace Set {
39 
40   /// %Tests for convexity constraints
41   namespace Convex {
42 
43     /**
44       * \defgroup TaskTestSetConvex Convexity constraints
45       * \ingroup TaskTestSet
46       */
47     //@{
48 
49     static IntSet ds_33(-4,4);
50 
51     /// %Test for convexity propagator
52     class Convex : public SetTest {
53     public:
54       /// Create and register test
Convex(const char * t)55       Convex(const char* t)
56         : SetTest(t,1,ds_33,false) {}
57       /// %Test whether \a x is solution
solution(const SetAssignment & x) const58       virtual bool solution(const SetAssignment& x) const {
59         CountableSetRanges xr0(x.lub, x[0]);
60         if (!xr0())
61           return true;
62         ++xr0;
63         if (!xr0())
64           return true;
65         return false;
66       }
67       /// Post constraint on \a x
post(Space & home,SetVarArray & x,IntVarArray &)68       virtual void post(Space& home, SetVarArray& x, IntVarArray&) {
69         Gecode::convex(home, x[0]);
70       }
71     };
72     Convex _convex("Convex::Convex");
73 
74     /// %Test for convex hull propagator
75     class ConvexHull : public SetTest {
76     public:
77       /// Create and register test
ConvexHull(const char * t)78       ConvexHull(const char* t)
79         : SetTest(t,2,ds_33,false) {}
80       /// %Test whether \a x is solution
solution(const SetAssignment & x) const81       virtual bool solution(const SetAssignment& x) const {
82         CountableSetRanges xr0(x.lub, x[0]);
83         CountableSetRanges xr1(x.lub, x[1]);
84 
85         if (!xr0())
86           return !xr1();
87 
88         int x0min = xr0.min();
89         int x0max = xr0.max();
90         ++xr0;
91         if (!xr0()) {
92           if (!xr1()) return false;
93           if (x0min != xr1.min()) return false;
94           int x1max = Gecode::Set::Limits::min;
95           while (xr1()) { x1max = xr1.max(); ++xr1;}
96           if (x0max != x1max) return false;
97           return true;
98         }
99         return false;
100       }
101       /// Post constraint on \a x
post(Space & home,SetVarArray & x,IntVarArray &)102       virtual void post(Space& home, SetVarArray& x, IntVarArray&) {
103         Gecode::convex(home, x[1], x[0]);
104       }
105     };
106     ConvexHull _convexhull("Convex::ConvexHull");
107 
108     /// Sharing test for convex hull propagator
109     class ConvexHullS : public SetTest {
110     public:
111       /// Create and register test
ConvexHullS(const char * t)112       ConvexHullS(const char* t)
113         : SetTest(t,1,ds_33,false) {}
114       /// %Test whether \a x is solution
solution(const SetAssignment & x) const115       virtual bool solution(const SetAssignment& x) const {
116         CountableSetRanges xr0(x.lub, x[0]);
117         if (!xr0())
118           return true;
119         ++xr0;
120         if (!xr0())
121           return true;
122         return false;
123       }
124       /// Post constraint on \a x
post(Space & home,SetVarArray & x,IntVarArray &)125       virtual void post(Space& home, SetVarArray& x, IntVarArray&) {
126         Gecode::convex(home, x[0], x[0]);
127       }
128     };
129     ConvexHullS _convexhulls("Convex::Sharing::ConvexHullS");
130 
131     //@}
132 
133 }}}
134 
135 // STATISTICS: test-set
136