Beispiel #1
0
def pseudo_cost_function(initial_pos, desired_shape, n):
    k = [[0 for j in range(n)] for i in range(n)]

    for i in range(n):
        for j in range(n):
            x = -np.transpose(initial_pos[i]).dot(np.array(desired_shape[j]))
            k[i][j] = x

    hungarian = Hungarian(k)
    hungarian.calculate()
    x_star = hungarian.get_results()
    k_star = hungarian.get_total_potential()

    return [x_star, k_star]
Beispiel #2
0
 def calc(state, cache):
     if 'hungarian' not in cache:
         cache['hungarian'] = {}
     player = state.getPlayerPosition()
     boxes = state.getBoxes()
     targets = state.getTargets()
     key = (",".join([str(x[0]) + "-" + str(x[1]) for x in boxes]),
            ",".join([str(x[0]) + "-" + str(x[1]) for x in targets]))
     total = 0
     if key in cache['hungarian']:
         total = cache['hungarian'][key]
     else:
         distance_list = []
         for b in boxes:
             distance_list.append([method(b, t) for t in targets])
         if len(distance_list) is 0:
             return 1
         array = np.array(distance_list, dtype='float64')
         hungarian = Hungarian(array)
         hungarian.calculate()
         total = hungarian.get_total_potential()
         cache['hungarian'][key] = total
     total += sum([method(player, b) for b in boxes] or [0])
     return total
Beispiel #3
0
desired_shape.append([x, y])
x = 500
y = 200
desired_shape.append([x, y])

k = [[0 for j in range(n)] for i in range(n)]

for i in range(n):
    for j in range(n):
        x = -np.transpose(initial_pos[i]).dot(np.array(desired_shape[j]))
        k[i][j] = x

hungarian = Hungarian(k)
hungarian.calculate()
x_star = hungarian.get_results()
k_star = hungarian.get_total_potential()

# initialize root Window and canvas
root = Tk()
root.title("Balls")
root.resizable(True, True)
canvas = Canvas(root, width=800, height=800)
canvas.pack()

# create two ball objects and animate them
balls = []
color = [
    "red", "green", "black", "orange", "blue", "yellow", "purple", "grey",
    "brown", "magenta"
]
Beispiel #4
0
# basing on Dijkstra

print("\nSame number of ambulances and patients")
# Distances from ambulance to patient + from patient to nearest hospital
distances = np.array(
[      # P0 P1 P2 P3
        [4, 5, 11, 2], # A0
        [12, 3, 7, 8], # A1
        [9, 15, 5, 4], # A2
        [2, 4, 17, 6]  # A3
])
hungarian = Hungarian()
hungarian.calculate(distances)
print(distances)
solution = hungarian.get_results()
print("Cost",hungarian.get_total_potential())
for amb in range(distances.shape[0]):
    line = "[ "
    for node in range(distances.shape[1]):
        if (amb, node) in solution:
            line += str(distances[amb, node]) + "  "
        else:
            line += "-  "
    print(line+"]")


print("\nMore ambulances than patients")
# Distances from ambulance to patient + from patient to nearest hospital
# or from ambulance to centroid
distances = np.array(
[      # P0 P1 C0 C1