001    package net.sf.cpsolver.ifs.extension;
002    
003    import java.util.*;
004    
005    import net.sf.cpsolver.ifs.model.*;
006    import net.sf.cpsolver.ifs.util.*;
007    
008    /**
009     * This class describing a set of assignment (used by CBS).
010     *
011     * It also contains a counter, name, description and a constraint (for printing purposes).
012     *
013     * @version
014     * IFS 1.1 (Iterative Forward Search)<br>
015     * Copyright (C) 2006 Tomáš Müller<br>
016     * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
017     * Lazenska 391, 76314 Zlin, Czech Republic<br>
018     * <br>
019     * This library is free software; you can redistribute it and/or
020     * modify it under the terms of the GNU Lesser General Public
021     * License as published by the Free Software Foundation; either
022     * version 2.1 of the License, or (at your option) any later version.
023     * <br><br>
024     * This library is distributed in the hope that it will be useful,
025     * but WITHOUT ANY WARRANTY; without even the implied warranty of
026     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
027     * Lesser General Public License for more details.
028     * <br><br>
029     * You should have received a copy of the GNU Lesser General Public
030     * License along with this library; if not, write to the Free Software
031     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
032     */
033    
034    public class AssignmentSet {
035        private Vector iSet = new FastVector();
036        private int iCounter = 1;
037        private String iName = null;
038        private String iDescription = null;
039        private Constraint iConstraint = null;
040        
041        public AssignmentSet() {}
042        public AssignmentSet(Assignment[] assignments) {
043            for (int i = 0; i < assignments.length; i++)
044                iSet.addElement(assignments[i]);
045        }
046        public AssignmentSet(Collection assignments) {
047            for (Iterator i = assignments.iterator(); i.hasNext();) {
048                iSet.addElement((Assignment)i.next());
049            }
050        }
051        
052        /** Create set of assignments from the list of Assignments, Values or (assigned) Variables */
053        public static AssignmentSet createAssignmentSet(long iteration, Collection assignments, double ageing) {
054            AssignmentSet set = new AssignmentSet();
055            for (Iterator i = assignments.iterator(); i.hasNext();) {
056                Object o = (Object)i.next();
057                if (o instanceof Assignment)
058                    set.addAssignment((Assignment)o);
059                if (o instanceof Value)
060                    set.addAssignment(new Assignment(iteration, ((Value)o), ageing));
061                if (o instanceof Variable && ((Variable)o).getAssignment() != null)
062                    set.addAssignment(new Assignment(iteration, ((Variable)o).getAssignment(), ageing));
063            }
064            return set;
065        }
066        
067        /** Increment counter*/
068        public void incCounter() {
069            iCounter++;
070        }
071        /** Returns counter */
072        public int getCounter() {
073            return iCounter;
074        }
075        /** Returns set of assignments */
076        public Vector getSet() {
077            return iSet;
078        }
079        /** Returns name */
080        public String getName() {
081            return iName;
082        }
083        /** Sets name */
084        public void setName(String name) {
085            iName = name;
086        }
087        /** Returns description */
088        public String getDescription() {
089            return iDescription;
090        }
091        /** Sets description */
092        public void setDescription(String description) {
093            iDescription = description;
094        }
095        /** Returns constraint */
096        public Constraint getConstraint() {
097            return iConstraint;
098        }
099        /** Sets constraint */
100        public void setConstraint(Constraint constraint) {
101            iConstraint = constraint;
102        }
103        /** Returns true if it contains the given assignment */
104        public boolean contains(Assignment assignment) {
105            return iSet.contains(assignment);
106        }
107        /* Returns true if it contains all of the given assignments */
108        public boolean contains(AssignmentSet assignmentSet) {
109            return iSet.containsAll(assignmentSet.getSet());
110        }
111        /** Returns true if it contains the given assignment */
112        public boolean contains(Value value) {
113            return iSet.contains(new Assignment(0l, value, 1.0));
114        }
115        /** Returns true if it contains the given assignment (assigned variable) */
116        public boolean contains(Variable variable) {
117            return (variable.getAssignment() == null ? false : iSet.contains(new Assignment(0l, variable.getAssignment(), 1.0)));
118        }
119        /* Returns true if it contains all of the given assignments */
120        public boolean contains(Collection assignments) {
121            for (Iterator i = assignments.iterator(); i.hasNext();) {
122                Object o = i.next();
123                if (o == null)
124                    return false;
125                if (o instanceof Assignment && !iSet.contains((Assignment)o))
126                    return false;
127                if (o instanceof Value || !iSet.contains(new Assignment(0l, ((Value)o), 1.0)))
128                    return false;
129                if (o instanceof Variable && (((Variable)o).getAssignment() == null || !iSet.contains(new Assignment(0l, ((Variable)o).getAssignment(), 1.0))))
130                    return false;
131            }
132            return true;
133        }
134        
135        /** Adds an assignment */
136        public void addAssignment(Assignment assignment) {
137            if (!contains(assignment))
138                iSet.addElement(assignment);
139        }
140        /** Adds an assignment */
141        public void addAssignment(long iteration, Value value, double ageing) {
142            addAssignment(new Assignment(iteration, value, ageing));
143        }
144        /** Returns assignment that corresponds to the given value (if it is present in the set) */
145        public Assignment getAssignment(Value value) {
146            for (Enumeration i = getSet().elements(); i.hasMoreElements();) {
147                Assignment a = (Assignment)i.nextElement();
148                if (a.getValue().getId() == value.getId())
149                    return a;
150            }
151            return null;
152        }
153        /** Returns number of assignments in the set*/    
154        public int size() {
155            return getSet().size();
156        }
157        /** Compares two assignment sets -- name, size and content (assignments) has to match. */
158        public boolean equals(Object o) {
159            if (o == null)
160                return false;
161            if (o instanceof AssignmentSet) {
162                AssignmentSet as = (AssignmentSet)o;
163                if (getName() == null && as.getName() != null)
164                    return false;
165                if (getName() != null && as.getName() == null)
166                    return false;
167                if (getName() != null && !getName().equals(as.getName()))
168                    return false;
169                if (as.getSet().size() != getSet().size())
170                    return false;
171                return contains(as);
172            }
173            if (o instanceof Collection) {
174                Collection c = (Collection)o;
175                if (c.size() != getSet().size())
176                    return false;
177                return contains(c);
178            }
179            return false;
180        }
181        
182        public static int xor(int a, int b) {
183            return (a | b) & (~a | ~b);
184        }
185        
186        public int hashCode() {
187            int ret = getSet().size();
188            for (Enumeration i = getSet().elements(); i.hasMoreElements();) {
189                Assignment a = (Assignment)i.nextElement();
190                ret = xor(ret, a.hashCode());
191            }
192            return ret;
193        }
194    }