/**
 *  Copyright (c) 1999-2011, Ecole des Mines de Nantes
 *  All rights reserved.
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *      * Redistributions of source code must retain the above copyright
 *        notice, this list of conditions and the following disclaimer.
 *      * Redistributions in binary form must reproduce the above copyright
 *        notice, this list of conditions and the following disclaimer in the
 *        documentation and/or other materials provided with the distribution.
 *      * Neither the name of the Ecole des Mines de Nantes nor the
 *        names of its contributors may be used to endorse or promote products
 *        derived from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
 *  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
 *  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package solver.search;


import org.testng.Assert;
import org.testng.annotations.Test;
import solver.ResolutionPolicy;
import solver.Solver;
import solver.constraints.ICF;
import solver.variables.IntVar;
import solver.variables.VF;

import java.util.Random;

/**
 * <br/>
 *
 * @author Charles Prud'homme
 * @since 19/07/13
 */
public class ObjectiveTest {

    @Test(groups = "1s")
    public void test1() {
        Solver solver = new Solver();

        IntVar iv = VF.enumerated("iv", -5, 15, solver);
        solver.post(ICF.arithm(iv, ">=", 0));
        solver.post(ICF.arithm(iv, "<=", 10));
        Random rnd = new Random();
        for (int i = 0; i < 100; i++) {
            int k = rnd.nextInt(4);
            switch (k) {
                case 0:
                    one(solver, iv);
                    break;
                case 1:
                    all(solver, iv);
                    break;
                case 2:
                    min(solver, iv);
                    break;
                case 3:
                    max(solver, iv);
                    break;
            }
        }
    }

    private void one(Solver solver, IntVar iv) {
        for (int i = 0; i < 2; i++) {
            solver.getSearchLoop().reset();
            solver.findSolution();
            Assert.assertEquals(solver.getMeasures().getSolutionCount(), 1);
            Assert.assertEquals(solver.getMeasures().getNodeCount(), 2);
        }
    }

    private void all(Solver solver, IntVar iv) {
        for (int i = 0; i < 2; i++) {
            solver.getSearchLoop().reset();
            solver.findAllSolutions();
            Assert.assertEquals(solver.getMeasures().getSolutionCount(), 11);
            Assert.assertEquals(solver.getMeasures().getNodeCount(), 21);
        }
    }

    private void min(Solver solver, IntVar iv) {
        for (int i = 0; i < 2; i++) {
            solver.getSearchLoop().reset();
            solver.findOptimalSolution(ResolutionPolicy.MINIMIZE, iv);
            Assert.assertEquals(solver.getMeasures().getBestSolutionValue(), 0);
            Assert.assertEquals(solver.getMeasures().getNodeCount(), 2);
        }
    }

    private void max(Solver solver, IntVar iv) {
        for (int i = 0; i < 2; i++) {
            solver.getSearchLoop().reset();
            solver.findOptimalSolution(ResolutionPolicy.MAXIMIZE, iv);
            Assert.assertEquals(solver.getMeasures().getBestSolutionValue(), 10);
            Assert.assertEquals(solver.getMeasures().getNodeCount(), 21);
        }
    }

    @Test(groups = "1s")
    public void test2() {
        Solver solver = new Solver();
        IntVar iv = VF.enumerated("iv", 0, 10, solver);
        solver.post(ICF.arithm(iv, ">=", 2));

        solver.findOptimalSolution(ResolutionPolicy.MINIMIZE, iv);
        Assert.assertEquals(iv.getValue(), 2);

        solver.getSearchLoop().reset();

        solver.findOptimalSolution(ResolutionPolicy.MINIMIZE, iv);
        Assert.assertEquals(iv.getValue(), 2);
    }

}

@@ -1795,16 +1795,69 @@
 ts.ICF;%0A
+import solver.search.loop.monitors.IMonitorSolution;%0A
 import s
@@ -4682,10 +4682,970 @@
 %0A    %7D%0A%0A
+    @Test(groups = %221s%22)%0A    public void test3() %7B%0A        final Solver solver = new Solver();%0A        final IntVar iv = VF.enumerated(%22iv%22, 0, 10, solver);%0A        solver.post(ICF.arithm(iv, %22%3E=%22, 2));%0A%0A        solver.getSearchLoop().plugSearchMonitor(new IMonitorSolution() %7B%0A            @Override%0A            public void onSolution() %7B%0A                solver.post(ICF.arithm(iv, %22%3E=%22, 4));%0A            %7D%0A        %7D);%0A        solver.findSolution();%0A        Assert.assertEquals(iv.getValue(), 2);%0A%0A        solver.getSearchLoop().reset();%0A        solver.getSearchLoop().plugSearchMonitor(new IMonitorSolution() %7B%0A            @Override%0A            public void onSolution() %7B%0A                solver.postCut(ICF.arithm(iv, %22%3E=%22, 6));%0A            %7D%0A        %7D);%0A        solver.findSolution();%0A        Assert.assertEquals(iv.getValue(), 2);%0A%0A        solver.getSearchLoop().reset();%0A        solver.findSolution();%0A        Assert.assertEquals(iv.getValue(), 6);%0A    %7D%0A%0A
 %7D%0A

Add a test (which fails) on dynamic addition of constraints and multiple resolution of the same problem