示例#1
0
    def __init__(self, instance, route: List[int], q_init: float, multi_insert: bool = True, check_tri: bool = False):
        """Initiates a Solver for the FRVCP.

        Args:
            instance: either the filename (str) of a problem instance or a
                dictionary containing the required info. Typically the problem
                instance is already in a compliant JSON file. However, if a
                filename ending in ".xml" is passed, it is assumed to be a
                VRP-REP instance, and it will be translated.
            route: A list of integers representing the fixed route to be
                traveled by the vehicle
            q_init: The energy with which the EV begins the route
            multi_insert: A boolean specifying whether the EV is allowed to
                visit multiple CSs between stops in the route
            check_tri: A boolean specifying whether to verify that the triangle
                inequality holds for the instance's time and energy matrices.
                Default is False, since this adds significant computation time.

        """
        # if passed an XML file, attempt to translate it
        if isinstance(instance, (str)) and instance[-4:] == ".xml":
            print("INFO: Passed instance is an XML file. " +
                  "Assuming it is a VRP-REP instance and attempting to translate it...")
            instance = translator.translate(instance)
            print("INFO: Instance translated.")

        self.instance = core.FrvcpInstance(instance, check_tri)
        self._q_init = q_init
        self._route = route
        self._multi_insert = multi_insert

        self.solution = None
示例#2
0
This example follows the "Example Usage" seciton (5.2) from the original research
article describing frvcpy.
That article may be found here:
https://doi.org/10.1287/ijoc.2020.1035

The script translates an instance from the VRP-REP format, solves an FRVCP on the
instance, and writes the solution to file.
"""

from frvcpy.translator import translate
from frvcpy.solver import Solver

if __name__ == "__main__":

    # Translate the VRP-REP instance.
    frvcp_instance = translate("../data/vrprep-instance.xml")

    route = [0, 40, 12, 33, 38, 16, 0]  # route to make energy feasible
    q_init = frvcp_instance["max_q"]  # EV begins with max battery capacity

    # Initialize solver with the instance, route, and initial charge.
    frvcp_solver = Solver(frvcp_instance, route, q_init)

    # Run the algorithm.
    duration, feas_route = frvcp_solver.solve()

    # Write a VRP-REP compliant solution file.
    frvcp_solver.write_solution("../results/my-solution.xml",
                                instance_name="frvcpy-instance")

    print(f"Duration: {duration:.4}")
示例#3
0
    def test_translation(self):
        """Translate an instance, compare it to a known reference"""

        frvcp_instance = translator.translate(self.MANUSCRIPT_INSTANCE)
        assertDeepAlmostEqual(self, frvcp_instance, self.ref_instance, 3)