This commit is contained in:
Torma Kristóf 2020-11-19 23:41:08 +01:00
parent f02a53afb4
commit 352dbeef06
Signed by: tormakris
GPG Key ID: DC83C4F2C41B1047
11 changed files with 1894 additions and 1 deletions

2
.gitignore vendored
View File

@ -128,4 +128,4 @@ dmypy.json
# Pyre type checker # Pyre type checker
.pyre/ .pyre/
.idea/

Binary file not shown.

10
Strats/tut.py Normal file
View File

@ -0,0 +1,10 @@
# neptun1 neptun2
def init_bw():
return 200
def decision(new, list_entry):
# print '--', new, list_entry
# print [1,[1 for x in list_entry]]
return [1,[1 for x in list_entry]]

17
Strats/tutgold.py Normal file
View File

@ -0,0 +1,17 @@
# neptun1 neptun2
def init_bw():
return 150
def decision(new, list_entry):
# print '--', new, list_entry
bw={'bronze': 1, 'silver': 3, 'gold': 10}
if new != 'gold':
br=len([x for x in list_entry if x[0]=='bronze'])
go=len([x for x in list_entry if x[0]=='bronze'])
si=len([x for x in list_entry if x[0]=='silver'])
bwn=sum([bw[x[0]] for x in list_entry])
if bwn > init_bw()-bw['gold']:
return [0,[1 for x in list_entry]]
return [1,[1 for x in list_entry]]

14
Strats/tutplus.py Normal file
View File

@ -0,0 +1,14 @@
# neptun1 neptun2
def init_bw():
return 80
def decision(new, list_entry):
# print '--', new, list_entry
if new == 'gold':
# print [1,[1 for x in list_entry]]
return [1,[1 for x in list_entry]]
else:
# print [0,[1 for x in list_entry]]
return [0,[1 for x in list_entry]]

17
Strats/tutpro.py Normal file
View File

@ -0,0 +1,17 @@
# neptun1 neptun2
def init_bw():
return 100
def decision(new, list_entry):
# print '--', new, list_entry
bw={'bronze': 1, 'silver': 3, 'gold': 10}
if new == 'gold':
b=sum([bw[x[0]] for x in list_entry])
if b+10>init_bw():
br=len([x for x in list_entry if x[0]=='bronze'])
si=len([x for x in list_entry if x[0]=='silver'])
if b+10-br<=init_bw():
return [1,[0 if x[0]=='bronze' else 0 for x in list_entry]]
return [1,[1 for x in list_entry]]

1
bw.json Normal file
View File

@ -0,0 +1 @@
{'bronze':{'bw': 1, 'value': 2, 'damage':4, 'duration': 100, 'pr': 0.4}, 'silver': {'bw': 3, 'value': 5, 'damage':6, 'duration': 50, 'pr': 0.3}, 'gold':{'bw': 10, 'value': 30, 'damage':80, 'duration': 70, 'pr': 0.1}}

1618
event1.txt Normal file

File diff suppressed because it is too large Load Diff

20
event_gen.py Normal file
View File

@ -0,0 +1,20 @@
import numpy as np
import numpy.random as rnd
def event_gen(n):
events=[]
for t in range(n):
r=rnd.choice(range(10))
if r<4:
events.append([t,'bronze'])
elif r<7:
events.append([t,'silver'])
elif r<8:
events.append([t,'gold'])
return events
if __name__ == "__main__":
events = event_gen(2000)
for event in events:
print (event[0], event[1])

101
sim.py Normal file
View File

@ -0,0 +1,101 @@
import os
import numpy as np
import numpy.random as rnd
import sys
import importlib
import imp
import ctypes
import ctypes.util
from numpy.ctypeslib import ndpointer
import event_gen as eg
PPP=60
claim_types={'bronze':{'bw': 1, 'value': 2, 'damage':4, 'duration': 100, 'pr': 0.4},
'silver': {'bw': 3, 'value': 5, 'damage':6, 'duration': 50, 'pr': 0.3},
'gold':{'bw': 10, 'value': 30, 'damage':80, 'duration': 70, 'pr': 0.1}}
class Claim:
def __init__(self,ty,time):
self.ty = ty
self.bw=claim_types[ty]['bw']
self.val=claim_types[ty]['value']
self.dam=claim_types[ty]['damage']
self.dur=claim_types[ty]['duration']
self.init_time=time
self.end_time=time+self.dur
class Sim:
def __init__(self,bw):
self.queue=[]
self.bw=bw
self.free=bw
self.budget=(-1)*PPP*self.bw
def step(self, lista):
self.budget-=sum([self.queue[i].dam for i in range(len(self.queue)) if lista[i]==0])
self.queue = [self.queue[i] for i in range(len(self.queue)) if lista[i]==1]
def new(self, item):
if item.bw<=self.free:
self.queue.append(item)
def process(self,time):
for i in range(len(self.queue)-1,-1,-1):
if self.queue[i].end_time<=time:
self.budget+=self.queue[i].val
del self.queue[i]
self.free=self.bw
for item in self.queue:
self.free-=item.bw
def get_queue_list(self,time):
lista=[]
for item in self.queue:
lista.append([item.ty,item.end_time-time])
return lista
results={}
folder='Strats'
sys.path.append(folder)
strats=os.listdir(folder)
strats_py=[x.split('.')[0] for x in strats if x.split('.')[-1]=="py"]
#strats_py=['tut_mymy']
for _ in range(10):
events=eg.event_gen(2000)
"""
os.system('python event_gen.py >event1.txt')
with open('event1.txt') as inf:
for line in inf:
a=line.split()
# print a
if a:
events.append([int(a[0]),a[1]])
#print events
"""
for strat in strats_py:
mod=importlib.import_module(strat)
sim=Sim(max(0,mod.init_bw()))
for e in events:
[time,claim]=e
sim.process(time)
[new,lista]=mod.decision(claim, sim.get_queue_list(time))
sim.step(lista)
if new==1:
sim.new(Claim(claim,time))
sim.process(time+1000)
if not strat in results:
results[strat]=0
results[strat]+=sim.budget
for key in results:
results[key]/=10.0
print results
"""
strat=strats[0]
mod=importlib.import_module(strat)
for _ in range(10):
print mod.decision(1,[3,3])
"""

95
sim_p3.py Normal file
View File

@ -0,0 +1,95 @@
import os
import numpy as np
import numpy.random as rnd
import sys
import importlib
#import imp
import ctypes
import ctypes.util
from numpy.ctypeslib import ndpointer
import event_gen as eg
PPP=60
claim_types={'bronze':{'bw': 1, 'value': 2, 'damage':4, 'duration': 100, 'pr': 0.4},
'silver': {'bw': 3, 'value': 5, 'damage':6, 'duration': 50, 'pr': 0.3},
'gold':{'bw': 10, 'value': 30, 'damage':80, 'duration': 70, 'pr': 0.1}}
class Claim:
def __init__(self,ty,time):
self.ty = ty
self.bw=claim_types[ty]['bw']
self.val=claim_types[ty]['value']
self.dam=claim_types[ty]['damage']
self.dur=claim_types[ty]['duration']
self.init_time=time
self.end_time=time+self.dur
class Sim:
def __init__(self,bw):
self.queue=[]
self.bw=bw
self.free=bw
self.budget=(-1)*PPP*self.bw
def step(self, lista):
self.budget-=sum([self.queue[i].dam for i in range(len(self.queue)) if lista[i]==0])
self.queue = [self.queue[i] for i in range(len(self.queue)) if lista[i]==1]
def new(self, item):
if item.bw<=self.free:
self.queue.append(item)
def process(self,time):
for i in range(len(self.queue)-1,-1,-1):
if self.queue[i].end_time<=time:
self.budget+=self.queue[i].val
del self.queue[i]
self.free=self.bw
for item in self.queue:
self.free-=item.bw
def get_queue_list(self,time):
lista=[]
for item in self.queue:
lista.append([item.ty,item.end_time-time])
return lista
results={}
folder='Strats'
sys.path.append(folder)
strats=os.listdir(folder)
strats_py=[x.split('.')[0] for x in strats if x.split('.')[-1]=="py"]
#strats_py=['tut_mymy']
for _ in range(10):
events=eg.event_gen(2000)
"""
os.system('python event_gen.py >event1.txt')
with open('event1.txt') as inf:
for line in inf:
a=line.split()
# print a
if a:
events.append([int(a[0]),a[1]])
#print events
"""
for strat in strats_py:
mod=importlib.import_module(strat)
sim=Sim(max(0,mod.init_bw()))
for e in events:
[time,claim]=e
sim.process(time)
[new,lista]=mod.decision(claim, sim.get_queue_list(time))
sim.step(lista)
if new==1:
sim.new(Claim(claim,time))
sim.process(time+1000)
if not strat in results:
results[strat]=0
results[strat]+=sim.budget
for key in results:
results[key]/=10.0
print(results)