def loadData(file): global STATES global STATE global NAME1 global NAME2 NAME1 = "" NAME2 = "" STATES = [] STATE = 0 data = sexpr.str2sexpr(file.read()) #("ident" ((2 "Example AI") (3 "Example AI"))) NAME1 = data[0][1][0][1] NAME2 = data[0][1][1][1] data = data[1:] for i in data: state = State() state.setScore0(i[1][1][1]) state.setScore1(i[1][2][1]) for j in i[1][1][2]: state.units.append(Unit(0, j[0])) unit = state.units[-1] unit.x, unit.y = int(j[3]), int(j[4]) for j in i[1][2][2]: state.units.append(Unit(1, j[0])) unit = state.units[-1] unit.x, unit.y = int(j[3]), int(j[4]) for j in i[1][3]: state.nodes.append(Unit(-1, "Node")) node = state.nodes[-1] node.x, node.y = int(j[2]), int(j[3]) node.juice = int(j[1]) STATES.append(state)
def run(self): """The main method of this process. It starts the simspark application, connects to it and receives updates of the simulation state and also sends scheduled commands to the simulation.""" self.__start_application() while (not self.__start_instance or self.__p.poll() is None) and not self.__cancel.is_set(): # connect to the simspark instance if not self.connected.is_set(): self.connect() else: length = struct.unpack("!I", self.socket.recv(4))[0] msg = self.socket.recv(length).decode() sexp = sexpr.str2sexpr(msg) self.__update_environment(sexp[0]) self.__update_scene(sexp[1:]) # send scheduled (trainer) commands if not self.__cmd_queue.empty(): cmd = self.__cmd_queue.get() logging.debug(cmd) self.socket.sendall(struct.pack("!I", len(cmd)) + str.encode(cmd)) # send kill command, before disconnecting self.cmd_killsim() # disconnect self.disconnect() # if process still running, kill it self.__stop_application()
def preload(self): if len(sys.argv) < 2: f = open('input.viz') else: f = open(sys.argv[1]) text = sexpr.str2sexpr(f.read()) initialload = 0 actionlist = [] for item in text: #print item[0] if item[0] == 'ident': main.State.Player1ID=int(item[1][0][0]) main.State.Player1=item[1][0][1] main.State.Player2ID=int(item[1][1][0]) main.State.Player2=item[1][1][1] elif item[0] == 'walls': for w in item[1:]: #print "Constructing a wall at (",w[1],",",w[2],")" initialload += -1 actionlist.append(main.Place(main.Unit(main.WALLTEAMID, "Tree", initialload, int(w[1]), int(w[2]), 0))) elif item[0] == 'buildZones': for n in item[1:]: #print "Constructing a BuildZone at (",n[1],",",n[2],")" initialload += -1 actionlist.append(main.Place(main.Unit(main.NODETEAMID, "BuildZone", initialload, int(n[1]), int(n[2]), 0))) return main.CompositeAction(actionlist)
def sense(self): length = '' while (len(length) < 4): length += self.socket.recv(4 - len(length)) length = struct.unpack("!I", length)[0] msg = '' while len(msg) < length: msg += self.socket.recv(length - len(msg)) sexp = str2sexpr(msg) self.perception.update(sexp) return self.perception
def sense(self): length = '' while(len(length) < 4): length += self.socket.recv(4 - len(length)) length = struct.unpack("!I", length)[0] msg = '' while len(msg) < length: msg += self.socket.recv(length - len(msg)) sexp = str2sexpr(msg) self.perception.update(sexp) return self.perception
def recieve_perceptors(): length_no=sock.recv(4) length=struct.unpack("!I", length_no) sexprstr=sock.recv(length[0]) sexprlist = sexpr.str2sexpr(sexprstr) print "sexprlist:", sexprlist, "\n" ''' perceptors={} for subl in sexprlist: if('HJ' in subl): perceptors[subl[1][1]] = float(subl[2][1]) print "R: hj1", perceptors["hj1"], "\n" ''' return sexprlist
def recieve_perceptors(): length_no = sock.recv(4) length = struct.unpack("!I", length_no) sexprstr = sock.recv(length[0]) sexprlist = sexpr.str2sexpr(sexprstr) print "sexprlist:", sexprlist, "\n" ''' perceptors={} for subl in sexprlist: if('HJ' in subl): perceptors[subl[1][1]] = float(subl[2][1]) print "R: hj1", perceptors["hj1"], "\n" ''' return sexprlist
def sense(self): """ Function to receive sensory data from the simulator and parse it the desired format. """ length = '' while(len(length) < 4): length += self.socket.recv(4 - len(length)) length = struct.unpack("!I", length)[0] msg = '' while len(msg) < length: msg += self.socket.recv(length - len(msg)) sexp = str2sexpr(msg) self.perception.update(sexp) return self.perception
def __init__(self,device,content): """ deviceclass is the name of the device we are using content is the content of the EDIF file """ self.device=device edif=str2sexpr(content)[0] #print edif assert edif[0] == "edif", "is it really an EDIF file?" self.designname=edif[1] #print "design name is", self.designname self.libs={} for construct in edif[2:]: if (construct[0]=="external"): lib={} self.parselib(lib,construct,external=True) self.libs[construct[1]]=lib elif (construct[0]=="library"): lib={} self.parselib(lib,construct) self.libs[construct[1]]=lib elif (construct[0]=="design"): assert (construct[2][0] == 'cellref') and ( construct[2][2][0] == 'libraryref'), "cannot parse design" devname=construct[2][1] libname=construct[2][2][1] assert libname in self.libs, "Cannot find design %s in libs (%s)"%(libname,self.libs.keys()) self.device.doDesign(self.libs[libname][devname]) elif (construct[0]=="edifversion"): if construct[1] != '2': raise NotImplementedError, "EDIF version other than 2: %s"%(construct) elif (construct[0]=="ediflevel"): if construct[1] != '0': raise NotImplementedError, "EDIF level other than 0: %s"%(construct) elif (construct[0]=="keywordmap"): pass elif (construct[0]=="status"): pass else: raise NotImplementedError, construct[0]
def testshortlist(self): self.assertEqual(sexpr.str2sexpr('(1)')[0], list(1))
def testemptylist(self): self.assertEqual(sexpr.str2sexpr('()')[0], list())
def testescape(self): self.assertEqual(sexpr.str2sexpr('\\"string\\"')[0], Symbol('"string"'))
def cegis(smt, params, k, r, statistics={}): global RESTART_THRESHOLD print('===== CEGIS[k={0},r={1}] ====='.format(k, r)) ### Initial values for statistics statistics['time'] = 0 statistics['#smt_calls'] = 0 statistics['#restarts'] = 0 statistics['#parameter_candidates'] = 0 statistics['parameter_synthesis.time'] = [] statistics['parameter_synthesis.model'] = [] statistics['#counterexamples'] = 0 statistics['correctness_check.time'] = [] statistics['correctness_check.model'] = [] statistics['#randomizations'] = 0 statistics['counterexample_randomization.time'] = [] statistics['counterexample_randomization.model'] = [] total_time_start = time.time() ### Build correctness formula init_smt = (smt + build_correctness_formula(k)).format(*params).split('\n') ### Remove parameters from SMT instance prepared_smt = [] for line in init_smt: matched = False for p in params: if line.strip().startswith('(define-fun ' + p): a, b, c, d, e = line.split(' ') prepared_smt.append('(declare-fun {0} {1} {2})'.format( b, c, d)) matched = True if not matched: prepared_smt.append(line) init_smt = prepared_smt count_no_progress = 0 while True: ### Restart the engine if CEGIS does not make progress if r >= 3 and len(statistics['parameter_synthesis.model']) > 1: total_progress = 0 for key in statistics['parameter_synthesis.model'][-2:][0]: new_value = sexpr.str2sexpr( statistics['parameter_synthesis.model'][-2:][1][key]) # print(new_value) if isinstance(new_value, list): new_value = new_value[0] new_value = eval_expression(new_value) old_value = sexpr.str2sexpr( statistics['parameter_synthesis.model'][-2:][0][key]) # print(old_value) if isinstance(old_value, list): old_value = old_value[0] old_value = eval_expression(old_value) # print(new_value) # print(old_value) assert new_value[0] == True assert old_value[0] == True progress = new_value[1] - old_value[1] total_progress += abs(progress) if total_progress < 0.0000001: print("N", end='') count_no_progress += 1 if count_no_progress > 10: print("R", end='') count_no_progress = 0 statistics['#restarts'] += 1 check_smt = list(init_smt) ### Restart the engine (and forget all previously learned counterexamples) if statistics['#counterexamples'] % RESTART_THRESHOLD == 0: print("R", end='') statistics['#restarts'] += 1 RESTART_THRESHOLD += 16 * statistics['#restarts'] check_smt = list(init_smt) else: # print(statistics['#counterexamples']) # print(RESTART_THRESHOLD) # print(statistics['#counterexamples'] % RESTART_THRESHOLD == 0) print('.', end='') sys.stdout.flush() ### Compute parameters compute_smt = list(check_smt) statistics['#smt_calls'] += 1 statistics['#parameter_candidates'] += 1 ts = time.time() (sat, model) = SMTsolve(compute_smt, params) te = time.time() statistics['parameter_synthesis.time'].append(te - ts) statistics['parameter_synthesis.model'].append(model if sat else {}) if not sat: total_time_end = time.time() statistics['cegis.time'] = total_time_end - total_time_start return False, {} ### Check k-correctness correct_smt = [] for line in check_smt: matched = False for p in params: if line.strip().startswith('(declare-fun ' + p): a, b, c, d = line.split(' ') correct_smt.append('(define-fun {0} {1} {2} {3})'.format( b, c, d[:-1], model[p])) matched = True if not matched: correct_smt.append(line) correct_smt.append('(declare-fun s0 () state)') for i in range(0, k): correct_smt.append('(declare-fun i{0} () input)'.format(i)) correct_smt.append('(assert (not (correct{0} s0 {1})))'.format( k, ' '.join("i{0}".format(cnt) for cnt in range(0, k)))) statistics['#smt_calls'] += 1 statistics['#counterexamples'] += 1 ts = time.time() (sat, cex) = SMTsolve(correct_smt, ['s0'] + ['i{0}'.format(j) for j in range(0, k)]) te = time.time() statistics['correctness_check.time'].append(te - ts) statistics['correctness_check.model'].append(cex if sat else {}) if not sat: total_time_end = time.time() statistics['cegis.time'] = total_time_end - total_time_start return True, model ### Make counterexamples as nasty as possible if r == 1 or (r >= 2 and statistics['#counterexamples'] % 2 == 0): state = sexpr.str2sexpr(cex['s0'])[0][1:] for i in range(0, len(state)): try_state = [] for j in range(0, len(state)): if i == j: ok, value = randomize_if_necessary(state[i]) try_state.append(value) else: try_state.append(state[j]) state_sexpr = sexpr.str2sexpr(cex['s0'])[0] state_sexpr[1:] = try_state if state_sexpr == sexpr.str2sexpr(cex['s0'])[0]: continue # skip the SMT check if no randomization happened state_sexpr = sexpr.sexpr2str(state_sexpr) nasty_check_smt = list(check_smt) nasty_check_smt.append( '(assert (not (correct{0} {1} {2})))'.format( k, state_sexpr, " ".join([cex["i" + str(cnt)] for cnt in range(0, k)]))) statistics['#smt_calls'] += 1 statistics['#randomizations'] += 1 ts = time.time() (sat, model) = SMTsolve(nasty_check_smt, params) te = time.time() statistics['counterexample_randomization.time'].append(te - ts) statistics['counterexample_randomization.model'].append( cex if sat else {}) if sat: # print("changed",state[i],"to",try_state[i]) state[i] = try_state[i] state_sexpr = sexpr.str2sexpr(cex['s0'])[0] state_sexpr[1:] = state if cex['s0'] != sexpr.sexpr2str(state_sexpr): print('G', end='') cex['s0'] = sexpr.sexpr2str(state_sexpr) ### Make sure that the next parameters are better check_smt.append('(assert (correct{0} {1} {2}))'.format( k, cex['s0'], " ".join([cex["i" + str(cnt)] for cnt in range(0, k)])))
def testlist(self): self.assertEqual(sexpr.str2sexpr('(a b c)')[0], list(Symbol('a'), Symbol('b'), Symbol('c')))
def getqmem(str): return sexpr.str2sexpr(str)[0]
def parse(): tree = ET.parse('work/nlp.txt.xml') root = tree.getroot() for s in root.findall(".//parse"): list_s = sexpr.str2sexpr(s.text) search_np(list_s)
def tle(): while True: print 'Lark> ', expr = str2sexpr(raw_input())[0] print ac_eval(expr, {})
def testfncall(self): self.assertEqual(sexpr.str2sexpr('((fn (a) a) 1)')[0], list(list(Symbol('fn'), list(Symbol('a')), Symbol('a')), 1))
def cegis(smt, params, k, r, statistics = {}): global RESTART_THRESHOLD print('===== CEGIS[k={0},r={1}] ====='.format(k,r)) ### Initial values for statistics statistics['time'] = 0 statistics['#smt_calls'] = 0 statistics['#restarts'] = 0 statistics['#parameter_candidates'] = 0 statistics['parameter_synthesis.time'] = [] statistics['parameter_synthesis.model'] = [] statistics['#counterexamples'] = 0 statistics['correctness_check.time'] = [] statistics['correctness_check.model'] = [] statistics['#randomizations'] = 0 statistics['counterexample_randomization.time'] = [] statistics['counterexample_randomization.model'] = [] total_time_start = time.time() ### Build correctness formula init_smt = (smt + build_correctness_formula(k)).format(*params).split('\n') ### Remove parameters from SMT instance prepared_smt = [] for line in init_smt: matched = False for p in params: if line.strip().startswith('(define-fun ' + p): a,b,c,d,e = line.split(' ') prepared_smt.append('(declare-fun {0} {1} {2})'.format(b,c,d)) matched = True if not matched: prepared_smt.append(line) init_smt = prepared_smt count_no_progress = 0 while True: ### Restart the engine if CEGIS does not make progress if r >= 3 and len(statistics['parameter_synthesis.model']) > 1: total_progress = 0 for key in statistics['parameter_synthesis.model'][-2:][0]: new_value = sexpr.str2sexpr(statistics['parameter_synthesis.model'][-2:][1][key]) # print(new_value) if isinstance(new_value,list): new_value = new_value[0] new_value = eval_expression(new_value) old_value = sexpr.str2sexpr(statistics['parameter_synthesis.model'][-2:][0][key]) # print(old_value) if isinstance(old_value,list): old_value = old_value[0] old_value = eval_expression(old_value) # print(new_value) # print(old_value) assert new_value[0] == True assert old_value[0] == True progress = new_value[1] - old_value[1] total_progress += abs(progress) if total_progress < 0.0000001: print("N",end='') count_no_progress += 1 if count_no_progress > 10: print("R",end='') count_no_progress = 0 statistics['#restarts'] += 1 check_smt = list(init_smt) ### Restart the engine (and forget all previously learned counterexamples) if statistics['#counterexamples'] % RESTART_THRESHOLD == 0: print("R",end='') statistics['#restarts'] += 1 RESTART_THRESHOLD += 16*statistics['#restarts'] check_smt = list(init_smt) else: # print(statistics['#counterexamples']) # print(RESTART_THRESHOLD) # print(statistics['#counterexamples'] % RESTART_THRESHOLD == 0) print('.',end='') sys.stdout.flush() ### Compute parameters compute_smt = list(check_smt) statistics['#smt_calls'] += 1 statistics['#parameter_candidates'] += 1 ts = time.time() (sat,model) = SMTsolve(compute_smt,params) te = time.time() statistics['parameter_synthesis.time'].append(te - ts) statistics['parameter_synthesis.model'].append(model if sat else {}) if not sat: total_time_end = time.time() statistics['cegis.time'] = total_time_end - total_time_start return False, {} ### Check k-correctness correct_smt = [] for line in check_smt: matched = False for p in params: if line.strip().startswith('(declare-fun ' + p): a,b,c,d = line.split(' ') correct_smt.append('(define-fun {0} {1} {2} {3})'.format(b,c,d[:-1],model[p])) matched = True if not matched: correct_smt.append(line) correct_smt.append('(declare-fun s0 () state)') for i in range(0,k): correct_smt.append('(declare-fun i{0} () input)'.format(i)) correct_smt.append('(assert (not (correct{0} s0 {1})))' .format(k, ' '.join("i{0}".format(cnt) for cnt in range(0,k)))) statistics['#smt_calls'] += 1 statistics['#counterexamples'] += 1 ts = time.time() (sat, cex) = SMTsolve(correct_smt, ['s0'] + ['i{0}'.format(j) for j in range(0,k)]) te = time.time() statistics['correctness_check.time'].append(te - ts) statistics['correctness_check.model'].append(cex if sat else {}) if not sat: total_time_end = time.time() statistics['cegis.time'] = total_time_end - total_time_start return True, model ### Make counterexamples as nasty as possible if r == 1 or (r >= 2 and statistics['#counterexamples'] % 2 == 0): state = sexpr.str2sexpr(cex['s0'])[0][1:] for i in range(0,len(state)): try_state = [] for j in range(0,len(state)): if i == j: ok, value = randomize_if_necessary(state[i]) try_state.append(value) else: try_state.append(state[j]) state_sexpr = sexpr.str2sexpr(cex['s0'])[0] state_sexpr[1:] = try_state if state_sexpr == sexpr.str2sexpr(cex['s0'])[0]: continue # skip the SMT check if no randomization happened state_sexpr = sexpr.sexpr2str(state_sexpr) nasty_check_smt = list(check_smt) nasty_check_smt.append('(assert (not (correct{0} {1} {2})))' .format(k, state_sexpr, " ".join([cex["i" + str(cnt)] for cnt in range(0,k)]))) statistics['#smt_calls'] += 1 statistics['#randomizations'] += 1 ts = time.time() (sat, model) = SMTsolve(nasty_check_smt,params) te = time.time() statistics['counterexample_randomization.time'].append(te - ts) statistics['counterexample_randomization.model'].append(cex if sat else {}) if sat: # print("changed",state[i],"to",try_state[i]) state[i] = try_state[i] state_sexpr = sexpr.str2sexpr(cex['s0'])[0] state_sexpr[1:] = state if cex['s0'] != sexpr.sexpr2str(state_sexpr): print('G',end='') cex['s0'] = sexpr.sexpr2str(state_sexpr) ### Make sure that the next parameters are better check_smt.append('(assert (correct{0} {1} {2}))'.format(k, cex['s0'], " ".join([cex["i" + str(cnt)] for cnt in range(0,k)])))
def load(self, s): #LOAD FROM FILE #""" yield self.preload() if len(sys.argv) < 2: f = open('input.viz') else: f = open(sys.argv[1]) text = sexpr.str2sexpr(f.read()) actions = [] initialload = 0 for item in text: if item[0] == 'animations': for i in item[1]: if i[0] == 'appear': #print "Appear hit!",i[1],i[3],i[4],"0" id = int(i[1][0]) unit = main.Unit(int(i[2]), "Sphere", id, float(i[1][2]), float(i[1][3]), 0.) unit.red = int(i[1][6]) unit.blue = int(i[1][7]) unit.green = int(i[1][8]) unit.yellow = int(i[1][9]) actions.append(main.Add(unit)) elif i[0] == 'move': #print "Move hit!" #print i[1:] id = int(i[1]) actions.append(main.Move(id,float(i[2]), float(i[3]))) elif i[0] == 'attack': print "Attack hit!" #print i[1:] elif i[0] == 'combine': print "Combine hit!" id0 = int(i[1]) id1 = int(i[2]) actions.append(main.Combine(s.units[id0], s.units[id1])) elif i[0] == 'build': print "Build hit!" unit = int(i[1]) x = int(i[2]) y = int(i[3]) actions.append(main.Build(unit, x, y)) elif i[0] == 'die': #print "Die hit!" id = int(i[1]) actions.append(main.Remove(s.units[id])) elif i[0] == 'achievement': print "Achievement hit!" elif i[0] == 'game-move-denied': print "ERROR: Move Denied!" #print i[1:] elif i[0] == 'game-attack-denied': print "ERROR: Attack Denied!" #print i[1:] elif i[0] == 'game-build-denied': print "ERROR: Build Denied!" #print i[1:] elif i[0] == 'game-combine-denied': print "ERROR: Combine Denied!" #print i[1:] yield main.CompositeAction(actions) actions = [] elif item[0] == 'status': #main.State.Turn=item[1] #main.State.score0=item[1][1][1] #main.State.score0=item[1][2][1] actions.append(main.UpdateStatus(s.score0, item[1][1][1], s.score1, item[1][2][1], s.turn, item[1][0])) #print "score0 = ",item[2][1],"score1 = ", item[3][1] #print "turn[1:] == ", item[1:] #""" """ #ORIGINAL TEST yield main.Add(main.Unit("Omicron", "Fight-man", 0, 15, 15, 0)) yield main.Add(main.Unit("Uniicron", "Flight-man", 1, 5, 5, 0)) i = 15. while True: yield main.Move(0, i, i+1, i, i+1) yield main.Move(1, 20-i, 20-i-1, 20-i, 20-i-1) i += 1 """ """ def animate(animation): for i in animation[1]: if i[0] == 'appear': print "Appearing hit!" print i[1:] yield main.Move("UnitTeamName", "UnitName", int(i[1]), int(i[3]), int(i[4]), 0) elif i[0] == 'move': print "Move hit!" print i[1:] #main.Move(0, i, i+1, i, i+1) elif i[0] == 'attack': print "Attack hit!" elif i[0] == 'combine': print "Combine hit!" elif i[0] == 'build': print "Build hit!" elif i[0] == 'disappear': print "Disappear hit!" elif i[0] == 'achievement': print "Achievement hit!" """ """
def testsymbol(self): self.assertEqual(sexpr.str2sexpr('a')[0], Symbol('a'))
def is_iterable(param): try: return type(param) == type([]) except TypeError: return False def get_prio(itterable): p = [] for foo in itterable: if is_iterable(foo): p.append(get_prio(foo)) else: p.append(prio[itterable[0]]) return p def expand(foo): pass if __name__ == '__main__': a = ["(* a (+ b c))", "(* (+ a b) ((+ c d)))", "(* (& (* (& a b) (& (+ c d) e))) (* f (+ g h i)))", "(+ (& a b) c)"] for foo in a: print foo, "\n\t", sexpr.str2sexpr(foo), "\n\t", get_prio(sexpr.str2sexpr(foo))
def teststring(self): self.assertEqual(sexpr.str2sexpr('"1"')[0], '1')
def readOut(self, data): print "Receiving message from ", self.ID, ": ", data try: self.readSExpr(sexpr.str2sexpr(data)) except ValueError: self.writeSExpr(['malformed-message', data])
def testnum(self): self.assertEqual(sexpr.str2sexpr('1')[0], 1)
def testfloat(self): self.assertEqual(sexpr.str2sexpr('0.5')[0], 0.5)
return type(param) == type([]) except TypeError: return False def get_prio(itterable): p = [] for foo in itterable: if is_iterable(foo): p.append(get_prio(foo)) else: p.append(prio[itterable[0]]) return p def expand(foo): pass if __name__ == '__main__': a = [ "(* a (+ b c))", "(* (+ a b) ((+ c d)))", "(* (& (* (& a b) (& (+ c d) e))) (* f (+ g h i)))", "(+ (& a b) c)" ] for foo in a: print foo, "\n\t", sexpr.str2sexpr(foo), "\n\t", get_prio( sexpr.str2sexpr(foo))