Apr 3rd, 2022
1,099
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import math
2. from functools import reduce
3. import numpy
4. import json
5.
6. initial_state = {
7.     "state": [3, 3, 0, 0],
8.     "prev": [],
9.     "distanceH": 0,
10.     "boat": 0,
11. }
12.
13. final_state = {
14.     "state": [0, 0, 3, 3],
15.     "prev": [],
16. }
17.
18. possibilities = [
19.     [1, 0],
20.     [0, 1],
21.     [2, 0],
22.     [0, 2],
23.     [1, 1],
24. ]
25.
26.
27. def resolveDeslocament(state, possibility):
28.     missionariesLeft = state.get("state")[0]
29.
30.     cannibalsLeft = state.get("state")[1]
31.
32.     missionariesRight = state.get("state")[2]
33.     cannibalsRight = state.get("state")[3]
34.
35.     numOfMissionariesLeft = 0
36.     numOfCannibalsLeft = 0
37.
38.     numOfMissionariesRight = 0
39.     numOfCannibalsRight = 0
40.
41.     if state.get("boat") == 0:
42.         if possibility[0] > missionariesLeft or possibility[1] > cannibalsLeft:
43.             return
44.         numOfMissionariesLeft = missionariesLeft - possibility[0]
45.         numOfCannibalsLeft = cannibalsLeft - possibility[1]
46.         numOfMissionariesRight = missionariesRight + possibility[0]
47.         numOfCannibalsRight = cannibalsRight + possibility[1]
48.     else:
49.         if possibility[0] > missionariesRight or possibility[1] > cannibalsRight:
50.             return
51.         numOfMissionariesLeft = missionariesLeft + possibility[0]
52.         numOfCannibalsLeft = cannibalsLeft + possibility[1]
53.         numOfMissionariesRight = missionariesRight - possibility[0]
54.         numOfCannibalsRight = cannibalsRight - possibility[1]
55.
56.     invalidLeft = numOfMissionariesLeft < numOfCannibalsLeft and numOfMissionariesLeft != 0
57.     invalidRight = numOfCannibalsRight < numOfMissionariesRight and numOfCannibalsRight != 0
58.
59.     if (invalidLeft or invalidRight):
60.         return
61.
62.     new_state = [
63.         numOfMissionariesLeft,
64.         numOfCannibalsLeft,
65.         numOfMissionariesRight,
66.         numOfCannibalsRight,
67.     ]
68.
69.     if state.get("boat") == 0:
70.         side = 1
71.     else:
72.         side = 0
73.
74.     data = {
75.         "state": new_state,
76.         "prev": state,
77.         "distanceH": None,
78.         "boat": side,
79.     }
80.     print("DATA", data)
81.     return data
82.
83.
84. def calculateDistEuc(arrX, arrY):
85.     sumValues = []
86.     dist = math.dist(arrX, arrY)  # calculo euclidiano
87.     sumValues.append(dist)
88.     sum = reduce(lambda x, y: x + y, sumValues)
89.     return sum
90.
91.
92. def main(currentState):
93.     if(currentState.get("state") == final_state.get("state")):
94.         return
95.
96.     deslocaments = []
97.     for possibility in possibilities:
98.         deslocament = resolveDeslocament(currentState, possibility)
99.         if deslocament != None:
100.             deslocaments.append(deslocament)
101.
102.     if len(deslocaments) == 0:
103.         main(currentState.get("prev"))
104.         return
105.
106.     minValueState = {}
107.     minValue = 0
108.     distanceH = 0
109.     distanceG = 0
110.
111.     for idx in deslocaments:
112.         distanceH = calculateDistEuc(
113.             idx.get("state"), currentState.get("state"))
114.         distanceG = calculateDistEuc(
115.             final_state.get("state"), idx.get("state"))
116.         totalDist = distanceH + distanceG
117.         if idx == 0:
118.             minValue = totalDist
119.             minValueState = deslocaments[idx]
120.         if totalDist < minValue:
121.             minValue = totalDist
122.             minValueState = deslocaments[idx]
123.         minValueState["distanceH"] = distanceH
124.     return
125.
126.
127. main(initial_state)
128.