Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

implementing time dependent constraints #557

Open
Omarelfarouk90 opened this issue Feb 17, 2024 · 0 comments
Open

implementing time dependent constraints #557

Omarelfarouk90 opened this issue Feb 17, 2024 · 0 comments

Comments

@Omarelfarouk90
Copy link

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

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant