GraphHopper.com | Forum | GitHub | Maps | Blog

VehicleRoutingAlgorithmBuilder in v1.7.2


#1

I am trying to implement my own algorithm for Vehicle Routing in jsprit framework. I am unable to find core.algorithm.VehicleRoutingAlgorithmBuilder class. Where can I find it ?


#2

Hey man @James_Green the new version does not have the VehicleRoutingAlgorithmBuidler i think, I am not sure i am actually a beginner in java.

Here is an alternative, hope it helps.

package com.jspritexample.jspritexample;

import static org.junit.Assert.assertNotNull;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.graphhopper.jsprit.analysis.toolbox.AlgorithmSearchProgressChartListener;
import com.graphhopper.jsprit.analysis.toolbox.GraphStreamViewer;
import com.graphhopper.jsprit.analysis.toolbox.Plotter;
import com.graphhopper.jsprit.analysis.toolbox.Plotter.Label;
import com.graphhopper.jsprit.core.algorithm.AlgorithmUtil;
import com.graphhopper.jsprit.core.algorithm.SearchStrategyManager;
import com.graphhopper.jsprit.core.algorithm.VehicleRoutingAlgorithm;
import com.graphhopper.jsprit.core.algorithm.box.Jsprit;
import com.graphhopper.jsprit.core.algorithm.box.Jsprit.Builder;
import com.graphhopper.jsprit.core.algorithm.box.Jsprit.Parameter;
import com.graphhopper.jsprit.core.algorithm.state.InternalStates;
import com.graphhopper.jsprit.core.algorithm.state.StateManager;
import com.graphhopper.jsprit.core.algorithm.state.StateUpdater;
import com.graphhopper.jsprit.core.problem.Location;
import com.graphhopper.jsprit.core.problem.VehicleRoutingProblem;
import com.graphhopper.jsprit.core.problem.VehicleRoutingProblem.FleetSize;
import com.graphhopper.jsprit.core.problem.constraint.ConstraintManager;
import com.graphhopper.jsprit.core.problem.constraint.HardActivityConstraint;
import com.graphhopper.jsprit.core.problem.constraint.SoftRouteConstraint;
import com.graphhopper.jsprit.core.problem.constraint.ConstraintManager.Priority;
import com.graphhopper.jsprit.core.problem.cost.VehicleRoutingTransportCosts;
import com.graphhopper.jsprit.core.problem.job.Job;
import com.graphhopper.jsprit.core.problem.job.Service;
import com.graphhopper.jsprit.core.problem.misc.JobInsertionContext;
import com.graphhopper.jsprit.core.problem.solution.SolutionCostCalculator;
import com.graphhopper.jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import com.graphhopper.jsprit.core.problem.solution.route.VehicleRoute;
import com.graphhopper.jsprit.core.problem.solution.route.activity.ActivityVisitor;
import com.graphhopper.jsprit.core.problem.solution.route.activity.TourActivity;
import com.graphhopper.jsprit.core.problem.solution.route.activity.TourActivity.JobActivity;
import com.graphhopper.jsprit.core.problem.vehicle.Vehicle;
import com.graphhopper.jsprit.core.problem.vehicle.VehicleImpl;
import com.graphhopper.jsprit.core.problem.vehicle.VehicleType;
import com.graphhopper.jsprit.core.problem.vehicle.VehicleTypeImpl;
import com.graphhopper.jsprit.core.reporting.SolutionPrinter;
import com.graphhopper.jsprit.core.reporting.SolutionPrinter.Print;
import com.graphhopper.jsprit.core.util.Coordinate;
import com.graphhopper.jsprit.core.util.Solutions;
import com.graphhopper.jsprit.core.util.VehicleRoutingTransportCostsMatrix;
import com.graphhopper.jsprit.io.problem.VrpXMLReader;
import com.jspritexample.jspritutils.JsPritUtils;

public class App
{

static class DontAllows2 implements HardActivityConstraint {

	
	@Override
	public ConstraintsStatus fulfilled(JobInsertionContext iFacts, TourActivity prevAct, TourActivity newAct,
			TourActivity nextAct, double prevActDepTime) {
			if(iFacts.getJob().getId().equals("s2")){				
				return ConstraintsStatus.NOT_FULFILLED;
			}else {
				return ConstraintsStatus.FULFILLED;
			}
	}


}



static class RewardAndPenaltiesThroughSoftConstraints {
	
	private VehicleRoutingProblem vrp;

	public RewardAndPenaltiesThroughSoftConstraints(VehicleRoutingProblem vrp) {
		super();
		this.vrp = vrp;
	}
	
	public double getCosts(VehicleRoute route) {
		boolean serves13 = route.getTourActivities().servesJob(getJob("13"));
		boolean serves21 = route.getTourActivities().servesJob(getJob("21"));
		
		if(serves13 && serves21) { //then reward
			return -100.;
		}
		return 0;
	}
	
	private Job getJob(String string) {
		return vrp.getJobs().get(string);
	}

}

public static void main( String[] args ) 
{       


	
	

    VehicleType type = VehicleTypeImpl.Builder.newInstance("type").build();        
    
    Location l0 =  Location.Builder.newInstance().setId("0").setName("Local0").setCoordinate(Coordinate.newInstance(23.1, 22.2)).build();
    Location l1 =  Location.Builder.newInstance().setId("1").setName("Local1").setCoordinate(Coordinate.newInstance(23, 24)).build();
    Location l2 =  Location.Builder.newInstance().setId("2").setName("Local2").setCoordinate(Coordinate.newInstance(21, 25)).build();
    Location l3 =  Location.Builder.newInstance().setId("3").setName("Local3").setCoordinate(Coordinate.newInstance(22.5, 23.5)).build();
    
    VehicleImpl vehicle = VehicleImpl.Builder.newInstance("vehicle")
    		
    		.setStartLocation(l0).setType(type).build();
    		
    Service s1 = Service.Builder.newInstance("s1").setLocation(l1).setServiceTime(30).build();
    Service s2 = Service.Builder.newInstance("s2").setLocation(l2).build();
    Service s3 = Service.Builder.newInstance("s3").setLocation(l3).build();
  
    
    VehicleRoutingTransportCostsMatrix.Builder costMatrixBuilder = VehicleRoutingTransportCostsMatrix.Builder.newInstance(true);

    costMatrixBuilder.addTransportTime("0", "1", 10);
    costMatrixBuilder.addTransportTime("0", "2", 5);
    costMatrixBuilder.addTransportTime("1", "0", 5);
    costMatrixBuilder.addTransportTime("2", "0", 5);
  
  
    costMatrixBuilder.addTransportTime("1", "0", 5);
    costMatrixBuilder.addTransportTime("1", "2", 5);        
    costMatrixBuilder.addTransportTime("0", "1", 5);
    costMatrixBuilder.addTransportTime("2", "1", 5);
    
    
    costMatrixBuilder.addTransportTime("2", "0", 5);
    costMatrixBuilder.addTransportTime("2", "1", 5);        
    costMatrixBuilder.addTransportTime("0", "2", 5);
    costMatrixBuilder.addTransportTime("1", "2", 5);
    
    costMatrixBuilder.addTransportTime("2", "0", 5);
    costMatrixBuilder.addTransportTime("2", "1", 5);        
    costMatrixBuilder.addTransportTime("0", "2", 5);
    costMatrixBuilder.addTransportTime("1", "2", 5);
 
    costMatrixBuilder.addTransportTime("0", "3", 5);
    costMatrixBuilder.addTransportTime("1", "3", 5);        
    costMatrixBuilder.addTransportTime("2", "3", 5);      
    costMatrixBuilder.addTransportTime("3", "1", 5);
    costMatrixBuilder.addTransportTime("3", "2", 5);        
    costMatrixBuilder.addTransportTime("3", "3", 5);

    

    VehicleRoutingTransportCosts costMatrix = costMatrixBuilder.build();


   
    VehicleRoutingProblem vrp = VehicleRoutingProblem.Builder.newInstance().setFleetSize(FleetSize.FINITE).setRoutingCost(costMatrix)
        .addVehicle(vehicle)
        .addJob(s1).addJob(s2).addJob(s3)    
        .build();
    
    /// OK THE UPPER IS SETUP
	
	final StateManager stateManager = new StateManager(vrp);	
	ConstraintManager constraintManager = new ConstraintManager(vrp,stateManager);	
	constraintManager.addConstraint(new DontAllows2(), Priority.CRITICAL);
	
	
	//
	final RewardAndPenaltiesThroughSoftConstraints softConstraintContributionToOverallObjective = new RewardAndPenaltiesThroughSoftConstraints(vrp);
	SolutionCostCalculator costCalculator = new SolutionCostCalculator() {
		
		 @Override
            public double getCosts(VehicleRoutingProblemSolution solution) {
                double costs = 0.0;
                for (VehicleRoute r : solution.getRoutes()) {
                	costs += stateManager.getRouteState(r, InternalStates.COSTS, Double.class);
                	costs += getFixedCosts(r.getVehicle());
                //    costs +=softConstraintContributionToOverallObjective.getCosts(r); 
                }
                costs += solution.getUnassignedJobs().size() * costs * .1;
                return costs;
            }

            private double getFixedCosts(Vehicle vehicle) {
                if (vehicle == null) return 0.0;
                if (vehicle.getType() == null) return 0.0;
                return vehicle.getType().getVehicleCostParams().fix;
            }
	};
	
	
	 
	Builder builder = Jsprit.Builder.newInstance(vrp);
    builder.setObjectiveFunction(costCalculator);
	builder.setStateAndConstraintManager(stateManager,constraintManager);

	builder.setProperty(Parameter.ITERATIONS.toString(), "2000");
	
	VehicleRoutingAlgorithm vra = builder.buildAlgorithm();
	
	
	
	Collection<VehicleRoutingProblemSolution> solutions = vra.searchSolutions();
	
	SolutionPrinter.print(vrp, Solutions.bestOf(solutions), Print.VERBOSE);
	
	
	
	GraphStreamViewer streamViewer =  new GraphStreamViewer(vrp, Solutions.bestOf(solutions));
	streamViewer.labelWith(GraphStreamViewer.Label.ID);
	streamViewer.setRenderDelay(500).display();
	
}

public static String getResourceFileAsString(String fileName) {
    ClassLoader classLoader = ClassLoader.getSystemClassLoader();
    InputStream is = classLoader.getResourceAsStream(fileName);
    if (is != null) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        return reader.lines().collect(Collectors.joining(System.lineSeparator()));
    }
    return null;
}

}

pom.txt (1.9 KB)

Here is my pom file for future references