-
Notifications
You must be signed in to change notification settings - Fork 0
/
display.py
98 lines (75 loc) · 3.71 KB
/
display.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import pickle
import constants as c
import pybullet as p
import numpy as np
import pyrosim.pyrosim as pyrosim
import random
import os
import time
import math
os.system("rm brain*.nndf")
os.system("rm body*.urdf")
#Enter the run number and generation you want to see:
runNumber = 23
generation = 0
def Create_Body(linkPlan, jointPlan, sensorWeights, motorWeights):
pyrosim.Start_URDF("body0.urdf")
Create_Links(linkPlan, jointPlan)
pyrosim.End()
# pyrosim.Start_URDF("body.urdf")
# Create_Links(linkPlan, jointPlan)
# pyrosim.End()
def Create_Links(linkPlan, jointPlan):
for link in linkPlan:
pyrosim.Send_Cube(name=str(link[0]), pos=link[2], size=link[3], colorName = link[4])
for joint in jointPlan:
pyrosim.Send_Joint(name = str(joint[0]) + "_" + str(joint[1]) , parent= str(joint[0]) , child = str(joint[1]) , type = "revolute", position = joint[2], jointAxis = "1 1 0")
def Create_Brain(linkPlan, jointPlan, sensorWeights, motorWeights):
pyrosim.Start_NeuralNetwork("brain0.nndf")
sensorIndex = 0
sensors = []
for link in linkPlan:
if link[4] == 'green':
pyrosim.Send_Sensor_Neuron(name = sensorIndex + len(jointPlan) + numHiddenNeurons , linkName = str(link[0]))
# self.sensors.append(link[0])
# sensorIndex += 1
if link[0] not in sensorWeights:
sensorWeights[link[0]] = []
for i in range(0, numHiddenNeurons):
sensorWeights[link[0]].append(random.random() * 2 - 1)
for i in range(0, numHiddenNeurons):
hiddenNeuronName = len(jointPlan) + i
pyrosim.Send_Synapse( sourceNeuronName = sensorIndex + len(jointPlan) + numHiddenNeurons , targetNeuronName = hiddenNeuronName , weight = sensorWeights[link[0]][i] )
sensors.append(link[0])
sensorIndex += 1
# print("Sensors: ")
# print(self.sensors)
for joint in jointPlan:
pyrosim.Send_Motor_Neuron( name = jointPlan.index(joint) , jointName = str(joint[0]) + "_" + str(joint[1]))
if jointPlan.index(joint) not in motorWeights:
motorWeights[jointPlan.index(joint)] = []
for i in range(0, numHiddenNeurons):
motorWeights[jointPlan.index(joint)].append(random.random() * 2 - 1)
for i in range(0, numHiddenNeurons):
hiddenNeuronName = len(jointPlan) + i
pyrosim.Send_Synapse( sourceNeuronName = hiddenNeuronName , targetNeuronName = jointPlan.index(joint) , weight = motorWeights[jointPlan.index(joint)][i] )
for hiddenNeuronID in range(0, numHiddenNeurons):
pyrosim.Send_Hidden_Neuron(name = len(jointPlan) + hiddenNeuronID)
pyrosim.End()
folderPath = '/Users/justin/Documents/CS Classes/artificial-life/run' + str(runNumber) + '/'
linkPlan = pickle.load( open( folderPath + "Gen" + str(generation) + "linkPlan.p", "rb" ) )
print(linkPlan[0])
jointPlan = pickle.load( open( folderPath + "Gen" + str(generation) + "jointPlan.p", "rb" ) )
print(jointPlan[0])
sensorWeights = pickle.load( open( folderPath + "Gen" + str(generation) + "sensorWeights.p", "rb" ) )
motorWeights = pickle.load( open( folderPath + "Gen" + str(generation) + "motorWeights.p", "rb" ) )
# numHiddenNeurons = pickle.load( open( folderPath + "Gen" + str(generation) + "hiddenNeurons.p", "wb" ) )
print(sensorWeights)
firstSensor = 0
for link in linkPlan:
if link[4] == 'green':
firstSensor = link[0]
numHiddenNeurons = len(sensorWeights[firstSensor])
Create_Body(linkPlan, jointPlan, sensorWeights, motorWeights)
Create_Brain(linkPlan, jointPlan, sensorWeights, motorWeights)
os.system("python3 simulate.py " + "GUI" + " " + str(0) + " 2&>1 &")