this repo has no description
at develop 3.9 kB view raw
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, 2003 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 <gecode/driver.hh> 35#include <gecode/int.hh> 36#include <gecode/minimodel.hh> 37 38using namespace Gecode; 39 40/** 41 * \brief %Example: partition numbers into two groups 42 * 43 * \ingroup Example 44 */ 45class Partition : public Script { 46protected: 47 /// First group of numbers 48 IntVarArray x; 49 /// Second group of numbers 50 IntVarArray y; 51public: 52 /// Actual model 53 Partition(const SizeOptions& opt) 54 : Script(opt), 55 x(*this,opt.size(),1,2*opt.size()), 56 y(*this,opt.size(),1,2*opt.size()) { 57 const int n = opt.size(); 58 59 // Break symmetries by ordering numbers in each group 60 rel(*this, x, IRT_LE); 61 rel(*this, y, IRT_LE); 62 63 rel(*this, x[0], IRT_LE, y[0]); 64 65 IntVarArgs xy(2*n); 66 for (int i = n; i--; ) { 67 xy[i] = x[i]; xy[n+i] = y[i]; 68 } 69 distinct(*this, xy, opt.ipl()); 70 71 IntArgs c(2*n); 72 for (int i = n; i--; ) { 73 c[i] = 1; c[n+i] = -1; 74 } 75 linear(*this, c, xy, IRT_EQ, 0); 76 77 // Array of products 78 IntVarArgs sxy(2*n), sx(n), sy(n); 79 80 for (int i = n; i--; ) { 81 sx[i] = sxy[i] = expr(*this, sqr(x[i])); 82 sy[i] = sxy[n+i] = expr(*this, sqr(y[i])); 83 } 84 linear(*this, c, sxy, IRT_EQ, 0); 85 86 // Redundant constraints 87 linear(*this, x, IRT_EQ, 2*n*(2*n+1)/4); 88 linear(*this, y, IRT_EQ, 2*n*(2*n+1)/4); 89 linear(*this, sx, IRT_EQ, 2*n*(2*n+1)*(4*n+1)/12); 90 linear(*this, sy, IRT_EQ, 2*n*(2*n+1)*(4*n+1)/12); 91 92 branch(*this, xy, INT_VAR_AFC_SIZE_MAX(opt.decay()), INT_VAL_MIN()); 93 } 94 95 /// Constructor used during cloning \a s 96 Partition(Partition& s) : Script(s) { 97 x.update(*this, s.x); 98 y.update(*this, s.y); 99 } 100 /// Copying during cloning 101 virtual Space* 102 copy(void) { 103 return new Partition(*this); 104 } 105 /// Print solution 106 virtual void 107 print(std::ostream& os) const { 108 os << "\t"; 109 int a, b; 110 a = b = 0; 111 for (int i = 0; i < x.size(); i++) { 112 a += x[i].val(); 113 b += x[i].val()*x[i].val(); 114 os << x[i] << ", "; 115 } 116 os << " = " << a << ", " << b << std::endl << "\t"; 117 a = b = 0; 118 for (int i = 0; i < y.size(); i++) { 119 a += y[i].val(); 120 b += y[i].val()*y[i].val(); 121 os << y[i] << ", "; 122 } 123 os << " = " << a << ", " << b << std::endl; 124 } 125}; 126 127/** 128 * \brief Main-functiona 129 * \relates Partition 130 */ 131int 132main(int argc, char* argv[]) { 133 SizeOptions opt("Partition"); 134 opt.size(32); 135 opt.ipl(IPL_BND); 136 opt.parse(argc,argv); 137 Script::run<Partition,DFS,SizeOptions>(opt); 138 return 0; 139} 140 141 142// STATISTICS: example-any 143