def main(): T = float(input("Enter Temperature: ")) Size = int(input("Enter Box Dimension: ")) Steps = int(input("Enter Number of steps: ")) Density = float(input("Enter Density (0<1): ")) simulation()
def parallelizeS(fcm, stabilizers): sim = simulation(fcm) for key in stabilizers: sim.stabilize(key, stabilizers[key]) #simulate until 10000 steps or stability reached sim.steps(10000) sim.changeTransferFunction(lambda x: 1 / (1 + math.exp(-x))) values = sim.run() return values
def hebbian_learning(fcm, restraints, stabilizers, transferFunct, neu, k=maxsize): #get error checking on all parameters... limit = k count = 0 oldValues = [] #list for values of concepts done = False nodeOrder = fcm._fcm_graph.nodes() edgeMatrix = nx.to_numpy_matrix( fcm._fcm_graph ) #edge matrix not transposed(input to concept in the column) #get the list of values in node order for node in nodeOrder: oldValues.append(fcm.concepts()[node]) shape = edgeMatrix.shape #get dimensions of the matrix numRows = shape[0] numCols = shape[1] stableConcepts = stable_concepts( fcm ) #find any concepts that have no input vectors(equation does not maintain edges it seems) while count < limit: if count == 0: oldConceptValues = fcm.concepts( ) #get concepts before changes to compare to loop break else: oldConceptValues = newConceptValues for row in range( 0, numRows ): #call equation to update edge values for each value in edgematrix #for col in range(0,numCols):#column is target #pass edge to edgeupdate newRow = updateEdge( edgeMatrix[row], oldValues[row], oldValues, neu ) #pass oldvalues source as a single digit as it wont change. send targets as list to work through with row #set new values in edge matrix edgeMatrix[row] = newRow #calculate new concempt values:refer to update nodes in simulation method. will only need slight modifications newValues = updateConcepts(edgeMatrix, oldValues, transferFunct, stableConcepts) #set values of fcm to the new values from the equation index = 0 for node in fcm._fcm_graph.nodes(): #keep things in node order fcm.set_value(node, newValues[index]) index += 1 #set the edge strengths to the new edge values for row in range(0, numRows): for col in range(0, numCols): if edgeMatrix.item((row, col)) == 0: continue # print("gds:",fcm._fcm_graph.nodes()[row], fcm._fcm_graph.nodes()[col]) fcm.add_edge( list(fcm._fcm_graph.nodes())[row], list(fcm._fcm_graph.nodes())[col], edgeMatrix.item((row, col))) #now start a simultion with fcm sim = simulation(fcm) for key in stabilizers: sim.stabilize(key, stabilizers[key]) #simulate til 10000 steps or stability reached sim.steps(100) sim.changeTransferFunction(transferFunct) ValuesList = sim.run( ) #run simuation. returns a dict of final values as well as print them newConceptValues = ValuesList[-1] # print (newConceptValues, "New values are here") #if the values returned from the simulation are in the desired range, and #the change is less than the stabilization threshold used in the simulationthen exit the loop # print ("restraints:",restraints) # print ("stabilizers:",stabilizers) # print ("oldConceptValues:",oldConceptValues) for key in restraints: if newConceptValues[key] > restraints[key][0] and newConceptValues[key] < restraints[key][1] and \ (abs(newConceptValues[key]-oldConceptValues[key])) < stabilizers[key]: done = True else: done = False break #learning not done. continue in main loop if done: break #else continue in loop count += 1 return edgeMatrix
def main(): app_running = True sporadicspawn = False selectedbugbox = None framerate = False clock = pygame.time.Clock() FPS = 30 display = True debug = False background = True selectedbug = None stats_bugnumberR = [] stats_bugnumberG = [] stats_bugnumberB = [] stats_foodammount = [] stats_repr = [0, 0] #gera uma simulacao sim = simulation() sim.display() #Main Loop while app_running == True: if sim.step % 200 == 0: Rbugs = 0 Gbugs = 0 Bbugs = 0 for i in range(len(sim.bugs)): if np.argmax(sim.bugs[i].color) == 0: Rbugs += 1 elif np.argmax(sim.bugs[i].color) == 1: Gbugs += 1 elif np.argmax(sim.bugs[i].color) == 2: Bbugs += 1 stats_bugnumberR.append(Rbugs) stats_bugnumberG.append(Gbugs) stats_bugnumberB.append(Bbugs) stats_foodammount.append( np.sum(sim.environment[0] + sim.environment[1] + sim.environment[2]) / 10000) stats_repr.append(sim.reproductions) sim.reproductions = 0 sim.epoch() if framerate is True: clock.tick(FPS) if sim.step % 100 == 0 and np.random.random() > 0.9: sporadicspawn = True while len( sim.bugs ) < bugnumber or sporadicspawn == True: # and len(sim.bugs)>len(sim.champions): championcolor = np.random.randint(0, 3) collection = None if championcolor == 0: collection = sim.championsR elif championcolor == 1: collection = sim.championsG elif championcolor == 2: collection = sim.championsB if len(collection) > 0: randombug = collection[random.randint(0, len(collection) - 1)] randombug2 = collection[random.randint(0, len(collection) - 1)] else: randombug = bug() randombug2 = bug() sporadicspawn = False if 0.9 > np.random.random(): newbug = randombug.reproduce(randombug, mutationrate=sim.radiation * 2) else: newbug = bug() newbug.state = "alive" sim.bugs.append(newbug) if len(sim.bugs) > 100 and sim.step % 100 == 0: sim.foodammount -= 1000 if sim.foodammount <= 1000: sim.foodammount = 1000 #Config.toxicity += 0.01 if len( sim.bugs ) < bugnumber + 1 and sim.step % 100 == 0 and sim.foodammount < sim.maxfoodammount: sim.foodammount += 1000 # if Config.toxicity > 0.6: # Config.toxicity -= 0.01 if selectedbug != None: if selectedbug.energy <= 0: selectedbug = sim.bugs[0] sim.radiation = button_val("Radiation", 20, 360, 50, 20, buttons_layer, sim.radiation, 1.1) sim.contrast = button_val("Decompose Contrast", 20, +360 + 50, 50, 20, buttons_layer, sim.contrast, 1.1) sim.maxfoodammount = button_val("MaxFood ammount", 20, 360 + 100, 50, 20, buttons_layer, sim.maxfoodammount, 1.1) Config.toxicity = button_val("Toxicity", 20, 360 + 150, 50, 20, buttons_layer, Config.toxicity, 1.02) sim.backgroundpower = button_val("", 720, 860, 50, 20, buttons_layer, sim.backgroundpower, 1.5) display = button_toggle("Realtime", 20, 560, 100, 50, buttons_layer, display) loadbug = False loadbug = button_toggle("Load bug", 20, 560 + 60, 100, 50, buttons_layer, loadbug) if loadbug is True: window = Tk() window.withdraw() filename = askopenfilename() if filename: for i in range(10): sim.bugs.append(bug(np.load(filename))) window.destroy() savebug = False savebug = button_toggle("Save bug", 20, 560 + 120, 100, 50, buttons_layer, savebug) if savebug is True: if selectedbug != None: np.save(str(selectedbug.name), selectedbug.gene) gen_randbug = False gen_randbug = button_toggle("rand_bugs", 20, 560 + 180, 100, 50, buttons_layer, gen_randbug) if gen_randbug is True: for i in range(10): sim.bugs.append(bug()) statistics = False statistics = button_toggle("Statistics", 20, 560 + 180 + 60, 100, 50, buttons_layer, statistics) if statistics is True: plt.plot(stats_repr) plt.plot(stats_bugnumberR, color=(1.0, 0.0, 0.0)) plt.plot(stats_bugnumberG, color=(0.0, 1.0, 0.0)) plt.plot(stats_bugnumberB, color=(0.0, 0.0, 1.0)) plt.plot(stats_foodammount) plt.show() plt.close() #procura eventos de pygame for event in pygame.event.get(): if event.type == pygame.QUIT: sim = None app_running = False pygame.quit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_t: if display == True: sim.draw = False display = False else: sim.draw = True display = True if event.key == pygame.K_b: if background == True: background = False else: background = True if event.key == pygame.K_KP_PLUS: sim.backgroundpower *= 1.5 buttons_layer.fill((0, 0, 0, 0)) if event.key == pygame.K_KP_MINUS: sim.backgroundpower *= 0.75 buttons_layer.fill((0, 0, 0, 0)) if event.key == pygame.K_f: if framerate == True: framerate = False else: framerate = True if event.key == pygame.K_s: if selectedbug != None: np.save(str(selectedbug.name), selectedbug.gene) if event.key == pygame.K_l: window = Tk() window.withdraw() filename = askopenfilename() for i in range(10): sim.bugs.append(bug(np.load(filename))) window.destroy() if event.key == pygame.K_0: sim.foodammount += 2000 sim.environment[0] += distribute(2000, prob=0.1) if event.key == pygame.K_r: for i in range(10): sim.bugs.append(bug()) if event.key == pygame.K_q: sim.contrast /= 1.1 if event.key == pygame.K_v: if sim.terrainview is False: sim.terrainview = True else: sim.terrainview = False # print "Contrast mult: "+str(sim.contrast) if event.key == pygame.K_m: window = Tk() window.withdraw() filename = askopenfilename() imagem = np.array(load_image(filename)) sim.environment[0] = (255 - imagem[:, :, 0].T) * 0.2 sim.environment[1] = (255 - imagem[:, :, 1].T) * 0.2 sim.environment[2] = (255 - imagem[:, :, 2].T) * 0.2 window.destroy() if event.key == pygame.K_n: np.save("Enviro", np.array(sim.environment)) if event.key == pygame.K_9: lessfood = (sim.foodammount - 2000) / sim.foodammount sim.foodammount -= 2000 sim.foodammount sim.environment[0] * lessfood if event.key == pygame.K_p: plt.plot(stats_repr) plt.plot(stats_bugnumberR, color=(1.0, 0.0, 0.0)) plt.plot(stats_bugnumberG, color=(0.0, 1.0, 0.0)) plt.plot(stats_bugnumberB, color=(0.0, 0.0, 1.0)) plt.plot(stats_foodammount) plt.show() plt.close() #seleciona bugs com o rato if event.type == pygame.MOUSEBUTTONDOWN: #print [int((pygame.mouse.get_pos()[0]-lowercorner[0])/(simsizeX/10)),int((pygame.mouse.get_pos()[1]-lowercorner[1])/(simsizeY/10))] mousecoordX = float((pygame.mouse.get_pos()[0] - lowercorner[0])) #/(simsizeX/10) mousecoordY = float((pygame.mouse.get_pos()[1] - lowercorner[1])) # / (simsizeY / 10) #print mousecoordX,mousecoordY distancia = 100000 for i in range(len(sim.bugs) - 1): candidatedistance = distance([mousecoordX, mousecoordY], sim.bugs[i].position) if candidatedistance < distancia: distancia = candidatedistance selectedbug = sim.bugs[i] if distancia > 80: selectedbug = sim.bugs[0] # # if mousecoordX < 10 and mousecoordY < 10 and mousecoordX >= 0 and mousecoordY >= 0: # selectedbugbox = sim.bugmap[mousecoordX][mousecoordY] # # # # if selectedbug != None: #selectedbug = selectedbugbox[0] printgene(selectedbug) else: selectedbug = sim.bugs[0] if selectedbug is None: # and display == True: if sim != None: if sim.step % 100 == 0 and len(sim.bugs) > 0: championbug = sim.bugs[0] printbug(championbug) else: if sim != None: # print selectedbug.energy printbug(selectedbug) if display == True: if sim != None: sim.display(debug, background, selectedbug) else: if sim != None: if sim.step % 100 == 0: sim.display(debug, background)
log_trace = "iteration:{:3d} | "\ "policy_loss:{:3.3f} | " \ "value_loss:{:3.3f}".format(iteration_index, float(policy_loss), float(value_loss)) print(log_trace) # save parameters, run simulation and plot figures if iteration_index == MAX_ITERATION: # ==================== Set log path ==================== print( "********************************* FINISH TRAINING **********************************" ) log_dir = "./Results_dir/" + datetime.now().strftime( "%Y-%m-%d-%H-%M-" + str(iteration_index)) os.makedirs(log_dir, exist_ok=True) value.save_parameters(log_dir) policy.save_parameters(log_dir) train.print_loss_figure(MAX_ITERATION, log_dir) train.save_data(log_dir) break if SIMULATION_FLAG == 1: print( "********************************* START SIMULATION *********************************" ) methods = ['MPC', 'ADP'] simu_dir = "./Simulation_dir/" + datetime.now().strftime("%Y-%m-%d-%H-%M") os.makedirs(simu_dir, exist_ok=True) if TRAIN_FLAG == 0: simulation(methods, load_dir, simu_dir) else: simulation(methods, log_dir, simu_dir)
def hebbian_learning(fcm, restraints, stabilizers, transferFunct, neu, k = maxsize): ''' Hebbian_learning Parameters: fcm(FCM): an fcm already created restraints:dictionary(tuple): dictionary that contains the range of the desired output of concepts stabilizers(dictionary): concepts to stabilize on and their stabilization threshold transferFunct(function(1 argument)): function to keep values in desired range neu(float): a float in range [0,1] that is the learning coefficient k(int):default is max system size, how many steps the system should use to learn Returns: The new edge matrix for the FCM Description: Will take an FCM and using the Nonlinear Hebbian Learning method http://biomine.ece.ualberta.ca/papers/WCCI2008.pdf to get optimal edge values for an FCM starting from an initial state. ''' limit = k count = 0 oldValues = [] #list for values of concepts done = False nodeOrder = fcm._fcm_graph.nodes() edgeMatrix = nx.to_numpy_matrix(fcm._fcm_graph) #edge matrix not transposed(input to concept in the column) #get the list of values in node order for node in nodeOrder: oldValues.append(fcm.concepts()[node]) shape = edgeMatrix.shape#get dimensions of the matrix numRows = shape[0] numCols = shape[1] stableConcepts = stable_concepts(fcm) #find any concepts that have no input vectors(equation does not maintain edges it seems) while count < limit: if count == 0: oldConceptValues = fcm.concepts()#get concepts before changes to compare to loop break else: oldConceptValues = newConceptValues for row in range(0,numRows): #call equation to update edge values for each value in edgematrix #for col in range(0,numCols):#column is target #pass edge to edgeupdate newRow = updateEdge(edgeMatrix[row],oldValues[row],oldValues,neu)#pass oldvalues source as a single digit as it wont change. send targets as list to work through with row #set new values in edge matrix edgeMatrix[row] = newRow #calculate new concempt values:refer to update nodes in simulation method. will only need slight modifications newValues = updateConcepts(edgeMatrix,oldValues,transferFunct,stableConcepts) #set values of fcm to the new values from the equation index = 0 for node in fcm._fcm_graph.nodes():#keep things in node order fcm.set_value(node,newValues[index]) index += 1 #set the edge strengths to the new edge values for row in range(0,numRows): for col in range(0,numCols): if edgeMatrix.item((row,col)) == 0: continue fcm.add_edge(fcm._fcm_graph.nodes()[row],fcm._fcm_graph.nodes()[col],edgeMatrix.item((row,col))) #now start a simultion with fcm sim = simulation(fcm) for key in stabilizers: sim.stabilize(key,stabilizers[key]) #simulate til 10000 steps or stability reached sim.steps(10000) sim.changeTransferFunction(transferFunct) ValuesList = sim.run() #run simuation. returns a dict of final values as well as print them newConceptValues = ValuesList[len(ValuesList)-1] print newConceptValues, "New values are here" #if the values returned from the simulation are in the desired range, and #the change is less than the stabilization threshold used in the simulationthen exit the loop print newConceptValues print restraints print stabilizers print oldConceptValues for key in restraints: if newConceptValues[key] > restraints[key][0] and newConceptValues[key] < restraints[key][1] and (abs(newConceptValues[key]-oldConceptValues[key])) < stabilizers[key]: done = True else: done = False break #learning not done. continue in main loop if done: break #else continue in loop count += 1 return edgeMatrix