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