Just another update
parent
7019f11e50
commit
e53b2cb918
@ -1,46 +1,199 @@
|
||||
from icfprequests import LibRequests
|
||||
import json
|
||||
import os.path
|
||||
import sys
|
||||
import random
|
||||
|
||||
class Room
|
||||
class ExploreError(Exception):
|
||||
pass
|
||||
|
||||
class LibraryMap:
|
||||
|
||||
def __init__(self, problemName:str, no_of_rooms:int, startRoom):
|
||||
self.no_of_rooms = no_of_rooms
|
||||
self.problemName = problemName
|
||||
self.rooms = [ startRoom ]
|
||||
self.startRoom = startRoom
|
||||
self.candidateRooms = []
|
||||
|
||||
def addRoom(self,room):
|
||||
if len(self.rooms)<self.no_of_rooms:
|
||||
self.rooms.append(room)
|
||||
|
||||
def __init__(self, room_id: str, pathcounter: int):
|
||||
self.id = room_id
|
||||
self.pathcount = pathcounter
|
||||
self.rooms = [None for _ in range(6)]
|
||||
|
||||
def createRoom(self,no: int,id):
|
||||
self.rooms[no]=Room(id,self.pathcount+1)
|
||||
class Room:
|
||||
|
||||
def full_id(self):
|
||||
def __init__(self, room_id, isStart=False):
|
||||
self.isStart = isStart
|
||||
self.id = room_id
|
||||
self.reachedby_id = None
|
||||
self.doors = [None for _ in range(6)]
|
||||
|
||||
def completed(self):
|
||||
for door in self.doors:
|
||||
if door is None:
|
||||
return False
|
||||
if self.id is None:
|
||||
return None
|
||||
fullid = self.id
|
||||
for room in self.rooms:
|
||||
if room is None:
|
||||
return None
|
||||
fullid = fullid + room.id
|
||||
return fullid
|
||||
|
||||
def rooms_without_id(self):
|
||||
none_rooms = []
|
||||
for doorcounter, room in enumerate(self.rooms):
|
||||
if room is None:
|
||||
none_rooms.append(doorcounter)
|
||||
return none_rooms
|
||||
|
||||
def setRoomId(self,id:str):
|
||||
if self.id is None & self.pathcount == 0:
|
||||
self.id =id
|
||||
else:
|
||||
print("Id can not be set for initial Path Element!")
|
||||
return False
|
||||
return True
|
||||
|
||||
def setRoomForDoor(self,door:int,room):
|
||||
self.doors[door]=room
|
||||
|
||||
def createRoomForDoor(self,door, id):
|
||||
self.doors[door]=Room(id)
|
||||
|
||||
class RoomPath:
|
||||
|
||||
def __init__(self, from_id, from_door, room_id, to_door, to_id ):
|
||||
self.from_id
|
||||
self.from_door
|
||||
self.room_id
|
||||
self.to_door
|
||||
self.to_id
|
||||
|
||||
|
||||
|
||||
def getFromId(self):
|
||||
return self.from_id + str(self.from_door) + self.room_id
|
||||
|
||||
def getToId(self):
|
||||
return self.room_id + str(self.to_door)+ self.to_id
|
||||
|
||||
def getFullId(self):
|
||||
return self.from_id + str(self.from_door) + self.room_id + str(self.to_door)+ self.to_id
|
||||
|
||||
|
||||
class LibMapper:
|
||||
|
||||
def __init__(self, no_of_rooms_to_find):
|
||||
self.no_of_rooms = no_of_rooms_to_find
|
||||
def __init__(self, problemName:str, no_of_rooms:int, processmode:str):
|
||||
self.no_of_rooms = no_of_rooms
|
||||
self.problemName = problemName
|
||||
self.processmode = processmode
|
||||
self.requester = LibRequests()
|
||||
self.libmap = None
|
||||
|
||||
def start(self):
|
||||
response = self.requester.select(self.problemName)
|
||||
if self.problemName != response:
|
||||
print("Select Request failed")
|
||||
else:
|
||||
self.explore_map()
|
||||
self.submit_guess()
|
||||
|
||||
def createRandomPlan(self,direction):
|
||||
plan = str(direction)
|
||||
# planzero = str(direction)+"[0]"
|
||||
for x in range(6*self.no_of_rooms-1):
|
||||
randdir = str(random.randint(0,5))
|
||||
plan += randdir
|
||||
# planzero += randdir + "[0]"
|
||||
return plan
|
||||
|
||||
def createAllDoorPlan(self):
|
||||
return "024135" * self.no_of_rooms
|
||||
|
||||
def createZeroedPlan(self, plan):
|
||||
zeroed_plan = ""
|
||||
for direction in plan:
|
||||
zeroed_plan += direction + "[0]"
|
||||
return zeroed_plan
|
||||
|
||||
def createinvertedPlans(self, plan_base, result_base):
|
||||
print(str(len(plan_base))+ " : "+str(len(result_base)))
|
||||
if len(plan_base) == len(result_base):
|
||||
inverted_plans = []
|
||||
for i in range(len(plan_base)):
|
||||
iplan = "[" + str(result_base[i][0]^3) + "]"
|
||||
for j in range(len(plan_base[i])):
|
||||
iplan += plan_base[i][j] + "[" + str(result_base[i][j+1]^3) + "]"
|
||||
inverted_plans.append(iplan)
|
||||
return inverted_plans
|
||||
else:
|
||||
print("Incompatible Input Array Dimensions")
|
||||
|
||||
def acquireExploreData(self):
|
||||
# acquire data
|
||||
print("Acquire")
|
||||
startdirections = [random.randint(0, 5)]
|
||||
plan_base = [self.createAllDoorPlan()]
|
||||
# [self.createRandomPlan(direction) for direction in startdirections]
|
||||
# plan_zero_base = [self.createZeroedPlan(plan) for plan in plan_base]
|
||||
plans = plan_base # + plan_zero_base
|
||||
print("Plans: " + str(plans))
|
||||
result = self.requester.explore(plans)
|
||||
print(result)
|
||||
result_base = result["results"][0:1]
|
||||
startRoomId = result_base[0][0]
|
||||
print(result_base)
|
||||
# create inverted plan (every id will be xored with 3 and set as current Room Id)
|
||||
plan_inverted_base = self.createinvertedPlans(plan_base, result_base)
|
||||
print(plan_inverted_base)
|
||||
result_inverted = self.requester.explore(plan_inverted_base)
|
||||
print(result_inverted)
|
||||
result_inverted_base = result_inverted["results"][0:1]
|
||||
print(result_inverted_base)
|
||||
return result_base[0], result_inverted_base[0], plan_inverted_base[0]
|
||||
|
||||
def explore_map(self):
|
||||
result_base, result_inverted_base = [] , []
|
||||
plan_inverted_base = ""
|
||||
if self.processmode != "cache":
|
||||
result_base, result_inverted_base, plan_inverted_base = self.acquireExploreData()
|
||||
if self.processmode == "store":
|
||||
self.storeResult(result_base, result_inverted_base, plan_inverted_base)
|
||||
if self.processmode == "cache":
|
||||
print("loading Data ...")
|
||||
result_base, result_inverted_base , plan_inverted_base= self.loadResults()
|
||||
print(result_base)
|
||||
print(result_inverted_base)
|
||||
print(plan_inverted_base)
|
||||
# calculate map
|
||||
print("Caluclate")
|
||||
print("-- Preperations")
|
||||
plan_base, inversions = self.splitInvertedPlan(plan_inverted_base)
|
||||
print(plan_base)
|
||||
print(inversions)
|
||||
room_pathes_base = self.createRoomPathes(result_base)
|
||||
print(room_pathes_base)
|
||||
room_pathes_inverted = self.createRoomPathes(result_inverted_base)
|
||||
print(room_pathes_inverted)
|
||||
|
||||
def createRoomPathes(self,result):
|
||||
pass
|
||||
|
||||
def splitInvertedPlan(self, plan_inverted_base):
|
||||
return plan_inverted_base[3::4], plan_inverted_base[1::4]
|
||||
|
||||
def submit_guess(self):
|
||||
pass
|
||||
|
||||
def storeResult(self, result_base, result_inverted_base,plan_inverted_base):
|
||||
dump = [ result_base , result_inverted_base , plan_inverted_base ]
|
||||
with open("result_dump.json", "w") as h:
|
||||
h.write(json.dumps(dump))
|
||||
|
||||
def loadResults(self):
|
||||
dump = {}
|
||||
try:
|
||||
with open("result_dump.json") as h:
|
||||
dump = json.loads(h.read())
|
||||
except OSError:
|
||||
pass
|
||||
return dump[0][0], dump[1][0], dump[2][0]
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
problem = sys.argv[1]
|
||||
processmode = sys.argv[2] # normal (webcall), store (Store Callresult), cache (skip Webcall and load stored data)
|
||||
|
||||
with open(os.path.join("..", "problems.json")) as h:
|
||||
problems = json.loads(h.read())
|
||||
|
||||
problems = {p["problem"]: {"size": p["size"]} for p in problems}
|
||||
|
||||
if problem not in problems:
|
||||
raise ExploreError(f"unknown problem {problem}")
|
||||
mapper = LibMapper(problem,problems[problem]["size"], processmode)
|
||||
mapper.start()
|
||||
@ -0,0 +1,96 @@
|
||||
from michael.icfprequests import LibRequests
|
||||
import json
|
||||
import os.path
|
||||
import sys
|
||||
import random
|
||||
|
||||
class Room:
|
||||
|
||||
def __init__(self, room_id: str, parent, path:str):
|
||||
self.id = room_id
|
||||
self.parent = parent
|
||||
self.path = path
|
||||
self.rooms = [None for _ in range(6)]
|
||||
self.double = False
|
||||
|
||||
def createRoom(self,no: int,id):
|
||||
self.rooms[no]=Room(id,self,self.path+str(no))
|
||||
|
||||
def full_id(self):
|
||||
if self.id is None:
|
||||
return None
|
||||
fullid = self.id
|
||||
for room in self.rooms:
|
||||
if room is None:
|
||||
return None
|
||||
fullid = fullid + room.id
|
||||
return fullid
|
||||
|
||||
def rooms_without_id(self):
|
||||
none_rooms = []
|
||||
for doorcounter, room in enumerate(self.rooms):
|
||||
if room is None:
|
||||
none_rooms.append(self.path+str(doorcounter))
|
||||
return none_rooms
|
||||
|
||||
|
||||
|
||||
def setRoomId(self,id:str):
|
||||
if self.id is None and self.parent is None and self.path =="":
|
||||
self.id =id
|
||||
else:
|
||||
print("Id can not be set for initial Path Element!")
|
||||
|
||||
|
||||
class LibMapper:
|
||||
|
||||
def __init__(self, problemName:str, no_of_rooms:int):
|
||||
self.no_of_rooms = no_of_rooms
|
||||
self.problemName = problemName
|
||||
self.requester = LibRequests()
|
||||
self.startRoom = Room(None,None,"")
|
||||
self.rooms_list = {}
|
||||
|
||||
def start(self):
|
||||
response = self.requester.select(self.problemName)
|
||||
if self.problemName != response:
|
||||
print("Select Request failed")
|
||||
else:
|
||||
self.explore_map(self.startRoom)
|
||||
self.submit_guess()
|
||||
|
||||
def explore_map(self, startRoom):
|
||||
plans = startRoom.rooms_without_id()
|
||||
print("Plans: "+str(plans))
|
||||
result = self.requester.explore(plans)
|
||||
print(result)
|
||||
if startRoom.id is None:
|
||||
startRoom.setRoomId(result["results"][0][0])
|
||||
for counter,result in enumerate(result["results"]):
|
||||
startRoom.createRoom(counter,result[1])
|
||||
fullid = startRoom.full_id()
|
||||
if not fullid is None:
|
||||
if self.rooms_list.get(fullid) is None:
|
||||
self.rooms_list[fullid] = startRoom
|
||||
else:
|
||||
startRoom.double = True
|
||||
|
||||
|
||||
|
||||
def submit_guess(self):
|
||||
pass
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
problem = sys.argv[1]
|
||||
|
||||
with open(os.path.join("..", "problems.json")) as h:
|
||||
problems = json.loads(h.read())
|
||||
|
||||
problems = {p["problem"]: {"size": p["size"]} for p in problems}
|
||||
|
||||
if problem not in problems:
|
||||
raise ExploreError(f"unknown problem {problem}")
|
||||
mapper = LibMapper(problem,problems[problem]["size"])
|
||||
mapper.start()
|
||||
Loading…
Reference in New Issue