graphhopper / jsprit

jsprit is a java based, open source toolkit for solving rich vehicle routing problems
https://www.graphhopper.com/open-source/
Apache License 2.0
1.64k stars 605 forks source link

implementing time dependent constraints #557

Open Omarelfarouk90 opened 9 months ago

Omarelfarouk90 commented 9 months ago

Hello Jsprit team i was wondering how can i implement a time dependent constraint that muitiply the arrival time with a factor during rush hour

attached is the code

# STEP 3- Run the problem and implement the solution using fast cost matrix build

costMatrix = costMatrixBuilder.build();

costsMatrix = costMatrixBuilder.build();

vrpbuilder.setRoutingCost(costMatrix);

vrpbuilder.addVehicle(vehicle1).addVehicle(vehicle2).addVehicle(vehicle3).addVehicle(vehicle4).addVehicle(vehicle5).addVehicle(vehicle6).addVehicle(vehicle7).addVehicle(vehicle8).addVehicle(vehicle9).addVehicle(vehicle10);

vrp = vrpbuilder.build()

algoBuilder = Jsprit.Builder.newInstance(vrp);

algoBuilder.addCoreStateAndConstraintStuff(True);

StateManager=jpype.JClass("com.graphhopper.jsprit.core.algorithm.state.StateManager")

stateManager =StateManager(vrp);

constraintManager = ConstraintManager(vrp, stateManager)

HardActivityConstraint=jpype.JClass("com.graphhopper.jsprit.core.problem.constraint.HardActivityConstraint")
#implementing constrain to the model
constraintManager.addConstraint(lambda iFacts, prevAct, newAct, nextAct, prevActDepTime:
    ConstraintsStatus.NOT_FULFILLED if prevActDepTime == prevActDepTime + costsmatrix.get_transport_time(
        prevAct.get_location(), newAct.get_location(), prevActDepTime, None, None) else
    ConstraintsStatus.NOT_FULFILLED if prevActDepTime + newAct.get_operation_time() == prevActDepTime + costsmatrix.get_transport_time(
        newAct.get_location(), nextAct.get_location(), prevActDepTime + newAct.get_operation_time(), None, None) else
    ConstraintsStatus.FULFILLED, ConstraintManager.Priority.HIGH)

algoBuilder.setStateAndConstraintManager(stateManager, constraintManager);

algoBuilder.setProperty(Jsprit.Parameter.MAX_TRANSPORT_COSTS, "50000");

algo = algoBuilder.buildAlgorithm()

# Print the solution progress as function of the iteration, added before the solution
algo.getAlgorithmListeners().addListener(AlgorithmSearchProgressChartListener(os.path.join(config.OUTPUT_DIRECTORY, "solution_progress.png")))

algo.setMaxIterations(50)

#solve the vehicle routing algorithm
solutions = algo.searchSolutions();

# Retrieve the best solution
bestSolution = SelectBest().selectSolution(solutions)

print(algo)

print(bestSolution)

# Create a PrintWriter that writes directly to a file
with PrintWriter(File(os.path.join(config.OUTPUT_DIRECTORY, "problemsolve.txt"))) as writer:
            SolutionPrinter.print_(writer, vrp, bestSolution, SolutionPrinter.Print.VERBOSE) # Using the corrected print_ method

note, i am importing the libraries on google colab and running the Java script on python using Jpype