001package org.clafer.compiler;
002
003import org.chocosolver.solver.Solution;
004import org.chocosolver.solver.Solver;
005import org.chocosolver.solver.variables.IntVar;
006import org.clafer.instance.InstanceModel;
007
008/**
009 *
010 * @author jimmy
011 */
012public class ClaferSingleObjectiveOptimizer extends AbstractImprovementOptimizer {
013
014    private int count = 0;
015    private boolean more = true;
016    private int optimalValue;
017    private Solution firstSolution = null;
018
019    ClaferSingleObjectiveOptimizer(Solver solver, ClaferSolutionMap solutionMap,
020            boolean maximize, IntVar score) {
021        super(solver, solutionMap, new boolean[]{maximize}, new IntVar[]{score});
022    }
023
024    public boolean isMaximize() {
025        return maximizes[0];
026    }
027
028    public boolean isMinimize() {
029        return !isMaximize();
030    }
031
032    @Override
033    public boolean find() throws ReachedLimitException {
034        if (!more || count == 1) {
035            more = false;
036            return false;
037        }
038        more &= solveFirst();
039        if (solver.isStopCriterionMet()) {
040            if (firstSolution != null) {
041                InstanceModel bestInstance = solutionMap.getInstance(firstSolution);
042                int bestObjectiveValue = firstSolution.getIntVal(scores[0]);
043                throw new ReachedLimitBestKnownException(
044                        bestInstance,
045                        new int[]{bestObjectiveValue});
046            }
047            throw new ReachedLimitException();
048        }
049        if (more) {
050            optimalValue = firstSolution.getIntVal(scores[0]);
051            count++;
052        }
053        return more;
054    }
055
056    /*
057     * Implementation of multiple optimal search based on discussion here:
058     * https://github.com/chocoteam/choco3/issues/121.
059     */
060    private boolean solveFirst() {
061        IntVar scoreVar = scores[0];
062        firstSolution = solver.findOptimalSolution(scoreVar, isMaximize());
063        return firstSolution != null && !solver.isStopCriterionMet();
064    }
065
066    @Override
067    public InstanceModel instance() {
068        if (count == 0 || !more) {
069            throw new IllegalStateException("No instances. Did you forget to call find?");
070        }
071        return solutionMap.getInstance(firstSolution);
072    }
073
074    @Override
075    public Solution solution() {
076        if (count == 0 || !more) {
077            throw new IllegalStateException("No instances. Did you forget to call find?");
078        }
079        return firstSolution;
080    }
081
082    @Override
083    public int[] optimalValues() {
084        if (count == 0) {
085            throw new IllegalStateException("No instances. Did you forget to call find?");
086        }
087        return new int[]{optimalValue};
088    }
089
090    @Override
091    public int instanceCount() {
092        return count;
093    }
094
095    @Override
096    public Solver getInternalSolver() {
097        return solver;
098    }
099}