Package de.timefinder.algo.ncp

Source Code of de.timefinder.algo.ncp.NoCollisionPrincipleTest

/*
* This file is part of the TimeFinder project.
*  Visit http://www.timefinder.de for more information.
*  Copyright (c) 2009 the original author or authors.
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*        http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package de.timefinder.algo.ncp;

import de.timefinder.algo.AlgorithmConditionIteration;
import de.timefinder.algo.AlgorithmConditionTime;
import de.timefinder.algo.SilentConsoleStatusBar;
import de.timefinder.algo.constraint.EventOrderConstraint;
import de.timefinder.algo.io.DataPool4Track2;
import de.timefinder.algo.util.TimeFinder2Tester;
import de.timefinder.data.Event;
import de.timefinder.data.algo.Assignment;
import de.timefinder.data.algo.Solution;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;

/**
* @author Peter Karich, peat_hal 'at' users 'dot' sourceforge 'dot' net
*/
public class NoCollisionPrincipleTest extends TimeFinder2Tester {

    private NoCollisionPrinciple ncpInstance;
    private String TIM_FILE = "comp-2007-2-09.tim";

    @Before
    @Override
    public void setUp() {
        super.setUp();

        // 1 hour       
        settings.setMillisPerTimeslot(60 * 60 * 1000L);
        settings.setNumberOfDays(5);
        // 9 hours per day
        settings.setTimeslotsPerDay(9);

        ncpInstance = new NoCollisionPrinciple();
        ncpInstance.setCondition(new AlgorithmConditionIteration(10000));
        ncpInstance.setRandom(new Random(123456L));
        ncpInstance.setDataPoolSettings(settings);
        ncpInstance.setStatusBar(new SilentConsoleStatusBar());
    }

    @Test
    public void testRunAlgorithm() {
        System.out.println("runAlgorithm");
        DataPool4Track2 dataPool4Track2 = createDP4Track2();
        dataPool4Track2.doWork();
        assertTrue(ncpInstance.getDataPool().getDao(Event.class).getAll().size() > 100);
        Solution sol = ncpInstance.doWork();
        ConstraintChecker.printStatistics(sol, true);
        assertTrue("Optimization is not efficient enough!", ncpInstance.getHardConflicts() < 20);
    }

    @Test
    public void testCorrectOrder() {
        System.out.println("correctOrder");
        Assignment a = newAssignment("A");
        Assignment b = newAssignment("B");
        Assignment c = newAssignment("C");
        Assignment d = newAssignment("D");
        Assignment e = newAssignment("E");
        Assignment f = newAssignment("F");
        Assignment g = newAssignment("G");
        Assignment h = newAssignment("H");
        Assignment i = newAssignment("I");
        Assignment j = newAssignment("J");
        Assignment k = newAssignment("K");
        Assignment l = newAssignment("L");
        Assignment m = newAssignment("M");
        connect(b, a);
        connect(c, b);
        connect(b, d);
        connect(c, d);
        connect(d, e);
        connect(d, f);
        connect(d, i);
        connect(g, h);
        connect(g, f);
        connect(c, e);
        connect(e, g);
        connect(a, i);
        connect(b, i);
        connect(b, f);
        connect(j, f);
        connect(k, f);
        connect(f, l);
        connect(f, m);

        List<Assignment> canister = Arrays.asList(b, a, c, d, e, f, g, h, i, j, k, l, m);
//        Map<Assignment, Integer> indexedTimeIntervals = new FastMap<Assignment, Integer>(canister.size());
//        for (int index = 0; index < canister.size(); index++) {
//            indexedTimeIntervals.put(canister.get(index), index);
//        }

        ncpInstance.correctOrderWithBubbleSort(canister);
        check(canister);
    }

    private void check(List<Assignment> list) {
        Assignment subj;
        for (int i = 0; i < list.size(); i++) {
            subj = list.get(i);

            //only check followers
            for (Event f : subj.getEvent().getConstraint(EventOrderConstraint.class).getFollows()) {
                assertTrue("i:" + i + " \tcurrent:" + subj + " \tfollower:" + f
                        + " \tlist:" + list, indexOfEvent(list, f) > i);
            }
        }
    }

    private void connect(Assignment prev, Assignment follow) {
        EventOrderConstraint oc = prev.getEvent().getConstraint(EventOrderConstraint.class);
        oc.addFollow(follow.getEvent());

        EventOrderConstraint oc2 = follow.getEvent().getConstraint(EventOrderConstraint.class);
        oc2.addBefore(prev.getEvent());
    }

    @Override
    protected Assignment newAssignment(String str) {
        Assignment ass = super.newAssignment(str, 1, 1);
        ass.getEvent().putConstraint(new EventOrderConstraint(ass.getEvent()));
        return ass;
    }

    private int indexOfEvent(List<Assignment> list, Event event) {
        int counter = -1;
        for (Assignment ass : list) {
            counter++;
            if (ass.getEvent() == event)
                break;
        }
        return counter;
    }

    @Test
    public void testGetInitialPeriode() {
        DataPool4Track2 dataPool4Track2 = createDP4Track2();
        dataPool4Track2.doWork();
        int oldSize = ncpInstance.getDataPool().getDao(Event.class).getAll().size();

        Period currentWeek = ncpInstance.getInitialPeriod(true);
        assertEquals(oldSize, currentWeek.getAll().size());
        assertEquals(oldSize, currentWeek.getInvalidAssignments().size());
    }

    private DataPool4Track2 createDP4Track2() {
        InputStream iStream = NoCollisionPrinciple.class.getResourceAsStream(TIM_FILE);
        assertNotNull(iStream);
        InputStreamReader reader = new InputStreamReader(iStream);
        DataPool4Track2 dataPool4Track2 = new DataPool4Track2();
        dataPool4Track2.setSettings(settings);
        dataPool4Track2.setDataPool(dataPool);
        dataPool4Track2.setSource(reader);
        ncpInstance.setDataPool(dataPool4Track2.getDataPool());

        return dataPool4Track2;
    }
}
TOP

Related Classes of de.timefinder.algo.ncp.NoCollisionPrincipleTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.