001package org.cpsolver.ifs.assignment.context;
002
003import org.cpsolver.ifs.assignment.Assignment;
004import org.cpsolver.ifs.heuristics.NeighbourSelection;
005import org.cpsolver.ifs.model.Value;
006import org.cpsolver.ifs.model.Variable;
007import org.cpsolver.ifs.solver.Solver;
008
009/**
010 * A neighborhood selection with an assignment context. In order to be able to hold multiple assignments in memory
011 * it is desired for all the assignment dependent data a selection may need (to keep its current state),
012 * to store these data in a separate class (implementing the 
013 * {@link AssignmentContext} interface). This context is created by calling
014 * {@link ConstraintWithContext#createAssignmentContext(Assignment)} and accessed by
015 * {@link ConstraintWithContext#getContext(Assignment)}.
016 * 
017 * 
018 * @see AssignmentContext
019 * 
020 * @author  Tomáš Müller
021 * @version IFS 1.3 (Iterative Forward Search)<br>
022 *          Copyright (C) 2014 Tomáš Müller<br>
023 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
024 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
025 * <br>
026 *          This library is free software; you can redistribute it and/or modify
027 *          it under the terms of the GNU Lesser General Public License as
028 *          published by the Free Software Foundation; either version 3 of the
029 *          License, or (at your option) any later version. <br>
030 * <br>
031 *          This library is distributed in the hope that it will be useful, but
032 *          WITHOUT ANY WARRANTY; without even the implied warranty of
033 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
034 *          Lesser General Public License for more details. <br>
035 * <br>
036 *          You should have received a copy of the GNU Lesser General Public
037 *          License along with this library; if not see <a href='http://www.gnu.org/licenses'>http://www.gnu.org/licenses</a>.
038 * @param <V> Variable
039 * @param <T> Value
040 * @param <C> Assignment Context
041 **/
042public abstract class NeighbourSelectionWithContext<V extends Variable<V, T>, T extends Value<V, T>, C extends AssignmentContext> implements NeighbourSelection<V, T>, HasAssignmentContext<V, T, C>, CanHoldContext {
043    private AssignmentContextReference<V, T, C> iContextReference = null;
044    private AssignmentContext[] iContext = new AssignmentContext[CanHoldContext.sMaxSize];
045    protected C iContextOverride = null;
046
047    @Override
048    public void init(Solver<V, T> solver) {
049        iContextReference = solver.currentSolution().getModel().createReference(this);
050        if (isSingleContextSolver(solver))
051            iContextOverride = createAssignmentContext(solver.currentSolution().getAssignment());
052    }
053    
054    /**
055     * Returns true if there should be only one context for this neighbourhood selection.
056     * @param solver current solver
057     * @return {@link Solver#hasSingleSolution()}
058     */
059    protected boolean isSingleContextSolver(Solver<V, T> solver) {
060        return solver.hasSingleSolution();
061    }
062    
063    /**
064     * Returns an assignment context associated with this selection. If there is no 
065     * assignment context associated with this selection yet, one is created using the
066     * {@link ConstraintWithContext#createAssignmentContext(Assignment)} method. From that time on,
067     * this context is kept with the assignment.
068     * @param assignment given assignment
069     * @return assignment context associated with this selection and the given assignment
070     */
071    @Override
072    public C getContext(Assignment<V, T> assignment) {
073        if (iContextOverride != null)
074            return iContextOverride;
075        return AssignmentContextHelper.getContext(this, assignment);
076    }
077    
078    @Override
079    public AssignmentContextReference<V, T, C> getAssignmentContextReference() { return iContextReference; }
080
081    @Override
082    public void setAssignmentContextReference(AssignmentContextReference<V, T, C> reference) { iContextReference = reference; }
083
084    @Override
085    public AssignmentContext[] getContext() { return iContext; }
086    
087    /**
088     * Has context override
089     * @return true if all threads are using the same context
090     */
091    public boolean hasContextOverride() {
092        return iContextOverride != null;
093    }
094}