Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
12 kB · 364 lines
C++
at develop
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- *//* * Main authors: * Christian Schulte <schulte@gecode.org> * * Copyright: * Christian Schulte, 2008 * * This file is part of Gecode, the generic constraint * development environment: * http://www.gecode.org * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * */
#include "test/int.hh"
#include <gecode/minimodel.hh>
namespace Test { namespace Int {
/// %Tests for minimal modelling constraints (arithmetic) namespace MiniModelArithmetic {
/** * \defgroup TaskTestIntMiniModelArithmetic Minimal modelling constraints (arithmetic) * \ingroup TaskTestInt */ //@{ /// %Test for multiplication constraint class Mult : public Test { public: /// Create and register test Mult(const std::string& s, const Gecode::IntSet& d) : Test("MiniModel::Mult::"+s,3,d) { testfix = false; } /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { double d0 = static_cast<double>(x[0]); double d1 = static_cast<double>(x[1]); double d2 = static_cast<double>(x[2]); return d0*d1 == d2; } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { using namespace Gecode; rel(home, expr(home, x[0] * x[1]), IRT_EQ, x[2], IPL_DOM); } };
/// %Test for division constraint class Div : public Test { public: /// Create and register test Div(const std::string& s, const Gecode::IntSet& d) : Test("MiniModel::Div::"+s,3,d) { testfix = false; } /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { return (x[1] != 0) && (x[0] / x[1] == x[2]); } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { using namespace Gecode; rel(home, expr(home, x[0] / x[1]), IRT_EQ, x[2], IPL_DOM); } };
/// %Test for division constraint class Mod : public Test { public: /// Create and register test Mod(const std::string& s, const Gecode::IntSet& d) : Test("MiniModel::Mod::"+s,3,d) { testfix = false; } /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { return (x[1] != 0) && (x[0] % x[1] == x[2]); } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { using namespace Gecode; rel(home, expr(home, x[0] % x[1]), IRT_EQ, x[2], IPL_DOM); } };
/// %Test for addition constraint class Plus : public Test { public: /// Create and register test Plus(const std::string& s, const Gecode::IntSet& d) : Test("MiniModel::Plus::"+s,3,d) { testfix = false; } /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { double d0 = static_cast<double>(x[0]); double d1 = static_cast<double>(x[1]); double d2 = static_cast<double>(x[2]); return ((d0+d1 >= Gecode::Int::Limits::min) && (d0+d1 <= Gecode::Int::Limits::max) && (d0+d1 == d2)); } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { using namespace Gecode; rel(home, expr(home, x[0] + x[1]), IRT_EQ, x[2], IPL_DOM); } };
/// %Test for subtraction constraint class Minus : public Test { public: /// Create and register test Minus(const std::string& s, const Gecode::IntSet& d) : Test("MiniModel::Minus::"+s,3,d) { testfix = false; } /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { double d0 = static_cast<double>(x[0]); double d1 = static_cast<double>(x[1]); double d2 = static_cast<double>(x[2]); return ((d0-d1 >= Gecode::Int::Limits::min) && (d0-d1 <= Gecode::Int::Limits::max) && (d0-d1 == d2)); } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { using namespace Gecode; rel(home, expr(home, x[0] - x[1]), IRT_EQ, x[2], IPL_DOM); } };
/// %Test for sqr constraint class Sqr : public Test { public: /// Create and register test Sqr(const std::string& s, const Gecode::IntSet& d) : Test("MiniModel::Sqr::"+s,2,d) { testfix = false; } /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { double d0 = static_cast<double>(x[0]); double d1 = static_cast<double>(x[1]); return d0*d0 == d1; } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { using namespace Gecode; rel(home, expr(home, sqr(x[0])), IRT_EQ, x[1], IPL_DOM); } };
/// %Test for sqrt constraint class Sqrt : public Test { public: /// Create and register test Sqrt(const std::string& s, const Gecode::IntSet& d) : Test("MiniModel::Sqrt::"+s,2,d) { testfix = false; } /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { double d0 = static_cast<double>(x[0]); double d1 = static_cast<double>(x[1]); return (d0 >= 0) && (d0 >= d1*d1) && (d0 < (d1+1)*(d1+1)); } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { using namespace Gecode; rel(home, expr(home, sqrt(x[0])), IRT_EQ, x[1], IPL_DOM); } };
/// %Test for absolute value constraint class Abs : public Test { public: /// Create and register test Abs(const std::string& s, const Gecode::IntSet& d, Gecode::IntPropLevel ipl) : Test("MiniModel::Abs::"+str(ipl)+"::"+s, 2,d,false,ipl) { testfix = false; } /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { double d0 = static_cast<double>(x[0]); double d1 = static_cast<double>(x[1]); return (d0<0.0 ? -d0 : d0) == d1; } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { using namespace Gecode; rel(home, expr(home, abs(x[0]), ipl), IRT_EQ, x[1], IPL_DOM); } };
/// %Test for binary minimum constraint class Min : public Test { public: /// Create and register test Min(const std::string& s, const Gecode::IntSet& d) : Test("MiniModel::Min::Bin::"+s,3,d) { testfix = false; } /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { return std::min(x[0],x[1]) == x[2]; } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { using namespace Gecode; rel(home, expr(home, min(x[0], x[1])), IRT_EQ, x[2], IPL_DOM); } };
/// %Test for binary maximum constraint class Max : public Test { public: /// Create and register test Max(const std::string& s, const Gecode::IntSet& d) : Test("MiniModel::Max::Bin::"+s,3,d) { testfix = false; } /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { return std::max(x[0],x[1]) == x[2]; } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { using namespace Gecode; rel(home, expr(home, max(x[0], x[1])), IRT_EQ, x[2], IPL_DOM); } };
/// %Test for n-ary minimmum constraint class MinNary : public Test { public: /// Create and register test MinNary(void) : Test("MiniModel::Min::Nary",4,-4,4) { testfix = false; } /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { return std::min(std::min(x[0],x[1]), x[2]) == x[3]; } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { using namespace Gecode; IntVarArgs m(3); m[0]=x[0]; m[1]=x[1]; m[2]=x[2]; rel(home, expr(home, min(m)), IRT_EQ, x[3], IPL_DOM); } };
/// %Test for n-ary maximum constraint class MaxNary : public Test { public: /// Create and register test MaxNary(void) : Test("MiniModel::Max::Nary",4,-4,4) { testfix = false; } /// %Test whether \a x is solution virtual bool solution(const Assignment& x) const { return std::max(std::max(x[0],x[1]), x[2]) == x[3]; } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { using namespace Gecode; IntVarArgs m(3); m[0]=x[0]; m[1]=x[1]; m[2]=x[2]; rel(home, expr(home, max(m)), IRT_EQ, x[3], IPL_DOM); } };
const int v1[7] = { Gecode::Int::Limits::min, Gecode::Int::Limits::min+1, -1,0,1, Gecode::Int::Limits::max-1, Gecode::Int::Limits::max }; const int v2[9] = { static_cast<int>(-sqrt(static_cast<double>(-Gecode::Int::Limits::min))), -4,-2,-1,0,1,2,4, static_cast<int>(sqrt(static_cast<double>(Gecode::Int::Limits::max))) };
Gecode::IntSet d1(v1,7); Gecode::IntSet d2(v2,9); Gecode::IntSet d3(-8,8);
Mult mult_max("A",d1); Mult mult_med("B",d2); Mult mult_min("C",d3);
Div div_max("A",d1); Div div_med("B",d2); Div div_min("C",d3);
Mod mod_max("A",d1); Mod mod_med("B",d2); Mod mod_min("C",d3);
Plus plus_max("A",d1); Plus plus_med("B",d2); Plus plus_min("C",d3);
Minus minus_max("A",d1); Minus minus_med("B",d2); Minus minus_min("C",d3);
Sqr sqr_max("A",d1); Sqr sqr_med("B",d2); Sqr sqr_min("C",d3);
Sqrt sqrt_max("A",d1); Sqrt sqrt_med("B",d2); Sqrt sqrt_min("C",d3);
Abs abs_bnd_max("A",d1,Gecode::IPL_BND); Abs abs_bnd_med("B",d2,Gecode::IPL_BND); Abs abs_bnd_min("C",d3,Gecode::IPL_BND); Abs abs_dom_max("A",d1,Gecode::IPL_DOM); Abs abs_dom_med("B",d2,Gecode::IPL_DOM); Abs abs_dom_min("C",d3,Gecode::IPL_DOM);
Min min_max("A",d1); Min min_med("B",d2); Min min_min("C",d3);
Max max_max("A",d1); Max max_med("B",d2); Max max_min("C",d3);
MinNary min_nary; MaxNary max_nary;
//@} }
}}
// STATISTICS: test-minimodel