Package de.timefinder.core

Source Code of de.timefinder.core.RunAlgo

/*
*  Copyright 2009 Peter Karich, peat_hal ‘at’ users ‘dot’ sourceforge ‘dot’ net.
*
*  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.
*  under the License.
*/
package de.timefinder.core;

import de.timefinder.algo.AlgorithmConditionTime;
import de.timefinder.data.algo.DataPoolSettings;
import de.timefinder.algo.SilentConsoleStatusBar;
import de.timefinder.algo.io.DataPool4Track2;
import de.timefinder.algo.ncp.NoCollisionPrinciple;
import de.timefinder.data.DataPool;
import de.timefinder.data.DataPoolImpl;
import de.timefinder.data.access.EventDao;
import de.timefinder.data.access.FeatureDao;
import de.timefinder.data.access.LocationDao;
import de.timefinder.data.access.PersonDao;
import de.timefinder.data.access.impl.EventDaoSimpl;
import de.timefinder.data.access.impl.FeatureDaoSimpl;
import de.timefinder.data.access.impl.LocationDaoSimpl;
import de.timefinder.data.access.impl.PersonDaoSimpl;
import de.timefinder.core.io.xml.bt.ImportBTXml;
import de.timefinder.data.algo.Solution;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.logging.Logger;

/**
* Intel Core Duo P8600 @ 2.40GHz; 4 GB RAM; Ubuntu 9.04
* (find out with "sudo lshw -html > hw.html; opera hw.html")
*
* @author Peter Karich, peat_hal ‘at’ users ‘dot’ sourceforge ‘dot’ net
*/
public class RunAlgo {

    private static Logger logger = Logger.getLogger(RunAlgo.class.getName());
    private static String TIM_FOLDER = "/home/peterk/Dokumente/timefinder/ITC-2007-2008/track2/";
    private static String TIM_FILE = TIM_FOLDER + "comp-2007-2-22.tim";
    private static String BT_FOLDER = "/home/peterk/Dokumente/timefinder/import-bt";
    private static String OPTIMIZATION_FILE;
    private static boolean LOAD_BT = false;

    public static void main(String args[]) throws Exception {
        int timeInSec = 345;
        // 123123L
        // 321456L
        // 9486273L
        // 1812L
        long seed = 1812L;

        int ii = 0;
        for (String arg : args) {
            if ("-time".equalsIgnoreCase(arg)) {
                try {
                    timeInSec = Integer.parseInt(args[ii + 1]);
                } catch (Exception ex) {
                    logger.severe(ex.getLocalizedMessage());
                }
            } else if ("-seed".equalsIgnoreCase(arg)) {
                try {
                    seed = Long.parseLong(args[ii + 1]);
                } catch (Exception ex) {
                    logger.severe(ex.getLocalizedMessage());
                }
            }
            ii++;
        }
        logger.info("max time:" + timeInSec + " seed:" + seed);

        if (LOAD_BT)
            OPTIMIZATION_FILE = BT_FOLDER;
        else
            OPTIMIZATION_FILE = TIM_FILE;

        // warm up
        new RunAlgo().start(600);
//        new RunAlgo().start(30);
//
//        for (int i = 1; i < 10; i++) {
//            long tmpTime = System.currentTimeMillis();
//            new RunAlgo().start(timeInSec, seed, TIM_FOLDER + "comp-2007-2-0" + i + ".tim");
//            mem(i, tmpTime);
//        }
//
//        for (int i = 10; i < 25; i++) {
//            long tmpTime = System.currentTimeMillis();
//            new RunAlgo().start(timeInSec, seed, TIM_FOLDER + "comp-2007-2-" + i + ".tim");
//            mem(i, tmpTime);
//        }
    }

