Commit a1ed80fc authored by Alexander Palmisano's avatar Alexander Palmisano
Browse files

added docker, changed snake_state to mdp model and switched to storm

parent 10e44606
FROM ubuntu:bionic
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get -y update
RUN apt-get -y install apt-utils
# INSTALL STORM
ENV STORM_DIR /opt/storm/
RUN apt-get -y install build-essential git cmake libboost-all-dev libcln-dev libgmp-dev libginac-dev \
automake libglpk-dev libhwloc-dev libz3-dev libxerces-c-dev libeigen3-dev
RUN git clone -b stable https://github.com/moves-rwth/storm.git $STORM_DIR
RUN cd $STORM_DIR \
&& mkdir build \
&& cd build \
&& cmake .. \
&& make
ENV PATH $PATH:$STORM_DIR/build/bin
RUN apt-get -y install python3.8
RUN apt-get -y install python3.8-dev
RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 2
RUN python3 --version
RUN apt-get -y install python3-pip
RUN apt-get -y install python3-tk
#INSTALL pycarl
RUN python3 -m pip install pycarl
#INSTALL stormpy
ENV STORMPY_DIR /opt/stormpy/
RUN git clone https://github.com/moves-rwth/stormpy.git $STORMPY_DIR
RUN cd $STORMPY_DIR \
&& python3 -m pip install .
COPY pip_req.txt /temp/
RUN python3 -m pip install -r /temp/pip_req.txt
\ No newline at end of file
# 2 Player Snake
- A Snake game for two players, implemented using PyGame and Python 3
- RL-Agent
- Shield
## install
## Install
execute following commands
install the Dockerfile
` conda env create -n your_env_name -f conda_req.yml `
`conda activate your_env_name `
`pip install -r pip_req.txt`
A Snake game for two players, implemented using PyGame and Python 3
`docker build -f ./Dockerfile -t IMAGE_NAME:TAG .`
## Main features:
## Main features of the game:
- Collect apples and race with your friends
- Score counter for both players
......
import multiprocessing
import pickle
import numpy as np
import copy
......@@ -9,6 +10,7 @@ import subprocess
import concurrent.futures
prism_folder = '~/Downloads/prism-4.6-linux64/bin'
storm_folder = '~/Downloads/storm/build/bin'
# k == 0 ... path
# k == 1 ... wall
......@@ -19,7 +21,7 @@ map, edges, max, node_name_max, edge_name_max, dist_max = pickle.load( open('map
max+=1
map = np.array(map)
max_steps = 4
max_steps = 20
postfix_enemy = '_e'
crash_string = ''
for i in range(max):
......@@ -208,32 +210,33 @@ class snake():
return ret
def write_file(self, filename ='',copy_filename='/out/master.nm'):
filename += str(time.time_ns())+'.nm'
copyfile(str(pathlib.Path().absolute())+copy_filename, str(pathlib.Path().absolute())+'/out/'+filename)
f = open('out/'+filename, 'a')
f.write('module snake\n')
f.write(self.prism[:self.prism.rfind('\n')])
f.write('\nendmodule')
f.close()
return filename
def append_file(self, file):
file.write(self.prism[:self.prism.rfind('\n')])
def get_prism_string(self):
return self.prism[:self.prism.rfind('\n')]
def recursion_step(snake, max_steps):
def recursion_step_mdp(snake, max_steps, copy_file):
f = open(copy_file[1:], 'a')
f.write('module snake\n')
snake.append_file(f)
dir = ['down', 'left', 'up', 'right']
files = {'down':[],'left':[],'up':[],'right':[]}
files = {}
snake.prismsteps = 0
start_steps = snake.steps
s_temp = snake.copy()
if not s_temp.decision:
while s_temp.step(allow_wrong=True) != 1:
string = snake.get_prism_string()
state_dec = string[string.rfind('\n') + 1:]
f.write(state_dec)
if s_temp.steps - start_steps == max_steps:
f.close()
return None
f.close()
for i in range(len(dir)):
s = s_temp.copy()
ret = s.step(dir[i], allow_wrong=True)
......@@ -241,47 +244,51 @@ def recursion_step(snake, max_steps):
if ret == -1:
del s
continue
if s_temp.steps - start_steps == max_steps:
files[dir[i]].append(s.write_file('out_'))
del s
continue
recursion_step_f(s, max_steps, start_steps, files[dir[i]])
filename = str(time.time_ns()) + '.nm'
copyfile(str(pathlib.Path().absolute()) + copy_file, str(pathlib.Path().absolute()) + '/out/' + filename)
files[dir[i]] = filename
f = open('out/' + filename, 'a')
string = s.get_prism_string()
f.write(string[string.rfind('\n') + 1:]+'\n')
recursion_step_mdp_f(s, max_steps, start_steps, f)
f.write('endmodule')
return files
def recursion_step_f(snake, max_steps, start_steps, files):
def recursion_step_mdp_f(snake, max_steps, start_steps, file=None):
dir = ['down', 'left', 'up', 'right']
break_flag = False
state_dec = ''
state_dec_prob = []
state = ''
while snake.step(allow_wrong=True) != 1:
string = snake.get_prism_string()
state += string[string.rfind('\n') + 1:] + '\n'
if snake.steps - start_steps == max_steps:
if file:
file.write(state + '\n')
return
for i in range(len(dir)):
cont_flag = False
s = snake.copy()
ret = s.step(dir[i], allow_wrong=True)
#direction not allowed
if ret == -1:
del s
continue
#no decision needed
if ret == 2:
break_flag = True
string = s.get_prism_string()
state_dec = string[string.rfind('\n') + 1:]
state_dec_prob.append(string[string.rfind('-> 1:') + len(('-> 1:')):])
if s.steps - start_steps == max_steps:
files.append(s.write_file('out_'))
del s
if break_flag:
break
continue
while s.step(allow_wrong=True) != 1:
if s.steps - start_steps == max_steps:
files.append(s.write_file('out_'))
cont_flag = True
break
if break_flag and cont_flag:
break
if cont_flag:
del s
continue
recursion_step_f(s, max_steps, start_steps, files)
if break_flag:
break
recursion_step_mdp_f(s, max_steps, start_steps, file)
print_states = ''
state_dec = state_dec[:state_dec.rfind('-> 1:')+len('-> 1:')]
for sp in state_dec_prob:
print_states += f'{state_dec} {sp} \n'
if file:
file.write('\n'+print_states)
file.write(state + '\n')
def recursion_step_prob(snake, max_steps, start_steps, file=None):
dir = ['down', 'left', 'up', 'right']
......@@ -326,8 +333,6 @@ def recursion_step_prob(snake, max_steps, start_steps, file=None):
file.write(state_dec+'\n')
file.write(state + '\n')
def step_fast_forward(snake, dir, allow_wrong=False):
ret = snake.step(dir, allow_wrong=allow_wrong)
if ret == -1:
......@@ -335,36 +340,47 @@ def step_fast_forward(snake, dir, allow_wrong=False):
while snake.step(allow_wrong=allow_wrong) != 1:
snake.step(allow_wrong=allow_wrong)
def check_files_prism_thread_funct(file, dir, return_dict):
output = str(subprocess.check_output('cd ' + storm_folder + '; ./storm --prism ' + str(pathlib.Path().absolute()) + '/out/' + file + ' --prop \"Pmin =? [F \\\"crash\\\"]\"', shell=True))
output = output[output.rfind('Result (for initial states): ') + len('Result (for initial states): '):]
return_dict[dir] = float(output[:output.find('\\n')])
print()
def check_files_prism(files):
results = {}
for dir, file_list in files.items():
for file in file_list:
output = str(subprocess.check_output('cd '+ prism_folder + '; ./prism '+str(pathlib.Path().absolute())+'/out/'+file+' -pf \'Pmin =? [F \"crash\"]\'', shell=True))
output = output[output.rfind('Result: ')+len('Result: '):]
result = float(output[:output.find(' ')])
if dir in results:
if result < results[dir]:
results[dir] = result
else:
results[dir] = result
return results
# def check_files_prism(files):
# results = {}
# for dir, file_list in files.items():
# for file in file_list:
# program = stormpy.parse_prism_program('out/'+file)
# prop = "Pmin=? [ F \"crash\" ]"
# properties = stormpy.parse_properties_for_prism_program(prop, program, None)
# for file in file_list:
# output = str(subprocess.check_output('cd '+ prism_folder + '; ./prism '+str(pathlib.Path().absolute())+'/out/'+file+' -pf \'Pmin =? [F \"crash\"]\'', shell=True))
# output = output[output.rfind('Result: ')+len('Result: '):]
# result = float(output[:output.find(' ')])
# output = str(subprocess.check_output('cd '+storm_folder+'; ./storm --prism '+str(pathlib.Path().absolute())+'/out/'+file+' --prop \"Pmin =? [F \\\"crash\\\"]\"', shell=True))
# output = output[output.rfind('Result (for initial states): ') + len('Result (for initial states): '):]
# result = float(output[:output.find('\\n')])
processes = []
manager = multiprocessing.Manager()
return_dict = manager.dict()
for dir, file in files.items():
p = multiprocessing.Process(target=check_files_prism_thread_funct, args=(file, dir, return_dict))
p.start()
processes.append(p)
for p in processes:
p.join()
return return_dict
# def check_files_prism(files):
# results = {}
# for dir, file_list in files.items():
# for file in file_list:
# program = stormpy.parse_prism_program('out/'+file)
# prop = "Pmin=? [ F \"crash\" ]"
# properties = stormpy.parse_properties_for_prism_program(prop, program, None)
#
# model = stormpy.build_model(program, properties)
# # initial_state = model.initial_states[0]
# result = stormpy.model_checking(model, properties[0])
# if dir in results:
# if result < results[dir]:
# results[dir] = result
# else:
# model = stormpy.build_model(program, properties)
# # initial_state = model.initial_states[0]
# result = stormpy.model_checking(model, properties[0])
# if dir in results:
# if result < results[dir]:
# results[dir] = result
# else:
# results[dir] = result
def remove_files(files):
for dir, file_list in files.items():
......@@ -376,7 +392,8 @@ def remove_files(files):
# net_graph = nx.read_gpickle('graph1.pickle')
#map1
# s = snake([[2,0],[1,0],[0,0]])
# s = snake([[2,4],[1,4],[0,4]], postfix=postfix_enemy)
# s = snake([[2,0],[1,0],[0,0]], start_dir=True)
# step_fast_forward(s, 'down')
# step_fast_forward(s, 'right')
# step_fast_forward(s, 'left')
......@@ -391,7 +408,8 @@ def remove_files(files):
# s = snake([[6,13],[6,14],[6,15],[6,16],[6,17],[6,18],[6,19],[6,20],[6,21],[6,22]])
# map3
# s = snake([[5,1],[4,1],[3,1],[2,1],[2,2],[2,3],[2,4],[2,5],[2,6],[2,7]])
# s = snake([[19,20],[19,21],[19,22],[19,23],[19,24],[19,25],[19,26],[19,27],[19,28],[20,28]], postfix=postfix_enemy)
# s = snake([[5,1],[4,1],[3,1],[2,1],[2,2],[2,3],[2,4],[2,5],[2,6],[2,7]], start_dir=True)
#map4
# s = snake([[1,11],[1,12],[1,13],[1,14],[1,15],[1,16],[1,17],[1,18],[1,19],[1,20]])
......@@ -400,7 +418,7 @@ try:
os.remove('out/master.nm')
except OSError:
pass
t = time.time_ns()
# t = time.time_ns()
file = open('out/master.nm', 'a')
file.write(prism_start_string)
file.write("\nmodule enemysnake\n")
......@@ -411,10 +429,11 @@ file.write("endmodule\n")
file.close()
s = snake([[2,0],[1,0],[0,0]], start_dir=True)
files = recursion_step(s, max_steps)
files = recursion_step_mdp(s, max_steps, '/out/master.nm')
# print(files)
# print((time.time_ns() - t)*1e-9)
t = time.time_ns()
crash_prob = check_files_prism(files)
# print((time.time_ns() - t)*1e-9)
print((time.time_ns() - t)*1e-9)
remove_files(files)
print('\n'+str(crash_prob))
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment