/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main authors: * Christian Schulte * * Copyright: * Christian Schulte, 2010 * * Last modified: * $Date: 2010-10-07 08:20:35 +1100 (Thu, 07 Oct 2010) $ by $Author: schulte $ * $Revision: 11468 $ * * 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 #include namespace Test { namespace Int { /// %Tests for bin-packing constraint namespace BinPacking { /** * \defgroup TaskTestIntBinPacking Bin-packing constraints * \ingroup TaskTestInt */ //@{ /// Generate load and bin assignments class LoadBinAssignment : public Assignment { protected: /// Number of bins int n_bins; /// Number of items int n_items; /// Domain for load variables Gecode::IntSet d_load; /// Domain for bin variables Gecode::IntSet d_bin; /// Load to generate (unless -1) int load; /// Iterator for each variable Gecode::IntSetValues* dsv; public: /// Initialize assignments for load and bin variables LoadBinAssignment(int m, const Gecode::IntSet& d_m, int n, const Gecode::IntSet& d_n, int l) : Assignment(m+n,d_m), n_bins(m), n_items(n), d_load(d_m), d_bin(d_n), load(l), dsv(new Gecode::IntSetValues[static_cast(m+n)]) { for (int i=n_bins; i--; ) dsv[i].init(d_load); for (int i=n_items; i--; ) dsv[n_bins+i].init(d_bin); } /// Test whether all assignments have been iterated virtual bool operator()(void) const { return dsv[0](); } /// Move to next assignment virtual void operator++(void) { // Try to generate next bin assignment { int i = n_items-1; while (i >= 0) { ++dsv[n_bins+i]; if (dsv[n_bins+i]()) return; dsv[n_bins+(i--)].init(d_bin); } } // Try to generate next load assignment { retry: int i = n_bins-1; while (true) { ++dsv[i]; if (dsv[i]() || (i == 0)) { if (dsv[i]() && (load >= 0)) { int l = 0; for (int k=0;k=0) && (i