forked from rlucas92/AnimatSimulation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
masterDriver.py
160 lines (142 loc) · 6.68 KB
/
masterDriver.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
__author__ = 'RJ'
import clusterDriver as cd
import pp
import time
import spur
import sys
import os
import getopt
import EvoDriver
import logging
import SimParam
class MasterDriver():
#Driver for Master Node
def __init__(self):
self.sP = SimParam.SimParam()
logger = logging.getLogger('pp')
logging.basicConfig(filename='log.txt')
self.runTime = 1000
self.writeInt = 100
tsTime = time.clock()
self.runEvo = False
#self.cwd = os.getcwd() + '/'
self.doWrite = True
self.usr = "lucasrh" #Username for SSH connections
self.pw = "Grammercy1101grove" #Password for SSH connections
self.nodeShells = [] #Holder for ssh shells for each node in cluster, tuple of (shell,process) objects
self.nodeP2Ps = [("10.2.1." + str(i) + ":60000") for i in xrange(2,12)] #P2P address for each node on cluste
self.nodeNum = 2 #Number of nodes needed. 1 Animat per node
self.simNum = 3 #Number of simulations to run in parallel on each node, different worlds
self.settingsFile = -1 #Holder for settingsFile
self.animParams = [["Wheel Animat",(1,0),10,[80,.02,.25,-65,2],[320,.02,.2,-65,8]] for i in xrange(self.nodeNum)]
#run parseOptions after all user changable vars are initialized to avoid errors
self.parseOptions() #Change above values if specified by command line options
self.sP.setWorld(1,15,20)
#self.worldParams = [[1,15,20] for i in xrange(self.simNum)]
#for running simulations without evolutionary algorithm
print "Node addresses to connect to:"
print self.nodeP2Ps[0:8]
if not self.runEvo:
self.nodeDrivers = [cd.ClusterDriver(i+1,self.worldParams,self.animParams[i],self.writeInt,writeFiles=self.doWrite,time=self.runTime) for i in xrange(self.nodeNum)] #driver objects for each node
#self.initializeNodes(self.nodeNum,self.nodeShells)
self.jobServer = pp.Server(ncpus=0,ppservers=tuple(self.nodeP2Ps[0:8])) #connect to each node
self.jobList =[self.jobServer.submit(node.startNode,modules=("pp",)) for node in self.nodeDrivers]
sTime = time.clock()
for job in self.jobList:
print "job about to execute"
job() #execute job
eTime = time.clock()
print "Run Time: " + str(eTime-sTime)
print"masterDriver stats"
self.jobServer.print_stats()
self.jobServer.destroy()
#for running simulation with evolutionary algorithm
else:
evo = EvoDriver.EvoDriver()
teTime = time.clock()
print "Total Run Time: " + str(teTime-tsTime)
#for shell,process in self.nodeShells: process.send_signal("SIGTERM") #terminate all ppservers
## Read in Options from Command Line ##
## -h : Display help information
## -a : How many different animats to run (1 animat per node)
## -w : How many worlds to run animat in (i.e. how many simulations on each node)
## -u : Username for SSH session
## -p : Password for SSH session
## -f : File to read settings from
## -e : Run with evolutionary algorithm
## -p : Print true or false
def parseOptions(self):
#Check for arguments
options = sys.argv[1:] #all command line arguments
try:
opts,args = getopt.getopt(options,"eha:w:u:p:f:p:t:i:")
except getopt.GetoptError:
print "run -h to display help information"
for opt, arg in opts:
if opt == '-h': self.printHelp()
elif opt == '-a':
self.nodeNum = int(arg)
self.animParams = [["Wheel Animat",(1,0),10,[80,.02,.25,-65,2],[320,.02,.2,-65,8]] for i in xrange(self.nodeNum)]
elif opt == '-w': self.simNum = int(arg)
elif opt == '-u': self.usr = arg
elif opt == '-p': self.pw = arg
elif opt == '-f': self.readSettings(arg)
elif opt == '-e': self.runEvo = True
elif opt == '-p': self.doWrite = bool(arg)
elif opt == '-t': self.runTime = int(arg)
elif opt == '-i': self.writeInt = int(arg)
def inializeEvo(self):
pass
def readSettings(self,fileName):
f = open(fileName, 'r')
self.nodeNum = int(f.readline())
self.simNum = int(f.readline())
temp = []
for i in xrange(self.simNum):
animNum = int(f.readline())
foodNum = int(f.readline())
size = int(f.readline())
temp.append([animNum,foodNum,size])
self.worldParams = temp
temp = []
for i in xrange(self.nodeNum):
type = f.readline()
origin = f.readline().split()
energy = int(f.readline())
inhib = f.readline().split(' ')
excit = f.readline().split(' ')
inhib[0] = int(inhib[0])
for i,val in inhib[1:]: inhib[i] = float(val)
excit[0] = int(excit[0])
for i,val in excit[1:]: excit[i] = float(val)
temp.append([type,(int(origin[0]),int(origin[1])),energy,inhib,excit])
self.animParams = temp
def printHelp(self):
print "\nUsage:"
print " masterDriver.py <command> [options]\n"
print "\nCommands: "
print " COMMANDS NOT YET IMPLEMENTED\n"
print "\nOptions: "
print " -a Number of Animats to simulate. Each runs on its own node. "
print " -w Number of worlds to run each Animat in."
print " -u Username for dogwood cluster."
print " -p Password for dogwood cluster."
print " -h Displays help information."
print " -f File to read settings from."
print " -e Run with Evolutionary Algorithm."
print " -t Number of simulated milliseconds."
print " -i Write interval"
sys.exit()
def initializeNodes(self,num,nodes):
print "Connecting to Nodes"
#create shell for each connection
shell, process = -1,-1 #holder for shell,process tuple
for i in xrange(num):
print i
if i+1 < 10: hn = "dogwood0" + str(i+1)
else: hn = "dogwood" + str(i+1)
shell = spur.SshShell(hostname=hn,username=self.usr,password=self.pw)
#alias to switch to AnimatClusterSim directory
process = shell.spawn(["python","ppserver.py"],cwd=self.cwd,store_pid=True) #spawn does not, ppserver does not terminate
nodes.append((shell,process))
mD = MasterDriver()