Generated on Thu Apr 11 13:59:10 2019 for Gecode by doxygen 1.6.3

sym-imp.cpp

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Christopher Mears <chris.mears@monash.edu>
00005  *
00006  *  Copyright:
00007  *     Christopher Mears, 2012
00008  *
00009  *  This file is part of Gecode, the generic constraint
00010  *  development environment:
00011  *     http://www.gecode.org
00012  *
00013  *  Permission is hereby granted, free of charge, to any person obtaining
00014  *  a copy of this software and associated documentation files (the
00015  *  "Software"), to deal in the Software without restriction, including
00016  *  without limitation the rights to use, copy, modify, merge, publish,
00017  *  distribute, sublicense, and/or sell copies of the Software, and to
00018  *  permit persons to whom the Software is furnished to do so, subject to
00019  *  the following conditions:
00020  *
00021  *  The above copyright notice and this permission notice shall be
00022  *  included in all copies or substantial portions of the Software.
00023  *
00024  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00025  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00026  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00027  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00028  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00029  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00030  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00031  *
00032  */
00033 
00034 #include <gecode/set/ldsb.hh>
00035 
00036 namespace Gecode { namespace Set { namespace LDSB {
00037   // XXX: implementation of equalValues could be improved?
00038 
00040   bool
00041   equalLUB(const Set::SetView& x, const Set::SetView& y) {
00042     unsigned int n = x.lubSize();
00043     if (n != y.lubSize()) return false;
00044     for (unsigned int i = 0 ; i < n ; i++)
00045       if (x.lubMinN(i) != y.lubMinN(i))
00046         return false;
00047     return true;
00048   }
00049 }}}
00050 
00051 // Note carefully that this is in the Gecode::Int::LDSB namespace, not
00052 // Gecode::Set::LDSB.
00053 namespace Gecode { namespace Int { namespace LDSB {
00054   template <>
00055   ArgArray<Literal>
00056   VariableSymmetryImp<Set::SetView>
00057   ::symmetric(Literal l, const ViewArray<Set::SetView>& x) const {
00058     (void) x;
00059     if (indices.valid(l._variable) && indices.get(l._variable)) {
00060       int n = 0;
00061       for (Iter::Values::BitSetOffset<Support::BitSetOffset<Space> > i(indices) ; i() ; ++i)
00062         n++;
00063       ArgArray<Literal> lits(n);
00064       int j = 0;
00065       for (Iter::Values::BitSetOffset<Support::BitSetOffset<Space> > i(indices) ; i() ; ++i) {
00066         lits[j++] = Literal(i.val(), l._value);
00067       }
00068       return lits;
00069     } else {
00070       return ArgArray<Literal>(0);
00071     }
00072   }
00073 
00074   template <>
00075   ArgArray<Literal>
00076   ValueSymmetryImp<Set::SetView>
00077   ::symmetric(Literal l, const ViewArray<Set::SetView>& x) const {
00078     (void) x;
00079     if (values.valid(l._value) && values.get(l._value)) {
00080       int n = 0;
00081       for (Iter::Values::BitSetOffset<Support::BitSetOffset<Space> > i(values) ; i() ; ++i)
00082         n++;
00083       ArgArray<Literal> lits(n);
00084       int j = 0;
00085       for (Iter::Values::BitSetOffset<Support::BitSetOffset<Space> > i(values) ; i() ; ++i) {
00086         lits[j++] = Literal(l._variable, i.val());
00087       }
00088       return lits;
00089     } else {
00090       return ArgArray<Literal>(0);
00091     }
00092   }
00093 
00094   template <>
00095   ArgArray<Literal>
00096   VariableSequenceSymmetryImp<Set::SetView>
00097   ::symmetric(Literal l, const ViewArray<Set::SetView>& x) const {
00098     Region region;
00099     Support::DynamicStack<Literal,Region> s(region);
00100     if (l._variable < (int)lookup_size) {
00101       int posIt = lookup[l._variable];
00102       if (posIt == -1) {
00103         return dynamicStackToArgArray(s);
00104       }
00105       unsigned int seqNum = posIt / seq_size;
00106       unsigned int seqPos = posIt % seq_size;
00107       for (unsigned int seq = 0 ; seq < n_seqs ; seq++) {
00108         if (seq == seqNum) {
00109           continue;
00110         }
00111         if (x[getVal(seq, seqPos)].assigned()) {
00112           continue;
00113         }
00114         bool active = true;
00115         const unsigned int *firstSeq = &indices[seqNum*seq_size];
00116         const unsigned int *secondSeq = &indices[seq*seq_size];
00117         for (unsigned int i = 0 ; i < seq_size ; i++) {
00118           const Set::SetView& xv = x[firstSeq[i]];
00119           const Set::SetView& yv = x[secondSeq[i]];
00120           if ((!xv.assigned() && !yv.assigned())
00121               || (xv.assigned() && yv.assigned() && Set::LDSB::equalLUB(xv, yv))) {
00122             continue;
00123           } else {
00124             active = false;
00125             break;
00126           }
00127         }
00128 
00129         if (active) {
00130           s.push(Literal(secondSeq[seqPos], l._value));
00131         }
00132       }
00133     }
00134     return dynamicStackToArgArray(s);
00135   }
00136 
00137   template <>
00138   ArgArray<Literal>
00139   ValueSequenceSymmetryImp<Set::SetView>
00140   ::symmetric(Literal l, const ViewArray<Set::SetView>& x) const {
00141     (void) x;
00142     Region region;
00143     Support::DynamicStack<Literal,Region> s(region);
00144     std::pair<int,int> location = findVar(values, n_values, seq_size, l._value);
00145     if (location.first == -1) return dynamicStackToArgArray(s);
00146     unsigned int seqNum = location.first;
00147     unsigned int seqPos = location.second;
00148     for (unsigned int seq = 0 ; seq < n_seqs ; seq++) {
00149       if (seq == seqNum) continue;
00150       if (dead_sequences.get(seq)) continue;
00151       s.push(Literal(l._variable, getVal(seq,seqPos)));
00152     }
00153     return dynamicStackToArgArray(s);
00154   }
00155 }}}
00156 
00157 // STATISTICS: set-branch