    public void performance() throws Exception {
        int NCP_COUNTER = 100;
        int COUNT = 10;

        logger.info("Start initialization");
        long tmpTime;
        // do not count the initial warm up phase
        for (int ii = 0; ii < COUNT / 4; ii++) {
            tmpTime = start(NCP_COUNTER);
            mem(ii, tmpTime);
        }

        logger.info("Start measurement");
        List<Long> allDuration = new ArrayList<Long>();
        for (int ii = 0; ii < COUNT; ii++) {
            tmpTime = start(NCP_COUNTER);
            mem(ii, tmpTime);
            allDuration.add(tmpTime);
        }

        statistics(allDuration);
    }

    public static void mem(int i, long time) {
//        System.out.println("iteration " + i + "; time was: " + (System.currentTimeMillis() - time) / 1000.0f
//                + ";  Free Mem " + (float) (Runtime.getRuntime().freeMemory() / 1024.0 / 1024.0) + " MB");
        System.out.println(i + "; " + (System.currentTimeMillis() - time) / 1000.0f);
    }

    private static void statistics(List<Long> allDurations) {
        long timeSum = 0;
        long maxDuration = 0;
        long minDuration = Integer.MAX_VALUE;

        for (Long val : allDurations) {
            if (val > maxDuration) {
                maxDuration = val;
            }
            if (val < minDuration) {
                minDuration = val;
            }
            timeSum += val;
        }
        float mean = timeSum / allDurations.size();
        double rms = 0;
        for (Long val : allDurations) {
            rms += (val - mean) * (val - mean);
        }
        System.out.println("mean duration: " + mean);
        rms = (float) Math.sqrt(rms / allDurations.size());
        System.out.print("root mean square duration: " + (float) rms);
        System.out.println(" (" + (float) (rms / mean * 100) + " %)");
        System.out.println("min, max duration: " + minDuration + ", " + maxDuration);
    }

    public long start(int seconds) throws Exception {
        return start(seconds, 1345678909876543219L, OPTIMIZATION_FILE);
    }

    public long start(int seconds, long seed, String fileOrFolder) throws Exception {
//        logger.info("process file:" + fileOrFolder);

        EventDao eDao = new EventDaoSimpl();
        PersonDao pDao = new PersonDaoSimpl();
        LocationDao lDao = new LocationDaoSimpl();
        FeatureDao fDao = new FeatureDaoSimpl();
        DataPool dataPool = new DataPoolImpl();
        dataPool.setDao(eDao);
        dataPool.setDao(fDao);
        dataPool.setDao(pDao);
        dataPool.setDao(lDao);

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

        NoCollisionPrinciple ncpInstance = new NoCollisionPrinciple();
        ncpInstance.setRandom(new Random(seed));//123456L
        ncpInstance.setCondition(new AlgorithmConditionTime(seconds));
        ncpInstance.setDataPoolSettings(settings);
        ncpInstance.setStatusBar(new SilentConsoleStatusBar());
        ncpInstance.setDataPool(dataPool);

        loadData(dataPool, new File(fileOrFolder), settings);

        long start = System.currentTimeMillis();
        Solution solution = ncpInstance.doWork();
//        ConstraintChecker.printStatistics(solution);

        return start;
    }

    public void loadData(DataPool dataPool, File fileOrFolder,
            DataPoolSettings settings) throws Exception {

        if (LOAD_BT) {
            ImportBTXml.overwriteSettings(settings);
            ImportBTXml importer = new ImportBTXml(dataPool, fileOrFolder);
            importer.setDataPoolSettings(settings);
            importer.doWork();
        } else {
            InputStreamReader reader = new InputStreamReader(new FileInputStream(fileOrFolder));

            DataPool4Track2 dataPool4Track2 = new DataPool4Track2();
            dataPool4Track2.setSettings(settings);
            dataPool4Track2.setDataPool(dataPool);
            dataPool4Track2.setSource(reader);

            dataPool4Track2.doWork();
        }
    }
}
TOP

Related Classes of de.timefinder.core.RunAlgo

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.