Package de.timefinder.algo.constraint

Source Code of de.timefinder.algo.constraint.MinGapsConstraint

/*
*  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.algo.constraint;

import de.timefinder.data.algo.DataPoolSettings;
import de.timefinder.data.Event;
import de.timefinder.data.algo.Assignment;
import de.timefinder.data.algo.Constraint;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

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

    private transient List<Assignment> assignments = new ArrayList<Assignment>();
    private transient DataPoolSettings settings;
    private Collection<Event> events;
    private boolean countEarly;
    private Comparator<Assignment> comparator = new Comparator<Assignment>() {

        @Override
        public int compare(Assignment o1, Assignment o2) {
            int start1 = o1.getStart();
            int start2 = o2.getStart();
            if (start1 > start2)
                return 1;
            else if (start2 > start1)
                return -1;
            else
                return 0;
        }
    };

    public MinGapsConstraint(DataPoolSettings settings, Collection<Event> events) {
        this.settings = settings;
        this.events = events;
    }

    public boolean isCountEarly() {
        return countEarly;
    }

    public Collection<Event> getEvents() {
        return events;
    }

    @Override
    public void transformEvents(Map<Event, Assignment> map) {
        for (Event ev : events) {
            Assignment as = map.get(ev);
            if (as == null)
                throw new NullPointerException("Cannot find assignment for:" + ev);

            assignments.add(as);
        }
    }

    public void setCountEarly(boolean b) {
        countEarly = b;
    }

    @Override
    public int getViolations(Object o) {
        return getViolations();
    }

    @Override
    public int getViolations() {
        if (assignments.size() == 0)
            return 0;

        // we assume non-overlapping assignments
        Collections.sort(assignments, comparator);
        int gaps = 0;
        int slotsPerDay = settings.getTimeslotsPerDay();
        int oldDay = -1;
        int oldEnd = 0;

        for (Assignment ass : assignments) {
            if (ass.getStart() < 0)
                continue;

            int tmpDay = ass.getStart() / slotsPerDay;
            if (oldDay != tmpDay) {
                oldDay = tmpDay;
                if (countEarly)
                    gaps += ass.getStart() - tmpDay * slotsPerDay;
            } else
                gaps += ass.getStart() - oldEnd;

            oldEnd = ass.getStart() + ass.getEvent().getDuration();
        }
        if (gaps < 0)
            throw new IllegalStateException("Assignments cannot overlap!");

        return (int) (gaps * getWeight());
    }
}
TOP

Related Classes of de.timefinder.algo.constraint.MinGapsConstraint

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.