BrunoRosendo / master-thesis

Source code for my master's thesis, in the topic "Quantum algorithms for optimizing urban transportation"
MIT License
6 stars 0 forks source link

Add pickup and delivery to math formulation #22

Closed BrunoRosendo closed 5 months ago

BrunoRosendo commented 6 months ago

this seems tricky for the same cap model, since the vehicles are not tracked. Maybe it's enough to use a u for each vehicle?

I need to search for formulations, to try and not increase the number of variables too much

BrunoRosendo commented 6 months ago

Architecture-wise, I think it's too much to do subclasses, so maybe I just use if statements for pickups

BrunoRosendo commented 6 months ago

check this, but not sure if it helps https://arxiv.org/pdf/1606.01935.pdf

BrunoRosendo commented 6 months ago

this paper describes the ride pooling problem. It's slightly different but might make sense for my application https://www.researchgate.net/publication/366190303_Modeling_routing_problems_in_QUBO_with_application_to_ride-hailing

BrunoRosendo commented 6 months ago

consider this shit, might save a lot of variables. Instead of using a 3D matrix with vehicle index:

If successful, consider using the same technique for the diff cap model

BrunoRosendo commented 6 months ago

this paper seems to have a nice formulation for pickup and deliveries https://core.ac.uk/download/pdf/19477982.pdf

BrunoRosendo commented 6 months ago

this one has a lot of great formulations https://pdf.sciencedirectassets.com/271709/1-s2.0-S0305054820X00074/1-s2.0-S0305054820301040/main.pdf?X-Amz-Security-Token=IQoJb3JpZ2luX2VjEIn%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCXVzLWVhc3QtMSJHMEUCIGQ6ZgchW0NmNshOJvxHrb6bs0v6fRqbVpelCiM%2FrEVXAiEA5gNDx%2Bx6KSVY13a4biF4GOz7jIcHFzuiwCNxJ101AC8qvAUIkf%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FARAFGgwwNTkwMDM1NDY4NjUiDL4U%2F0RClbC854XDYiqQBbZXGA5aVrNwCrzKSgPpKCa7Lla9hM9WdogIhup48GBoXLecI0et7ms6M%2Fd%2BTQ1UQo%2BT1XUxpf1YnQTr0RMhBfH2FMkHoymC9S4lz%2BUO3ygaTGptw0pNeGa92VLTkfx18WNcKUec7Z94VJ9mmY30yGW68dqrNw6%2B18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aders=host&X-Amz-Expires=300&X-Amz-Credential=ASIAQ3PHCVTYT3SON56T%2F20240315%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Signature=fabb30c2f9c962ae05bc7d865b768d5366c6c0c48a0942edcab577c2f120161f&hash=99ec0633b312e9223b36651a607b9468856a40a5b252abb0c6c34bdce3fd5d4f&host=68042c943591013ac2b2430a89b270f6af2c76d8dfd086a07176afe7c76c2c61&pii=S0305054820301040&tid=spdf-c2de9e5f-b3e6-4966-889a-f7aa90548f4d&sid=166b5a7b1ae9f3496389970562f078f2d007gxrqb&type=client&tsoh=d3d3LnNjaWVuY2VkaXJlY3QuY29t&ua=0a165e565a065d5d53&rr=864e29145f9294ef&cc=pt

BrunoRosendo commented 6 months ago

