All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.drools.planner.examples.traindesign.domain.RailNode Maven / Gradle / Ivy

Go to download

Drools Planner optimizes automated planning by combining metaheuristic search algorithms with rule engine powered score calculation. This is the drools-planner-examples module which contains examples on how to use Drools Planner.

There is a newer version: 6.0.0.Alpha9
Show newest version
/*
 * Copyright 2011 JBoss Inc
 *
 * 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 org.drools.planner.examples.traindesign.domain;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
import com.thoughtworks.xstream.annotations.XStreamOmitField;
import org.apache.commons.lang.builder.CompareToBuilder;
import org.drools.planner.examples.common.domain.AbstractPersistable;
import org.drools.planner.examples.pas.domain.BedDesignation;
import org.drools.planner.examples.traindesign.domain.solver.RailNodeShortestPath;
import org.drools.planner.examples.traindesign.domain.solver.RailPath;

@XStreamAlias("RailNode")
public class RailNode extends AbstractPersistable {

    private String code;
    private int blockSwapCost;

    private List originatingRailArcList;
    @XStreamOmitField
    private Map shortestPathMap;

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public int getBlockSwapCost() {
        return blockSwapCost;
    }

    public void setBlockSwapCost(int blockSwapCost) {
        this.blockSwapCost = blockSwapCost;
    }

    public List getOriginatingRailArcList() {
        return originatingRailArcList;
    }

    public void setOriginatingRailArcList(List originatingRailArcList) {
        this.originatingRailArcList = originatingRailArcList;
    }

    public Map getShortestPathMap() {
        return shortestPathMap;
    }

    public RailNodeShortestPath getShortestPathTo(RailNode other) {
        return shortestPathMap.get(other);
    }

    public void setShortestPathMap(Map shortestPathMap) {
        this.shortestPathMap = shortestPathMap;
    }

    @Override
    public String toString() {
        return code;
    }

    public void initializeShortestPathMap(List railNodeList) {
        shortestPathMap = new HashMap(
                railNodeList.size());
        // Dijkstra algorithm
        List unvisitedShortestPathList = new ArrayList(
                railNodeList.size());

        RailNodeShortestPath originShortestPath = new RailNodeShortestPath();
        originShortestPath.setOrigin(this);
        originShortestPath.setDestination(this);
        originShortestPath.setDistance(0);
        originShortestPath.resetRailPathList();
        RailPath originRailPath = new RailPath(new ArrayList(0));
        originShortestPath.addRailPath(originRailPath);
        shortestPathMap.put(this, originShortestPath);
        unvisitedShortestPathList.add(originShortestPath);

        while (!unvisitedShortestPathList.isEmpty()) {
            RailNodeShortestPath campingShortestPath = unvisitedShortestPathList.remove(0);
            for (RailArc nextRailArc : campingShortestPath.getDestination().getOriginatingRailArcList()) {
                RailNode nextNode = nextRailArc.getDestination();
                int nextDistance = campingShortestPath.getDistance() + nextRailArc.getDistance();

                RailNodeShortestPath nextShortestPath = shortestPathMap.get(nextNode);
                if (nextShortestPath == null) {
                    nextShortestPath = new RailNodeShortestPath();
                    nextShortestPath.setOrigin(this);
                    nextShortestPath.setDestination(nextNode);
                    nextShortestPath.setDistance(Integer.MAX_VALUE);
                    shortestPathMap.put(nextNode, nextShortestPath);
                    unvisitedShortestPathList.add(nextShortestPath);
                }
                if (nextDistance <= nextShortestPath.getDistance()) {
                    if (nextDistance < nextShortestPath.getDistance()) {
                        nextShortestPath.setDistance(nextDistance);
                        nextShortestPath.resetRailPathList();
                    }
                    for (RailPath campingRailPath : campingShortestPath.getRailPathList()) {
                        List railArcList = new ArrayList(campingRailPath.getRailArcList());
                        railArcList.add(nextRailArc);
                        RailPath nextRailPath = new RailPath(railArcList);
                        nextShortestPath.addRailPath(nextRailPath);
                    }
                }
            }
            Collections.sort(unvisitedShortestPathList, new Comparator() {
                public int compare(RailNodeShortestPath a, RailNodeShortestPath b) {
                    return new CompareToBuilder()
                            .append(a.getDistance(), b.getDistance())
                            .append(a.getRailPathList().size(), b.getRailPathList().size())
                            .append(a.getId(), b.getId())
                            .toComparison();
                }
            });
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy