def test_successor_1(self):
        vehicles = [Vehicle(0)]
        packages = [Package(2, 1)]
        state = State(vehicles, packages)

        # Travel to package at position 2:
        p1 = Package(2, 1)
        p1.setCarried()
        p1.setUnDelivered()
        v1 = Vehicle(2)
        v1.addPackage(p1)
        n1 = State([v1], [])

        self.assertTrue([n1] == state.successor())
 def __init__(self, _m, _n, _k, _y, packs):
     """
     Initializes a problem.
     :param _m: number of vehicles.
     :param _n: number of packages.
     :param _k: capacity of each vehicle.
     :param _y: dimension of the space.
     """
     self.m = _m
     self.n = _n
     self.k = _k
     self.y = _y
     vehicles = HashableDictionary("VEHICLES")
     packages = HashableDictionary("PACKAGES")
     for i in range(_m):
         vehicles[i] = Vehicle(tuple([0 for i in range(_y)]), i, _k)
     for j in range(len(packs)):
         packages[j] = Package(packs[j][0], packs[j][1], j, None)
     self.initState = State(vehicles, packages)
 def test_init(self):
     vehicles = [Vehicle(0)]
     packages = [Package(2, 1)]
     state = State(vehicles, packages)
     self.assertTrue(set(vehicles) == state.vehicles)
     self.assertTrue(set(packages) == state.packages)
 def test_init(self):
     package = Package(0, 0)
     self.assertTrue(package.position == 0)
     self.assertTrue(package.destination == 0)
     self.assertFalse(package.carried == True)
     self.assertFalse(package.delivered == True)
 def test_addPackages(self):
     veh = Vehicle(0)
     p = Package(1, 2)
     veh.addPackage(p)
     self.assertTrue(veh.getPackages() == set([p]))
 def test_setUnDelivered(self):
     package = Package(0, 0)
     package.setUnDelivered()
     self.assertTrue(package.isDelivered() == False)
 def test_setUnCarried(self):
     package = Package(0, 0)
     package.setUnCarried()
     self.assertTrue(package.isCarried() == False)
 def test_setDestination(self):
     package = Package(0, 0)
     package.setDestination(1)
     self.assertTrue(package.getDestination() == 1)
 def test_getDestination(self):
     package = Package(0, 0)
     self.assertTrue(package.getDestination() == 0)
 def test_getPackages(self):
     vehicles = [Vehicle(0)]
     packages = [Package(2, 1)]
     state = State(vehicles, packages)
     self.assertTrue(set(packages) == state.getPackages())
Exemple #11
0
    Instructor: Michael Horsch
    Assignment: 1

    * - all authors equally contributed to the implementation 
"""

v1 = Vehicle((0, ), 0, 2)
v1_copy = Vehicle((0, ), 0, 2)

v3 = Vehicle((0, ), 1, 2)
v3_copy = Vehicle((0, ), 1, 2)

v4 = Vehicle((0, ), 2, 2)
v4_copy = Vehicle((0, ), 2, 2)

p1 = Package((2, ), (3, ), 0, False)
p1_copy = Package((2, ), (3, ), 0, False)

p3 = Package((3, ), (4, ), 1, False)
p3_copy = Package((3, ), (4, ), 1, False)

p4 = Package((6, ), (5, ), 2, False)
p4_copy = Package((6, ), (5, ), 2, False)

p5 = Package((5, ), (4, ), 3, False)
p5_copy = Package((5, ), (4, ), 3, False)

p6 = Package((4, ), (3, ), 4, False)
p6_copy = Package((4, ), (3, ), 4, False)

lv = [v1, v3, v4]
    def successors(self, node):
        """
        Set of possible transitions from the current state.
        :return: list of all possible states.
        """

        possibleSuccessors = []
        for k1, v in node.getState().getVehicles().items():
            for k2, p in node.getState().getPackages().items():

                # Vehicle is not carrying this package and it has no more room:
                if p.carrier() != v.getIndex() and v.getRoom() <= 0:
                    # it can neither pickup this package nor deliver it:
                    continue

                # First, cover the case when you can deliver something
                # For each package picked up by/moving with v:
                else:
                    if p.carrier() == v.getIndex():
                        # Change copied state to reflect a delivery:
                        currVehicle = Vehicle(p.getDestination(),\
                                                v.getIndex(), v.getRoom() + 1)
                        vehicles = node.getState().getVehicles().clone()
                        vehicles[v.getIndex()] = currVehicle

                        # Make sure that no vehicle carries beyond capacity:
                        assert (currVehicle.getRoom() <= self.k)
                        assert (currVehicle.getRoom() >= 0)

                        packages = node.getState().getPackages().clone()
                        packages.pop(p.getIndex())
                        # Append to list of possible states:
                        planStep = "V" + str(v.getIndex()) + " delivers " +\
                            "P" + str(p.getIndex()) + "\n"
                        newNode = SearchNode(State(vehicles, packages), node,
                                             planStep)
                        possibleSuccessors.append(newNode)

                    # If the vehicle can pick up more packages:
                    elif (v.getRoom() > 0) and (p.carrier() is None):
                        # Change copied state to reflect a pick up of package
                        # p by v:
                        currVehicle = Vehicle(p.getPosition(),\
                                                v.getIndex(), v.getRoom() - 1)
                        vehicles = node.getState().getVehicles().clone()
                        vehicles[v.getIndex()] = currVehicle

                        # Make sure that no vehicle carries beyond capacity:
                        assert (currVehicle.getRoom() <= self.k)
                        assert (currVehicle.getRoom() >= 0)

                        # Set package as carried
                        pickUp = Package(p.getPosition(), p.getDestination(),
                                         p.getIndex(), v.getIndex())
                        packages = node.getState().getPackages().clone()
                        packages[p.getIndex()] = pickUp

                        planStep = "V" + str(v.getIndex()) + " picks up " +\
                            "P" + str(p.getIndex()) + "\n"

                        newNode = SearchNode(State(vehicles, packages), node,
                                             planStep)

                        # Append to the list of possible states:
                        possibleSuccessors.append(newNode)

            # Vehicle is empty, an option is to go back to origin:
            if v.getRoom() == self.k\
                    and v.getPosition() != tuple([0 for x in range(self.y)]):
                # Define origin
                garage = tuple([0 for x in range(self.y)])
                currVehicle = Vehicle(garage, v.getIndex(), v.getRoom())
                vehicles = node.getState().getVehicles().clone()
                vehicles[v.getIndex()] = currVehicle
                packages = node.getState().getPackages().clone()
                planStep = "V" + str(v.getIndex()) + " returns to origin\n"
                newNode = SearchNode(State(vehicles, packages), node, planStep)
                # Append state to the possible successor
                possibleSuccessors.append(newNode)
        return possibleSuccessors