this one has a lot of great formulations https://pdf.sciencedirectassets.com/271709/1-s2.0-S0305054820X00074/1-s2.0-S0305054820301040/main.pdf?X-Amz-Security-Token=IQoJb3JpZ2luX2VjEIn%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCXVzLWVhc3QtMSJHMEUCIGQ6ZgchW0NmNshOJvxHrb6bs0v6fRqbVpelCiM%2FrEVXAiEA5gNDx%2Bx6KSVY13a4biF4GOz7jIcHFzuiwCNxJ101AC8qvAUIkf%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FARAFGgwwNTkwMDM1NDY4NjUiDL4U%2F0RClbC854XDYiqQBbZXGA5aVrNwCrzKSgPpKCa7Lla9hM9WdogIhup48GBoXLecI0et7ms6M%2Fd%2BTQ1UQo%2BT1XUxpf1YnQTr0RMhBfH2FMkHoymC9S4lz%2BUO3ygaTGptw0pNeGa92VLTkfx18WNcKUec7Z94VJ9mmY30yGW68dqrNw6%2B18kRvn3W%2BjsK9APYpO4Fp84GEMyvdoE%2BZPTXerpi7PTE0GZzkmwX8MqkfkTLjttgPCy3vjGBL3zgTXLm6iNzIOpEq6HSK%2BbgIMv5Z4xzG1R6HKBKyq8%2FhGvq5Xz3%2FMaEiN41rKvdN6O8b0sWWXNgwHL2amcX0vHyuSPML3Kpc3Pw3fRkLVdZmBzWX9nlTJWsVFmjI2mKh43mxlVsqe3GXgmaESS9zAMzBikCL93rm3CERqbITTg9Cv4rskb3n5xwRTjY%2FXCAsk4HH%2BLcC0mJHC86VsDOd8a13MXWIVh1869VDqqDIWCwauRV%2BJmmaV8GKGm9GIwqPJfkmf7GTrxUVN3lu5p2GbUHb1ybpzKRM2XxGYSAE3kOPS10UfmDuHGdNss8AbaYx361i5j20lPqcMXLr%2FTxc4rpMRv0Vb79H0rvRrAJKsQZwNFvJCoD88QBDtWmXkdJL00IXEtI1ZSmqpyl%2FmNKtBcDatoQ8E%2F2Yvc4bryP4G%2Bd1c3IIHtvpbe5WmSiYfv8XLF%2F5XGF1c44W7yXt3QMW3NeMvHqiBwyoCc2rOBtFvxpmnZ8cpMffb7f4svmdMGSv%2ByHCF1yMo%2BxfZdgIv2BGR4uDgXZVdOj%2FoUHPu7uqIy3HbCFBeD4xojJAmAG6MjcsOtdtbIdCAtZ39S5dKbpRLzAtpLzoPr6venoVRcE5yGZI6ff4ArwyWJf5WhRhS4bqw%2FXMK%2Fk0a8GOrEBIrmHxxWpz40HfxDRvIlUE2CkhwmZvifS9K9XRxdEyhpImUmjhYRS8m2rd2pvuzZJkVOb3hjPfxVixirA5rLlDcsxupyzgiVHxHgPh5hxVF59g8JF5cqfDSIr8oPcTwH3DEtPfhWORW4WMnRIXMC%2BqioTGplrqcod2VuDBOgRXaMJ6b6JnDW%2F242v5LWzgmmLk%2FZYr7ilPNf0A2jOmOyM2JBesvSFiketInHL4e2GLcPQ&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20240315T171803Z&X-Amz-SignedHeaders=host&X-Amz-Expires=300&X-Amz-Credential=ASIAQ3PHCVTYT3SON56T%2F20240315%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Signature=fabb30c2f9c962ae05bc7d865b768d5366c6c0c48a0942edcab577c2f120161f&hash=99ec0633b312e9223b36651a607b9468856a40a5b252abb0c6c34bdce3fd5d4f&host=68042c943591013ac2b2430a89b270f6af2c76d8dfd086a07176afe7c76c2c61&pii=S0305054820301040&tid=spdf-c2de9e5f-b3e6-4966-889a-f7aa90548f4d&sid=166b5a7b1ae9f3496389970562f078f2d007gxrqb&type=client&tsoh=d3d3LnNjaWVuY2VkaXJlY3QuY29t&ua=0a165e565a065d5d53&rr=864e29145f9294ef&cc=pt

This has 2 great formulations worth considering for same capacity:

BrunoRosendo commented 6 months ago

The approach I'm following for same cap is explained in detail in this paper https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2266430

BrunoRosendo commented 6 months ago

for no cap, try using the same approach without upper limit for aux vars, and try using a high constant (N?) instead of Q

