def decision(image): [label, score] = requestLabel( image) # Output of API converted into a label and a score print("Oooooh that's a", label, ". I'm", score, "percent sure!") params = memory.getValue( label ) # Array of paramaters [alpha, beta, survival, ex alpha, ex beta] # return alpha = coward coefficient; beta = aggressor coefficient alpha = params[0] beta = params[1] adjustment = params[2] return alpha, beta, label, adjustment
def error(totalTime, label): # We calculate our error based on the change in Survival Time = Duration 1000 sec # if survival time decreases # error = (old time - new time)/total # if error is -ve --> Positive stimulus (Survival Time Has Increased) # increase beta and decrease alpha # if error is +ve --> Negative stimulus (Survival Time Has Decreased) # increase alpha and decrease beta params = memory.getValue(label) delta = -1 * params[2] survivalAdjustment = (delta) / (totalTime) # if delta = -100 and totalTime = 1000 error should be 0.1 return survivalAdjustment, delta
def update_positions(): global imagex, imagey, survival_time, E, alpha, beta, index, ABL, imgplot, currenttime, survivalprogress, timeprogress, recentindex sr.x = bug.x + sr.x_disp * sin(bug.angle) + sr.y_disp * cos(bug.angle) sr.y = bug.y + -sr.x_disp * cos(bug.angle) + sr.y_disp * sin(bug.angle) sl.x = bug.x + sl.x_disp * sin(bug.angle) + sl.y_disp * cos(bug.angle) sl.y = bug.y - sl.x_disp * cos(bug.angle) + sl.y_disp * sin(bug.angle) # sr.x = bug.x + sr.x_disp*cos(bug.angle-pi/2) - sr.y_disp*sin(bug.angle-pi/2) # sr.y = bug.y + sr.x_disp*sin(bug.angle-pi/2) + sr.y_disp*cos(bug.angle-pi/2) # # sl.x = bug.x + sl.x_disp*cos(bug.angle-pi/2) - sl.y_disp*sin(bug.angle-pi/2) # sl.y = bug.y + sl.x_disp*sin(bug.angle-pi/2) + sl.y_disp*cos(bug.angle-pi/2) # totalTime = duration/ms totalTime = 1000 index = distances(ABL, bugx, bugy) imagex = ABL[index][4] imagey = ABL[index][5] apiradius = 1500 contactradius = 50 if ((bug.x - imagex)**2 + (bug.y - imagey)**2) > apiradius: if ABL[index][6] == 1: ABL[index][6] = 0 E = -80 sr.E = E sl.E = E sbr.E = E sbl.E = E if ((bug.x - imagex)**2 + (bug.y - imagey)**2) <= apiradius and ( (bug.x - imagex)**2 + (bug.y - imagey)**2) >= contactradius: # ABL[index].append(1) if ABL[index][6] == 0 and recentindex != index: ABL[index][6] = 1 recentindex = index [alpha, beta, label, adjustment] = decision(image[index]) img = Image.open(image[index]) if imgplot != 1: imgplot.remove() plt.subplot(122) imgplot = plt.imshow(img) plt.axis('off') syn_ll_c.g_synmax = g_synmaxval * alpha syn_rr_c.g_synmax = g_synmaxval * alpha syn_ll_a.g_synmax = g_synmaxval * beta syn_rr_a.g_synmax = g_synmaxval * beta # print("The alpha value before backpropagation is", memory.bug_memory[label][0]) #print alpha and beta before adjustment # print("The beta value before backpropagation is", memory.bug_memory[label][1]) print("The object is within", apiradius) if ABL[index][0] > 0.5: E = 0 elif ABL[index][0] < 0.5: E = -80 sr.E = E sl.E = E sbr.E = E sbl.E = E print("The E value is", E) if ((bug.x - imagex)**2 + (bug.y - imagey)**2) < contactradius: ABL[index][6] = 0 [alpha, beta, label, adjustment] = decision(image[index]) img = Image.open(image[index]) if imgplot != 1: imgplot.remove() plt.subplot(122) imgplot = plt.imshow(img) plt.axis('off') syn_ll_c.g_synmax = g_synmaxval * alpha syn_rr_c.g_synmax = g_synmaxval * alpha syn_ll_a.g_synmax = g_synmaxval * beta syn_rr_a.g_synmax = g_synmaxval * beta print("The alpha value before backpropagation is", memory.bug_memory[label] [0]) #print alpha and beta before adjustment print("The beta value before backpropagation is", memory.bug_memory[label][1]) [survivalAdjustment, delta] = error(totalTime, label) memory.adjustValue(label, survivalAdjustment) ABL[index][0] = memory.bug_memory[label][0] ABL[index][1] = memory.bug_memory[label][1] print("The alpha value after backpropagation is", memory.bug_memory[label] [0]) #print alpha and beta after adjustment print("The beta value after backpropagation is", memory.bug_memory[label][1]) print(memory.getValue(label)) syn_ll_c.g_synmax = g_synmaxval * memory.bug_memory[label][0] syn_rr_c.g_synmax = g_synmaxval * memory.bug_memory[label][0] syn_ll_a.g_synmax = g_synmaxval * memory.bug_memory[label][1] syn_rr_a.g_synmax = g_synmaxval * memory.bug_memory[label][1] if ABL[index][0] > 0.5: E = 0 elif ABL[index][0] < 0.5: E = -80 sr.E = E sl.E = E sbr.E = E sbl.E = E print("The E value is", E) #update_decision(label) # Implement ud() survival_time -= delta # Get Current clock time survivalprogress.append(survival_time) timeprogress.append(time.time() - currenttime) print("The survival time is", survival_time, "and has changed by", -1 * delta, "during this iteration\n") # if survival_time <= 0: # print('Elapsed Time: ' , time.time() - currenttime, 'seconds') # plt.figure(2) # plot(timeprogress, survivalprogress) imagex = randint(-map_size + 10, map_size - 10) imagey = randint(-map_size + 10, map_size - 10) ABL[index][4] = imagex ABL[index][5] = imagey # else: # E=0 # sr.E = E # sl.E = E # sbr.E = E # sbl.E = E if (bug.x < -map_size): bug.x = -map_size bug.angle = pi - bug.angle if (bug.x > map_size): bug.x = map_size bug.angle = pi - bug.angle if (bug.y < -map_size): bug.y = -map_size bug.angle = -bug.angle if (bug.y > map_size): bug.y = map_size bug.angle = -bug.angle sr.imagex = imagex sr.imagey = imagey sl.imagex = imagex sl.imagey = imagey