Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
21 kB · 585 lines
C++
at develop
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- *//* * Main authors: * Christian Schulte <schulte@gecode.org> * * Copyright: * Christian Schulte, 2009 * * 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 cumulative scheduling constraints namespace Cumulative {
/** * \defgroup TaskTestIntCumulative Cumulative scheduling constraints * \ingroup TaskTestInt */ //@{ /// Test for cumulative constraint with mandatory tasks class ManFixPCumulative : public Test { protected: /// Capacity of resource int c; /// The processing times Gecode::IntArgs p; /// The resource usage Gecode::IntArgs u; /// Get a reasonable maximal start time static int st(int c, const Gecode::IntArgs& p, const Gecode::IntArgs& u) { double e = 0; for (int i=p.size(); i--; ) e += static_cast<double>(p[i])*u[i]; return e / std::max(1,std::abs(c)); } /// Offset int o; public: /// Create and register test ManFixPCumulative(int c0, const Gecode::IntArgs& p0, const Gecode::IntArgs& u0, int o0, Gecode::IntPropLevel ipl0) : Test("Cumulative::Man::Fix::"+str(o0)+"::"+ str(c0)+"::"+str(p0)+"::"+str(u0)+"::"+str(ipl0), (c0 >= 0) ? p0.size():p0.size()+1,0,st(c0,p0,u0),false,ipl0), c(c0), p(p0), u(u0), o(o0) { testsearch = false; testfix = false; contest = CTL_NONE; } /// Create and register initial assignment virtual Assignment* assignment(void) const { return new RandomAssignment(arity,dom,500); } /// Test whether \a x is solution virtual bool solution(const Assignment& x) const { int cmax = (c >= 0) ? c : x[x.size()-1]; int n = (c >= 0) ? x.size() : x.size()-1;
if (c < 0 && x[n] > -c) return false;
// Compute maximal time int t = 0; for (int i=0; i<n; i++) t = std::max(t,x[i]+std::max(1,p[i])); // Compute resource usage (including at start times) int* used = new int[t]; for (int i=0; i<t; i++) used[i] = 0; for (int i=0; i<n; i++) for (int t=0; t<p[i]; t++) used[x[i]+t] += u[i]; // Check resource usage for (int i=0; i<t; i++) if (used[i] > cmax) { delete [] used; return false; } // Compute resource usage (only internal) for (int i=0; i<t; i++) used[i] = 0; for (int i=0; i<n; i++) { for (int t=1; t<p[i]; t++) { used[x[i]+t] += u[i]; } } // Check resource usage at start times for (int i=0; i<n; i++) if (used[x[i]]+u[i] > cmax) { delete [] used; return false; } delete [] used; return true; } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { int n = (c >= 0) ? x.size() : x.size()-1; Gecode::IntVarArgs xx; if (o==0) { xx=x.slice(0,1,n); } else { xx=Gecode::IntVarArgs(n); for (int i=n; i--;) xx[i]=Gecode::expr(home,x[i]+o,Gecode::IPL_DOM); } if (c >= 0) { Gecode::cumulative(home, c, xx, p, u, ipl); } else { Gecode::rel(home, x[n] <= -c); Gecode::cumulative(home, x[n], xx, p, u, ipl); } } };
/// Test for cumulative constraint with optional tasks class OptFixPCumulative : public Test { protected: /// Capacity of resource int c; /// The processing times Gecode::IntArgs p; /// The resource usage Gecode::IntArgs u; /// Limit for optional tasks int l; /// Offset int o; /// Get a reasonable maximal start time static int st(int c, const Gecode::IntArgs& p, const Gecode::IntArgs& u) { double e = 0; for (int i=p.size(); i--; ) e += static_cast<double>(p[i])*u[i]; return e / std::max(1,std::abs(c)); } public: /// Create and register test OptFixPCumulative(int c0, const Gecode::IntArgs& p0, const Gecode::IntArgs& u0, int o0, Gecode::IntPropLevel ipl0) : Test("Cumulative::Opt::Fix::"+str(o0)+"::"+ str(c0)+"::"+str(p0)+"::"+str(u0)+"::"+str(ipl0), (c0 >= 0) ? 2*p0.size() : 2*p0.size()+1,0,st(c0,p0,u0), false,ipl0), c(c0), p(p0), u(u0), l(st(c,p,u)/2), o(o0) { testsearch = false; testfix = false; contest = CTL_NONE; } /// Create and register initial assignment virtual Assignment* assignment(void) const { return new RandomAssignment(arity,dom,500); } /// Test whether \a x is solution virtual bool solution(const Assignment& x) const { int nn = (c >= 0) ? x.size() : x.size()-1; int cmax = (c >= 0) ? c : x[nn];
if (c < 0 && x[nn] > -c) return false;
int n = nn / 2; // Compute maximal time int t = 0; for (int i=0; i<n; i++) t = std::max(t,x[i]+std::max(1,p[i])); // Compute resource usage (including at start times) int* used = new int[t]; for (int i=0; i<t; i++) used[i] = 0; for (int i=0; i<n; i++) if (x[n+i] > l) for (int t=0; t<p[i]; t++) used[x[i]+t] += u[i]; // Check resource usage for (int i=0; i<t; i++) { if (used[i] > cmax) { delete [] used; return false; } } // Compute resource usage (only internal) for (int i=0; i<t; i++) used[i] = 0; for (int i=0; i<n; i++) if (x[n+i] > l) { for (int t=1; t<p[i]; t++) used[x[i]+t] += u[i]; } // Check resource usage at start times for (int i=0; i<n; i++) if (x[n+i] > l) if (used[x[i]]+u[i] > cmax) { delete [] used; return false; } delete [] used; return true; } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { int nn=(c >= 0) ? x.size() : x.size()-1; int n=nn / 2; Gecode::IntVarArgs s(n); Gecode::BoolVarArgs m(n);
for (int i=0; i<n; i++) { s[i]=(c >= 0) ? x[i] : Gecode::expr(home,x[i]+o,Gecode::IPL_DOM); m[i]=Gecode::expr(home, x[n+i] > l); }
if (c >= 0) { Gecode::cumulative(home, c, s, p, u, m, ipl); } else { Gecode::rel(home, x[nn] <= -c); Gecode::cumulative(home, x[nn], s, p, u, m, ipl); } } };
/// Test for cumulative constraint with flexible mandatory tasks class ManFlexCumulative : public Test { protected: /// Capacity of resource int c; /// Minimum processing time int _minP; /// Maximum processing time int _maxP; /// The resource usage Gecode::IntArgs u; /// Get a reasonable maximal start time static int st(int c, int maxP, const Gecode::IntArgs& u) { double e = 0; for (int i=u.size(); i--; ) e += static_cast<double>(maxP)*u[i]; return e / std::max(1,std::abs(c)); } /// Offset int o; public: /// Create and register test ManFlexCumulative(int c0, int minP, int maxP, const Gecode::IntArgs& u0, int o0, Gecode::IntPropLevel ipl0) : Test("Cumulative::Man::Flex::"+str(o0)+"::"+ str(c0)+"::"+str(minP)+"::"+str(maxP)+"::"+str(u0)+ "::"+str(ipl0), (c0 >= 0) ? 2*u0.size() : 2*u0.size()+1, 0,std::max(maxP,st(c0,maxP,u0)),false,ipl0), c(c0), _minP(minP), _maxP(maxP), u(u0), o(o0) { testsearch = false; testfix = false; contest = CTL_NONE; } /// Create and register initial assignment virtual Assignment* assignment(void) const { return new RandomMixAssignment((c >= 0) ? arity/2 : arity/2+1, dom,arity/2, Gecode::IntSet(_minP,_maxP),500); } /// Test whether \a x is solution virtual bool solution(const Assignment& x) const { int nn = (c >= 0) ? x.size() : x.size()-1; int n = nn/2; int cmax = (c >= 0) ? c : x[n]; int pstart = (c >= 0) ? n : n+1;
if (c < 0 && cmax > -c) return false;
// Compute maximal time int t = 0; for (int i=0; i<n; i++) { t = std::max(t,x[i]+std::max(1,x[pstart+i])); } // Compute resource usage (including at start times) int* used = new int[t]; for (int i=0; i<t; i++) used[i] = 0; for (int i=0; i<n; i++) for (int t=0; t<x[pstart+i]; t++) used[x[i]+t] += u[i]; // Check resource usage for (int i=0; i<t; i++) if (used[i] > cmax) { delete [] used; return false; } // Compute resource usage (only internal) for (int i=0; i<t; i++) used[i] = 0; for (int i=0; i<n; i++) { for (int t=1; t<x[pstart+i]; t++) used[x[i]+t] += u[i]; } // Check resource usage at start times for (int i=0; i<n; i++) if (used[x[i]]+u[i] > cmax) { delete [] used; return false; } delete [] used; return true; } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { int nn = (c >= 0) ? x.size() : x.size()-1; int n = nn/2; int pstart = (c >= 0) ? n : n+1; Gecode::IntVarArgs s(n); Gecode::IntVarArgs px(x.slice(pstart,1,n)); Gecode::IntVarArgs e(home,n, Gecode::Int::Limits::min, Gecode::Int::Limits::max); for (int i=s.size(); i--;) { s[i] = expr(home, o+x[i], Gecode::IPL_DOM); rel(home, s[i]+px[i] == e[i]); rel(home, _minP <= px[i]); rel(home, _maxP >= px[i]); } if (c >= 0) { Gecode::cumulative(home, c, s, px, e, u, ipl); } else { rel(home, x[n] <= -c); Gecode::cumulative(home, x[n], s, px, e, u, ipl); } } };
/// Test for cumulative constraint with optional flexible tasks class OptFlexCumulative : public Test { protected: /// Capacity of resource int c; /// Minimum processing time int _minP; /// Maximum processing time int _maxP; /// The resource usage Gecode::IntArgs u; /// Limit for optional tasks int l; /// Offset int o; /// Get a reasonable maximal start time static int st(int c, int maxP, const Gecode::IntArgs& u) { double e = 0; for (int i=u.size(); i--; ) e += static_cast<double>(maxP)*u[i]; return e / std::max(1,std::abs(c)); } public: /// Create and register test OptFlexCumulative(int c0, int minP, int maxP, const Gecode::IntArgs& u0, int o0, Gecode::IntPropLevel ipl0) : Test("Cumulative::Opt::Flex::"+str(o0)+"::"+ str(c0)+"::"+str(minP)+"::"+str(maxP)+"::"+str(u0)+ "::"+str(ipl0), (c0 >= 0) ? 3*u0.size() : 3*u0.size()+1, 0,std::max(maxP,st(c0,maxP,u0)), false,ipl0), c(c0), _minP(minP), _maxP(maxP), u(u0), l(std::max(maxP,st(c0,maxP,u0))/2), o(o0) { testsearch = false; testfix = false; contest = CTL_NONE; } /// Create and register initial assignment virtual Assignment* assignment(void) const { return new RandomMixAssignment((c >= 0) ? 2*(arity/3) : 2*(arity/3)+1, dom,arity/3, Gecode::IntSet(_minP,_maxP),500); } /// Test whether \a x is solution virtual bool solution(const Assignment& x) const { int nn = (c >= 0) ? x.size() : x.size()-1; int n = nn / 3; int cmax = (c >= 0) ? c : x[2*n]; int pstart = (c >= 0) ? 2*n : 2*n+1;
if (c < 0 && cmax > -c) return false;
// Compute maximal time int t = 0; for (int i=0; i<n; i++) t = std::max(t,x[i]+std::max(1,x[pstart+i])); // Compute resource usage (including at start times) int* used = new int[t]; for (int i=0; i<t; i++) used[i] = 0; for (int i=0; i<n; i++) if (x[n+i] > l) for (int t=0; t<x[pstart+i]; t++) used[x[i]+t] += u[i]; // Check resource usage for (int i=0; i<t; i++) if (used[i] > cmax) { delete [] used; return false; } // Compute resource usage (only internal) for (int i=0; i<t; i++) used[i] = 0; for (int i=0; i<n; i++) if (x[n+i] > l) for (int t=1; t<x[pstart+i]; t++) used[x[i]+t] += u[i]; // Check resource usage at start times for (int i=0; i<n; i++) if (x[n+i] > l && used[x[i]]+u[i] > cmax) { delete [] used; return false; } delete [] used; return true; } /// Post constraint on \a x virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { int nn = (c >= 0) ? x.size() : x.size()-1; int n=nn / 3; int pstart= (c >= 0) ? 2*n : 2*n+1;
Gecode::IntVarArgs s(n); Gecode::IntVarArgs px(n); Gecode::IntVarArgs e(home,n, Gecode::Int::Limits::min, Gecode::Int::Limits::max); for (int i=n; i--;) { s[i] = expr(home, o+x[i]); px[i] = x[pstart+i]; rel(home, s[i]+px[i] == e[i]); rel(home, _minP <= px[i]); rel(home, _maxP >= px[i]); } Gecode::BoolVarArgs m(n); for (int i=0; i<n; i++) m[i]=Gecode::expr(home, (x[n+i] > l)); if (c >= 0) { Gecode::cumulative(home, c, s, px, e, u, m, ipl); } else { Gecode::rel(home, x[2*n] <= -c); Gecode::cumulative(home, x[2*n], s, px, e, u, m, ipl); } } };
/// Help class to create and register tests class Create { public: /// Perform creation and registration Create(void) { using namespace Gecode; IntArgs p1({1,1,1,1}); IntArgs p2({2,2,2,2}); IntArgs p3({4,3,3,5}); IntArgs p4({4,0,3,5}); IntArgs p5({1,1,1});
IntArgs u1({1,1,1,1}); IntArgs u2({2,2,2,2}); IntArgs u3({2,3,4,5}); IntArgs u4({2,3,0,5}); IntArgs u5({1,3,2});
for (IntPropBasicAdvanced ipba; ipba(); ++ipba) { // Regression test: check correct detection of disjunctive case (void) new ManFixPCumulative(3,p5,u5,0,ipba.ipl());
for (int c=-7; c<8; c++) { int off = 0; for (int coff=0; coff<2; coff++) { (void) new ManFixPCumulative(c,p1,u1,off,ipba.ipl()); (void) new ManFixPCumulative(c,p1,u2,off,ipba.ipl()); (void) new ManFixPCumulative(c,p1,u3,off,ipba.ipl()); (void) new ManFixPCumulative(c,p1,u4,off,ipba.ipl()); (void) new ManFixPCumulative(c,p2,u1,off,ipba.ipl()); (void) new ManFixPCumulative(c,p2,u2,off,ipba.ipl()); (void) new ManFixPCumulative(c,p2,u3,off,ipba.ipl()); (void) new ManFixPCumulative(c,p2,u4,off,ipba.ipl()); (void) new ManFixPCumulative(c,p3,u1,off,ipba.ipl()); (void) new ManFixPCumulative(c,p3,u2,off,ipba.ipl()); (void) new ManFixPCumulative(c,p3,u3,off,ipba.ipl()); (void) new ManFixPCumulative(c,p3,u4,off,ipba.ipl()); (void) new ManFixPCumulative(c,p4,u1,off,ipba.ipl()); (void) new ManFixPCumulative(c,p4,u2,off,ipba.ipl()); (void) new ManFixPCumulative(c,p4,u3,off,ipba.ipl()); (void) new ManFixPCumulative(c,p4,u4,off,ipba.ipl());
(void) new ManFlexCumulative(c,0,1,u1,off,ipba.ipl()); (void) new ManFlexCumulative(c,0,1,u2,off,ipba.ipl()); (void) new ManFlexCumulative(c,0,1,u3,off,ipba.ipl()); (void) new ManFlexCumulative(c,0,1,u4,off,ipba.ipl()); (void) new ManFlexCumulative(c,0,2,u1,off,ipba.ipl()); (void) new ManFlexCumulative(c,0,2,u2,off,ipba.ipl()); (void) new ManFlexCumulative(c,0,2,u3,off,ipba.ipl()); (void) new ManFlexCumulative(c,0,2,u4,off,ipba.ipl()); (void) new ManFlexCumulative(c,3,5,u1,off,ipba.ipl()); (void) new ManFlexCumulative(c,3,5,u2,off,ipba.ipl()); (void) new ManFlexCumulative(c,3,5,u3,off,ipba.ipl()); (void) new ManFlexCumulative(c,3,5,u4,off,ipba.ipl());
(void) new OptFixPCumulative(c,p1,u1,off,ipba.ipl()); (void) new OptFixPCumulative(c,p1,u2,off,ipba.ipl()); (void) new OptFixPCumulative(c,p1,u3,off,ipba.ipl()); (void) new OptFixPCumulative(c,p1,u4,off,ipba.ipl()); (void) new OptFixPCumulative(c,p2,u1,off,ipba.ipl()); (void) new OptFixPCumulative(c,p2,u2,off,ipba.ipl()); (void) new OptFixPCumulative(c,p2,u3,off,ipba.ipl()); (void) new OptFixPCumulative(c,p2,u4,off,ipba.ipl()); (void) new OptFixPCumulative(c,p3,u1,off,ipba.ipl()); (void) new OptFixPCumulative(c,p3,u2,off,ipba.ipl()); (void) new OptFixPCumulative(c,p3,u3,off,ipba.ipl()); (void) new OptFixPCumulative(c,p3,u4,off,ipba.ipl()); (void) new OptFixPCumulative(c,p4,u1,off,ipba.ipl()); (void) new OptFixPCumulative(c,p4,u2,off,ipba.ipl()); (void) new OptFixPCumulative(c,p4,u3,off,ipba.ipl()); (void) new OptFixPCumulative(c,p4,u4,off,ipba.ipl());
(void) new OptFlexCumulative(c,0,1,u1,off,ipba.ipl()); (void) new OptFlexCumulative(c,0,1,u2,off,ipba.ipl()); (void) new OptFlexCumulative(c,0,1,u3,off,ipba.ipl()); (void) new OptFlexCumulative(c,0,1,u4,off,ipba.ipl()); (void) new OptFlexCumulative(c,0,2,u1,off,ipba.ipl()); (void) new OptFlexCumulative(c,0,2,u2,off,ipba.ipl()); (void) new OptFlexCumulative(c,0,2,u3,off,ipba.ipl()); (void) new OptFlexCumulative(c,0,2,u4,off,ipba.ipl()); (void) new OptFlexCumulative(c,3,5,u1,off,ipba.ipl()); (void) new OptFlexCumulative(c,3,5,u2,off,ipba.ipl()); (void) new OptFlexCumulative(c,3,5,u3,off,ipba.ipl()); (void) new OptFlexCumulative(c,3,5,u4,off,ipba.ipl());
off = Gecode::Int::Limits::min; } } } } };
Create c; //@}
}}}
// STATISTICS: test-int