BrunoRosendo commented 6 months ago

for diff cap, I can follow a similar approach to without PD. Instead, I can adapt the pickup-delivery constraints and still use the 3D matrix for capacity constraints

BrunoRosendo commented 5 months ago

CVRPSPD IS NOT the problem Im looking for, that's pickup and deliveries of homogeneous goods, not trips...

This is the failed model

from src.model.cplex.cvrp.ConstantCVRP import ConstantCVRP

class ConstantCVRPPD(ConstantCVRP):
    """
    A class to represent a CPLEX math formulation of the CVRP model with pickups and deliveries
    with all vehicles having the same capacity.

    Attributes:
        num_vehicles (int): Number of vehicles available.
        capacity (int): Capacity of each vehicle.
        trips (list): List of tuples, where each tuple contains the pickup and delivery locations, and the amount of customers for a trip.
        depot (int): Index of the depot, which is the starting and ending point for each vehicle.
        distance_matrix (list): Matrix with the distance between each pair of locations.
        locations (list): List of coordinates for each location.
        use_deliveries (bool): Whether the problem uses deliveries or not (always yes).
        cplex (Model): CPLEX model for the CVRP
        simplify (bool): Whether to simplify the problem by removing unnecessary variables.
    """

    def create_vars(self):
        """
        Create the variables for the CPLEX model.
        """

        self.x = self.cplex.binary_var_matrix(
            self.num_locations, self.num_locations, name="x"
        )

        # amount of load after visiting customer
        self.p = self.cplex.integer_var_list(self.num_locations, name="l")

        # amount of load that has to be delivered to location i and to all other following locations
        self.d = self.cplex.integer_var_list(self.num_locations, name="d")

    def create_constraints(self):
        """
        Create the constraints for the CPLEX model.
        """

        self.create_location_constraints()
        self.create_vehicle_constraints()
        self.create_pickup_delivery_constraints()

    def create_pickup_delivery_constraints(self):
        """
        Create the pickup and delivery constraints for the CPLEX model.
        It also takes care of capacity constraints and subtour elimination.
        """

        for i in range(self.num_locations):
            for j in range(1, self.num_locations):
                self.cplex.add_constraint(
                    self.d[i]
                    >= self.d[j]
                    + self.get_location_delivery(i)
                    - self.capacity * (1 - self.x[i, j]),
                )

            self.cplex.add_constraint(self.d[i] >= self.get_location_delivery(i))
            self.cplex.add_constraint(self.d[i] <= self.capacity)

        for i in range(1, self.num_locations):
            for j in range(1, self.num_locations):
                self.cplex.add_constraint(
                    self.p[j]
                    >= self.p[i]
                    + self.get_location_demand(j)
                    - self.capacity * (1 - self.x[i, j])
                )

            self.cplex.add_constraint(self.p[i] >= self.get_location_pickup(i))
            self.cplex.add_constraint(self.p[i] <= self.capacity)
            self.cplex.add_constraint(
                self.p[i] >= self.d[i] + self.get_location_demand(i)
            )
BrunoRosendo commented 5 months ago

consider this shit, might save a lot of variables. Instead of using a 3D matrix with vehicle index:

* Mantain 2D for i and j (x) for the edges and add another 2D for i and k (y). That way, we have the order and the vehicle assignment

* Constraint 1: yik = yjk for each pickup i->j (need to make some connection to xij too)

* Constraint 2: ui < uj for each pickup i->j, to guarantee the right order

If successful, consider using the same technique for the diff cap model

let's try to use this one again. With the new variable list (y list does not contain depot): every trip (i, j) every k -> yik = yjk every i every j every k -> xij yik = xij yjk substitute all y0k for 1

This is not right, all vehicles would follow all routes Also, I still need the u constraint try to reduce constraints, but I don't think it's possible

BrunoRosendo commented 5 months ago

I tried to implement and there's an open branch. However, it was not successful and I gave up because it would also have too many variables. I think I should instead focus on using quantum algorithms with the simplest problem and then explore RPP, as explained in #26