def _generate_data(self, data): '''Generate data for the generation''' solution_fitness_items = [] solution_items = {} solutions = [] for i in range(0, self.population): rooms = Rooms(ROOMS_CSV) slots = Slots(rooms) courses = Courses(CLASSES_CSV) schedule = data[i] if data else Schedule(rooms, slots, courses) solutions.append(schedule) schedule_fitness = schedule.get_fitness() solution_fitness_items.append(schedule_fitness) solution_fitness_items.sort() if schedule_fitness in solution_items.keys(): solution_items[schedule_fitness][schedule.id] = schedule else: solution_items[schedule_fitness] = {} solution_items[schedule_fitness][schedule.id] = schedule self.elites = self._generate_elite_list(solution_items) self.avg_solution_fitness = round( sum(solution_fitness_items) / len(solution_fitness_items)) return solutions
def main(): parser = argparse.ArgumentParser(description='SM Room Timer') parser.add_argument('-f', '--file', dest='filename', default=None) parser.add_argument('--rooms', dest='rooms_filename', default='rooms.json') parser.add_argument('--doors', dest='doors_filename', default='doors.json') parser.add_argument('--segment', dest='segments', action='append', default=[]) parser.add_argument('--split', dest='splits', action='append', default=[]) parser.add_argument('--splits', dest='splits_filename') parser.add_argument('--brief', action='store_true') args = parser.parse_args() rooms = Rooms.read(args.rooms_filename) doors = Doors.read(args.doors_filename, rooms) history = read_transition_log(args.filename, rooms, doors) # ids = build_route(history) if args.build_route else history.keys() route = build_route(history) # TODO: Needed? split_names = args.splits if args.splits_filename is not None: with open(args.splits_filename) as f: lines = [ line for line in f.readlines() if not line.startswith('#') and not line.isspace() ] split_names.extend([ line.strip() for line in lines ]) segments = [ segment_from_name(name, rooms, route) for name in args.segments ] splits = [ transition_from_name(name, rooms, route) for name in split_names ] segments.extend(segments_from_splits(route, splits)) if not args.brief: segment_history = build_segment_history(segments, history) print_room_stats(history, segment_history, segments) print_segment_stats(history, segments)
def main(): parser = argparse.ArgumentParser(description='SM Room Timer') parser.add_argument('-f', '--file', dest='filename', default=None) parser.add_argument('--rooms', dest='rooms_filename', default='rooms.json') parser.add_argument('--doors', dest='doors_filename', default='doors.json') parser.add_argument('--segment', dest='segments', action='append', default=[]) parser.add_argument('--split', dest='splits', action='append', default=[]) parser.add_argument('--splits', dest='splits_filename') args = parser.parse_args() rooms = Rooms.read(args.rooms_filename) doors = Doors.read(args.doors_filename, rooms) with open(args.filename) as csvfile: tailer = Tailer(csvfile) history_reader = read_transition_log_csv_incrementally( tailer, rooms, doors) while not tailer.at_eof(): history, transition = next(history_reader) # ids = build_route(history) if args.build_route else history.keys() route = build_route(history) # TODO: Needed? split_names = args.splits if args.splits_filename is not None: with open(args.splits_filename) as f: lines = [ line for line in f.readlines() if not line.startswith('#') and not line.isspace() ] split_names.extend([line.strip() for line in lines]) segments = [ segment_from_name(name, rooms, route) for name in args.segments ] splits = [ transition_from_name(name, rooms, route) for name in split_names ] segments.extend(segments_from_splits(route, splits)) old_stats = SegmentStats(history, segments) old_rendered_stats = None while True: stats = SegmentStats(history, segments) rendered_stats = render_segment_stats(old_stats, stats) if rendered_stats != old_rendered_stats: print() print(rendered_stats) old_rendered_stats = rendered_stats old_stats = stats history, transition = next(history_reader)
def add_room(data): room = (data["room"]).strip() if (not len(room) or len(room) > 10): emit("submit error", "room-name") elif room not in rooms: rooms[room] = Rooms(room) emit("add room", room, broadcast=True) else: # Alerts only user, who tried adding room emit("submit error", "room-name")
def run_tests(): """ Runs all tests defined in task2_tests.yaml. Returns: results: Results dataframe. """ with open(FILENAME) as file: # Loads testing parameters from the yaml file. tests = yaml.safe_load(file) # create a dataframe to keep the results test_dict = tests['Tests'] results = pd.DataFrame(test_dict) results['Last Average Score'] = "" results['No of Q-Learning episodes'] = "" # run experiments: for i, test in enumerate(test_dict): grid = Rooms(test["env_size"], testing=True) learning = QLearning(grid, test["gamma"], test["alpha"], test["agent_start_pos"]) e_greedy = Policy("e-greedy", test["epsilon"], test["decay"]) greedy = Policy(policy_type="greedy") experiment = Experiments(grid, learning, greedy, test["iters"], test["agent_start_pos"], test["test_no"]) for session in range(test["iters"]): learning.run_multiple_episodes(test["batch_episodes"], e_greedy) mean_reward = experiment.run_experiments(test["exp_per_batch"]) results.loc[i, 'Last Average Score'] = mean_reward results.loc[i, 'No of Q-Learning episodes'] = (session + 1) * test["batch_episodes"] # save results to csv file filename = 'results/' + 'test_table.csv' results.to_csv(filename) # plot & save graphs experiment.generate_results(test["test_no"], test) return results
l = by_room_and_exit.get(key) if l: if len(l) == 1: print("Fixing entry door on line %d" % n) transition.id.entry_door = list( l.keys())[0].entry_door else: print( "More than one entry door found for %s to $s (line %d): %s" % (transition.id.room, transition.id.exit_room, n, repr(l))) else: # print("No entry door found for %s to %s (line %d)" % # (transition.id.room, transition.id.exit_room, n)) pass writer.writerow(transition.as_csv_row()) if __name__ == '__main__': parser = argparse.ArgumentParser(description='SM Room Timer') parser.add_argument('-i', '--input', dest='inputs', action='append') parser.add_argument('-o', '--output', dest='output', default=None) parser.add_argument('--rooms', dest='rooms_filename', default='rooms.json') parser.add_argument('--doors', dest='doors_filename', default='doors.json') args = parser.parse_args() rooms = Rooms.read(args.rooms_filename) doors = Doors.read(args.doors_filename, rooms) rebuild_history(rooms, doors, args.inputs, args.output)
def main(): parser = argparse.ArgumentParser(description='SM Room Timer') parser.add_argument('-f', '--file', dest='filename', default=None) parser.add_argument('--rooms', dest='rooms_filename', default='rooms.json') parser.add_argument('--doors', dest='doors_filename', default='doors.json') parser.add_argument('--debug', dest='debug', action='store_true') parser.add_argument('--debug-log', dest='debug_log_filename') parser.add_argument('--verbose', dest='verbose', action='store_true') parser.add_argument('--usb2snes', action='store_true') parser.add_argument('--route', action='store_true') parser.add_argument('--rebuild', action='store_true') parser.add_argument('--port', type=int, default=15000) parser.add_argument('--headless', action='store_true') # parser.add_argument('--segment', action='append', required=True) args = parser.parse_args() rooms = Rooms.read(args.rooms_filename) doors = Doors.read(args.doors_filename, rooms) route = Route() if args.route else DummyRoute() if args.filename and need_rebuild(args.filename): if not args.rebuild: print( "File needs to be rebuilt before it can be used; run rebuild_history.py or pass --rebuild to this script." ) sys.exit(1) backup_and_rebuild(rooms, doors, args.filename) if args.debug_log_filename: debug_log = open(args.debug_log_filename, 'a') verbose = True elif args.debug: debug_log = sys.stdout verbose = True else: debug_log = None verbose = args.verbose shutdown = [] try: server = WebsocketServer(port=args.port) server.start() shutdown.append(server.stop) json_generator = JsonEventGenerator(verbose=verbose, debug_log=debug_log, on_event=server.broadcast) if args.filename is not None and os.path.exists(args.filename): history = read_transition_log(args.filename, rooms, doors) else: history = History() for tid in history: route.record(tid) if route.complete: break print('Route is %s' % ('complete' if route.complete else 'incomplete')) transition_log = FileTransitionLog( args.filename) if args.filename is not None else NullTransitionLog( ) tracker = SegmentTimeTracker( history, transition_log, route, on_new_room_time=json_generator.new_room_time) timer_thread = TimerThread(history, rooms, doors, transition_log, route, json_generator, server, usb2snes=args.usb2snes) timer_thread.start() shutdown.append(timer_thread.stop) if args.headless: timer_thread.join() else: dirname = os.path.dirname(os.path.realpath(__file__)) filename = 'sm_web_timer.html' port = args.port url = 'file://%s/%s?port=%s' % (dirname, filename, port) browser = Browser(sys.argv, url) sys.exit(browser.run()) finally: for f in reversed(shutdown): try: f() except Exception as e: print(e)
import os import time from flask import Flask, render_template, request, redirect, url_for from flask_socketio import SocketIO, emit, send, join_room, leave_room from rooms import Rooms app = Flask(__name__) app.config["SECRET_KEY"] = os.getenv("SECRET_KEY") socketio = SocketIO(app) rooms = {"general": Rooms("general")} @app.route("/", methods=["GET", "POST"]) def index(): if request.method == "POST": return redirect(url_for("chat")) else: return render_template("index.html") @app.route("/chat") def chat(): return render_template("chat.html", rooms=rooms.keys()) @socketio.on("submit room") def add_room(data): room = (data["room"]).strip()
f"\tWell Done!\n\tYou have reached the Sub-Optimal Goal\n\tYour total reward is:{total_reward}\t¦\tIn {step+1} Steps\n\tGood Job!!" ) time.sleep(3) else: print( "\tBAD NEWS!!!\n\tSadnly, you have Died\n\tYou either collided with a tornado or you ran out of time\n\tBetter Luck next time!" ) time.sleep(3) break state = new_state #%% #Create Environment game = Rooms(10) game.reset() #Create Q-table q_table = np.zeros((game.states.size, len(game.actions_dict))) #%% # Define Hyper-Parameters turns = 10000 steps = 500 alpha = 0.6 #learning rate gamma = 0.99 #discount rate epsilon = 1 #exploration_rate epsilon_decay = 0.99 #exploration_decay_rate
def rungame(): skiptitle = False if(skiptitle == True): Rooms() else: None _ = system('cls') time.sleep(1) print( "\n\n\n\n\n\n\n\n\n\n\n" " A long time ago\n" " In a galaxy far, far away...\n" ) time.sleep(4.5) answer = input("\n\n\n\n\n\n\n\n\nPress ENTER to Continue (Press Q + ENTER to Skip) ") if answer.lower() == "q": _ = system('cls') Rooms() else: None time.sleep(0.5) _ = system('cls') # play music mixer.init() mixer.music.load('ThemeSong.mp3') mixer.music.play() print( "\n\n\n\n" " ________________. ___ .______\n" " / | / \ | _ \\\n" " | (-----| |----`/ ^ \ | |_) |\n" " \ \ | | / /_\ \ | /\n" " .-----) | | | / _____ \ | |\ \-------.\n" " |________/ |__| /__/ \__\| _| `.________|\n" " ____ __ ____ ___ .______ ________.\n" " \ \ / \ / / / \ | _ \ / |\n" " \ \/ \/ / / ^ \ | |_) || (-----`\n" " \ / / /_\ \ | / \ \\\n" " \ /\ / / _____ \ | |\ \---) |\n" " \__/ \__/ /__/ \__\|__| `._______/\n" "\n" " ------------------------------------------------" " Created By: Gabriel Wolf" "\n\n" ) time.sleep(5) answer = input("\n\n" " Press ENTER to Continue...\n" " Or Q + ENTER to Skip\n)") if answer.lower() == "q": mixer.music.fadeout(3000) time.sleep(3) _ = system('cls') Rooms() else: None # stop music mixer.music.fadeout(3000) time.sleep(3) _ = system('cls') time.sleep(0.5) print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n") print(" It is a period of civil war.") time.sleep(0.5) _ = system('cls') print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n") print(" It is a period of civil war.") print(" Rebel spaceships, striking") time.sleep(0.5) _ = system('cls') print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n") print(" It is a period of civil war.") print(" Rebel spaceships, striking") print(" Rebel spaceships, striking") time.sleep(0.5) _ = system('cls') print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n") print(" It is a period of civil war.") print(" Rebel spaceships, striking") print(" Rebel spaceships, striking") print(" from a hidden base, have won") time.sleep(0.5) _ = system('cls') print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n") print(" It is a period of civil war.") print(" Rebel spaceships, striking") print(" Rebel spaceships, striking") print(" from a hidden base, have won") print(" their first victory against") time.sleep(0.5) _ = system('cls') print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n") print(" It is a period of civil war.") print(" Rebel spaceships, striking") print(" Rebel spaceships, striking") print(" from a hidden base, have won") print(" their first victory against") print(" the evil Galactic Empire.") time.sleep(0.5) _ = system('cls') print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n") print(" It is a period of civil war.") print(" Rebel spaceships, striking") print(" Rebel spaceships, striking") print(" from a hidden base, have won") print(" their first victory against") print(" the evil Galactic Empire.") print(" ") time.sleep(0.5) _ = system('cls') print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n") print(" It is a period of civil war.") print(" Rebel spaceships, striking") print(" Rebel spaceships, striking") print(" from a hidden base, have won") print(" their first victory against") print(" the evil Galactic Empire.") print(" ") print(" During the battle, Rebel") time.sleep(0.5) _ = system('cls') print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n") print(" It is a period of civil war.") print(" Rebel spaceships, striking") print(" Rebel spaceships, striking") print(" from a hidden base, have won") print(" their first victory against") print(" the evil Galactic Empire.") print(" ") print(" During the battle, Rebel") print(" spies managed to steal secret") time.sleep(0.5) _ = system('cls') print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n") print(" It is a period of civil war.") print(" Rebel spaceships, striking") print(" Rebel spaceships, striking") print(" from a hidden base, have won") print(" their first victory against") print(" the evil Galactic Empire.") print(" ") print(" During the battle, Rebel") print(" spies managed to steal secret") print(" plans to the Empire's") time.sleep(0.5) _ = system('cls') print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n") print(" It is a period of civil war.") print(" Rebel spaceships, striking") print(" Rebel spaceships, striking") print(" from a hidden base, have won") print(" their first victory against") print(" the evil Galactic Empire.") print(" ") print(" During the battle, Rebel") print(" spies managed to steal secret") print(" plans to the Empire's") print(" ultimate weapon, the DEATH") time.sleep(0.5) _ = system('cls') print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n") print(" It is a period of civil war.") print(" Rebel spaceships, striking") print(" Rebel spaceships, striking") print(" from a hidden base, have won") print(" their first victory against") print(" the evil Galactic Empire.") print(" ") print(" During the battle, Rebel") print(" spies managed to steal secret") print(" plans to the Empire's") print(" ultimate weapon, the DEATH") print(" STAR, an armored space") time.sleep(0.5) _ = system('cls') print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n") print(" It is a period of civil war.") print(" Rebel spaceships, striking") print(" Rebel spaceships, striking") print(" from a hidden base, have won") print(" their first victory against") print(" the evil Galactic Empire.") print(" ") print(" During the battle, Rebel") print(" spies managed to steal secret") print(" plans to the Empire's") print(" ultimate weapon, the DEATH") print(" STAR, an armored space") print(" station with enough power to") time.sleep(0.5) _ = system('cls') print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n") print(" It is a period of civil war.") print(" Rebel spaceships, striking") print(" Rebel spaceships, striking") print(" from a hidden base, have won") print(" their first victory against") print(" the evil Galactic Empire.") print(" ") print(" During the battle, Rebel") print(" spies managed to steal secret") print(" plans to the Empire's") print(" ultimate weapon, the DEATH") print(" STAR, an armored space") print(" station with enough power to") print(" destroy an entire planet.") time.sleep(0.5) _ = system('cls') print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n") print(" It is a period of civil war.") print(" Rebel spaceships, striking") print(" Rebel spaceships, striking") print(" from a hidden base, have won") print(" their first victory against") print(" the evil Galactic Empire.") print(" ") print(" During the battle, Rebel") print(" spies managed to steal secret") print(" plans to the Empire's") print(" ultimate weapon, the DEATH") print(" STAR, an armored space") print(" station with enough power to") print(" destroy an entire planet.") print(" ") time.sleep(0.5) _ = system('cls') print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n") print(" It is a period of civil war.") print(" Rebel spaceships, striking") print(" Rebel spaceships, striking") print(" from a hidden base, have won") print(" their first victory against") print(" the evil Galactic Empire.") print(" ") print(" During the battle, Rebel") print(" spies managed to steal secret") print(" plans to the Empire's") print(" ultimate weapon, the DEATH") print(" STAR, an armored space") print(" station with enough power to") print(" destroy an entire planet.") print(" ") print(" Pursued by the Empire's") time.sleep(0.5) _ = system('cls') print("\n\n\n\n\n\n\n\n\n\n\n\n\n") print(" It is a period of civil war.") print(" Rebel spaceships, striking") print(" Rebel spaceships, striking") print(" from a hidden base, have won") print(" their first victory against") print(" the evil Galactic Empire.") print(" ") print(" During the battle, Rebel") print(" spies managed to steal secret") print(" plans to the Empire's") print(" ultimate weapon, the DEATH") print(" STAR, an armored space") print(" station with enough power to") print(" destroy an entire planet.") print(" ") print(" Pursued by the Empire's") print(" sinister agents, Princess") time.sleep(0.5) _ = system('cls') print("\n\n\n\n\n\n\n\n\n\n\n\n") print(" It is a period of civil war.") print(" Rebel spaceships, striking") print(" Rebel spaceships, striking") print(" from a hidden base, have won") print(" their first victory against") print(" the evil Galactic Empire.") print(" ") print(" During the battle, Rebel") print(" spies managed to steal secret") print(" plans to the Empire's") print(" ultimate weapon, the DEATH") print(" STAR, an armored space") print(" station with enough power to") print(" destroy an entire planet.") print(" ") print(" Pursued by the Empire's") print(" sinister agents, Princess") print(" Leia races home aboard her") time.sleep(0.5) _ = system('cls') print("\n\n\n\n\n\n\n\n\n\n\n") print(" It is a period of civil war.") print(" Rebel spaceships, striking") print(" Rebel spaceships, striking") print(" from a hidden base, have won") print(" their first victory against") print(" the evil Galactic Empire.") print(" ") print(" During the battle, Rebel") print(" spies managed to steal secret") print(" plans to the Empire's") print(" ultimate weapon, the DEATH") print(" STAR, an armored space") print(" station with enough power to") print(" destroy an entire planet.") print(" ") print(" Pursued by the Empire's") print(" sinister agents, Princess") print(" Leia races home aboard her") print(" starship, custodian of the") time.sleep(0.5) _ = system('cls') print("\n\n\n\n\n\n\n\n\n\n") print(" It is a period of civil war.") print(" Rebel spaceships, striking") print(" Rebel spaceships, striking") print(" from a hidden base, have won") print(" their first victory against") print(" the evil Galactic Empire.") print(" ") print(" During the battle, Rebel") print(" spies managed to steal secret") print(" plans to the Empire's") print(" ultimate weapon, the DEATH") print(" STAR, an armored space") print(" station with enough power to") print(" destroy an entire planet.") print(" ") print(" Pursued by the Empire's") print(" sinister agents, Princess") print(" Leia races home aboard her") print(" starship, custodian of the") print(" stolen plans that can save") time.sleep(0.5) _ = system('cls') print("\n\n\n\n\n\n\n\n\n") print(" It is a period of civil war.") print(" Rebel spaceships, striking") print(" Rebel spaceships, striking") print(" from a hidden base, have won") print(" their first victory against") print(" the evil Galactic Empire.") print(" ") print(" During the battle, Rebel") print(" spies managed to steal secret") print(" plans to the Empire's") print(" ultimate weapon, the DEATH") print(" STAR, an armored space") print(" station with enough power to") print(" destroy an entire planet.") print(" ") print(" Pursued by the Empire's") print(" sinister agents, Princess") print(" Leia races home aboard her") print(" starship, custodian of the") print(" stolen plans that can save") print( "her people and restore") time.sleep(0.5) _ = system('cls') print("\n\n\n\n\n\n\n\n") print(" It is a period of civil war.") print(" Rebel spaceships, striking") print(" Rebel spaceships, striking") print(" from a hidden base, have won") print(" their first victory against") print(" the evil Galactic Empire.") print(" ") print(" During the battle, Rebel") print(" spies managed to steal secret") print(" plans to the Empire's") print(" ultimate weapon, the DEATH") print(" STAR, an armored space") print(" station with enough power to") print(" destroy an entire planet.") print(" ") print(" Pursued by the Empire's") print(" sinister agents, Princess") print(" Leia races home aboard her") print(" starship, custodian of the") print(" stolen plans that can save") print(" her people and restore") print(" reedom to the galaxy.....") time.sleep(0.5) _ = system('cls') print("\n\n\n\n\n\n\n") time.sleep(1.75) Rooms()
def main(): parser = argparse.ArgumentParser(description='SM Room Timer') parser.add_argument('-f', '--file', dest='filename', default=None) parser.add_argument('--rooms', dest='rooms_filename', default='rooms.json') parser.add_argument('--doors', dest='doors_filename', default='doors.json') parser.add_argument('--debug', dest='debug', action='store_true') parser.add_argument('--debug-log', dest='debug_log_filename') parser.add_argument('--verbose', dest='verbose', action='store_true') parser.add_argument('--usb2snes', action='store_true') parser.add_argument('--route', action='store_true') parser.add_argument('--rebuild', action='store_true') # parser.add_argument('--segment', action='append', required=True) args = parser.parse_args() rooms = Rooms.read(args.rooms_filename) doors = Doors.read(args.doors_filename, rooms) route = Route() if args.route else DummyRoute() if args.filename and need_rebuild(args.filename): if not args.rebuild: print( "File needs to be rebuilt before it can be used; run rebuild_history.py or pass --rebuild to this script." ) sys.exit(1) backup_and_rebuild(rooms, doors, args.filename) if args.debug_log_filename: debug_log = open(args.debug_log_filename, 'a') verbose = True elif args.debug: debug_log = sys.stdout verbose = True else: debug_log = None verbose = args.verbose frontend = SegmentTimerTerminalFrontend(verbose=verbose, debug_log=debug_log) if args.filename is not None and os.path.exists(args.filename): history = read_transition_log(args.filename, rooms, doors) else: history = History() for tid in history: route.record(tid) if route.complete: break print('Route is %s' % ('complete' if route.complete else 'incomplete')) transition_log = FileTransitionLog( args.filename) if args.filename is not None else NullTransitionLog() tracker = SegmentTimeTracker(history, transition_log, route, on_new_room_time=frontend.new_room_time) state_reader = ThreadedStateReader(rooms, doors, usb2snes=args.usb2snes, logger=frontend) state_reader.start() try: timer = SegmentTimer(frontend, state_reader, on_transitioned=tracker.transitioned, on_state_change=frontend.state_changed, on_reset=tracker.room_reset) while state_reader.is_alive(): timer.poll() finally: state_reader.stop()
from data import * from rooms import Rooms print(Rooms(entrance)) print(Rooms(entrance).my_choice()) print(Rooms(second_stage)) print(Rooms(second_stage).my_choice()) print(Rooms(third_stage)) print(Rooms(third_stage).my_choice()) print(Rooms(fourth_stage)) print(Rooms(fourth_stage).my_choice()) print(Rooms(fifth_stage)) print(Rooms(fifth_stage).my_choice())
def setUp(self): self.new_room = Rooms("general") self.new_room.message_limit = 5
class TestRoom(unittest.TestCase): def setUp(self): self.new_room = Rooms("general") self.new_room.message_limit = 5 def test_name_of_the_room(self): self.assertEqual(self.new_room.name, "general") def test_adding_user(self): self.new_room.append_user("John", 245) self.assertIn(245, self.new_room.users) def test_removing_user(self): self.new_room.append_user("John", 275) self.new_room.remove_user(275) self.assertNotIn(275, self.new_room.users) def test_adding_message(self): self.new_room.append_message("Hello!") self.assertIn("Hello!", self.new_room.messages) def test_enforcing_limit(self): self.new_room.messages = ["Hello", "Hi"] self.new_room.enforce_max_messages() self.assertEqual(len(self.new_room.messages), 2) def test_enforcing_limit2(self): self.new_room.messages = [ "Hello", "Hi", "Tony", "How are you?", "good", "bad" ] self.new_room.enforce_max_messages() self.assertEqual(len(self.new_room.messages), 5)
from engine import Engine from curses_ui import UI from clock import Clock from autotracker import Autotracker if __name__ == '__main__': parser = argparse.ArgumentParser(description='Zebbo Probability Engine') parser.add_argument('--autotrack', dest='autotrack', action='store_true', default=False) parser.add_argument('--clock', dest='clock', action='store_true', default=False) args = parser.parse_args() items = Items.read('items.json') locations = Locations.read('locations.json') geography = Geography.read(locations, 'geography.json') rooms = Rooms.read('rooms.json') seeds = Seeds.read(items, locations, 'seeds.json') engine = Engine(items, locations, geography, rooms, seeds) if args.clock: clock = Clock() else: clock = None if args.autotrack: autotracker = Autotracker(engine, clock) poll = autotracker.poll else: poll = lambda: None UI.run(items=items, engine=engine, poll=poll, clock=clock)
def load_site_info(self): self.rooms = Rooms() # Rooms represent all the rooms in a site self.rooms.load_site_info(self.rmbs, self.area) # Load all the room info from DB
class Site: CHECK_FAILED_MEETING_SIZE = -1 CHECK_FAILED_NO_OF_MEETINGS = -2 CHECK_FIXED_ROOM_CONFLICT = -3 def __init__(self, rmbs, area: int, num_timeslots=None): self.rmbs = rmbs self.area = area # In RMBS, area represents a "building". E.g. 教育樓 if num_timeslots is None: # let's assume 8am to 10pm right now # Each timeslot is 30 mins num_timeslots = (22 - 8 + 1) * 2 self.num_timeslots = num_timeslots self.timeslots = list(range(num_timeslots)) self.rooms = None self.meetings = None self.requests = [] self.__timeslot_requests = None self._solver = cp_model.CpSolver() self._solver.parameters.linearization_level = 0 self._lastBookings = None # the solutions from the last CP solver self._lastStatus = None self._dateLoaded = None def load_site_info(self): self.rooms = Rooms() # Rooms represent all the rooms in a site self.rooms.load_site_info(self.rmbs, self.area) # Load all the room info from DB def load_existing_meetings(self, meeting_date: date, ratio=1.0): assert self.rooms is not None, "Can't be called until all room info was loaded already" self.meetings = Meetings(site=self) self.meetings.load_meeting_requests( self.rmbs, self.area, meeting_date, ratio) # Load meetings info from DB for m in self.meetings: assert m.room_name is None or m.room_name in self.rooms.room_names, f"Room '{m.room_name}' is not found" self.detect_related_meetings() self._dateLoaded = meeting_date def load_new_requests(self, path=None, df_requests=None): if path: df_requests = pd.read_csv(path) print(f"Records read: {len(df_requests)}") else: assert df_requests is not None fac_types = self.rmbs.read_facility_types().query( f'area_id == {self.area}')['type'].to_list() for request in df_requests.itertuples(): if path: # This is for reading the input file used during development for testing name = request.name start, end = Util.parse_time_field(request.time) size, min_size = Util.parse_size(request.size) description = '' facilities = request.facilities if pd.isna(facilities): facilities = None else: facilities = facilities.split(",") facilities = [x.strip() for x in facilities] for fac in facilities: assert fac in fac_types, f"{name} requested facility {fac} is not found in DB" else: name = Application.to_event_name(request._asdict()) start = request.start_time end = request.stop_time size = min_size = request.size description = Application.to_description(request._asdict()) # TODO: facilities aren't available in the form right now facilities = None if name.startswith("skip"): print(f'Request {name} is skipped') continue meeting = self.addMeeting(name, size, description=description, min_size=min_size, start_time=start, end_time=end, facilities=facilities) self.requests.append(meeting) def detect_related_meetings(self): all_meetings = sorted( self.meetings._meetings, key=lambda m: m.name ) # TODO: WIP. E.g. Two 連貫 meetings: 馬其頓團契練歌,馬其頓團契 def addMeeting(self, name, size, description='', min_size=0, start_timeslot=None, start_time=None, end_time=None, duration=None, facilities=None): meeting = Meeting(name=name, description=description, size=size, min_size=min_size, meetings=self.meetings, start_timeslot=start_timeslot, start_time=start_time, end_time=end_time, duration=duration, facilities=facilities) self.meetings.add_meeting(meeting) self.__timeslot_requests = None return meeting @property def max_room_size(self): return self.rooms.max_cap @property def max_timeslot(self): return self.num_timeslots - 1 @property def timeslot_requests(self): if self.__timeslot_requests is None: self.__timeslot_requests = defaultdict(list) for meeting in self.meetings: for t in meeting.meeting_times: self.__timeslot_requests[t].append(meeting) return self.__timeslot_requests def printConfig(self, print_meetings=True, print_rooms=True, print_timeslots=True): if print_meetings: print("---------------\n") for meeting in self.meetings: name = meeting.name if meeting.unit: name += f" <{meeting.unit}>" if meeting.room_name: name += f" : {meeting.room_name}" print( f"Meeting {name}: size={meeting.size}, timeslots={str(meeting.meeting_times)}" ) if print_rooms: print("---------------\n") for room in self.rooms: if room.facilities: fac = f'[{",".join(room.facilities)}]' else: fac = '' print(f"Room {room.name}: cap={room.room_cap} {fac}") if print_timeslots: print("---------------\n") for t in self.timeslots: ms = [] for m in self.timeslot_requests[t]: name = m.name if m.room_name: name += f" : {m.room_name}" ms.append(name) print(f"Time {t}: {ms} ({len(ms)})") print("---------------\n") def basicCheck(self): room_caps_sorted = sorted([room.room_cap for room in self.rooms], reverse=True) for t in self.timeslots: tr = self.timeslot_requests[t] if len(tr) > 0: # Check if any 'fixed room' requests have conflict room_booked_by = {} for meeting in tr: if meeting.room_name: if meeting.room_name in room_booked_by: existing_booking = room_booked_by[ meeting.room_name] msg = f"Timeslot {t} ({Util.timeslot_to_dt(t).strftime('%H:%M:%S')}): " + \ f"Room '{meeting.room_name}' is requested by both:\n" + \ f"'{existing_booking.name} ({existing_booking.mrbs_entry_id})'\n" + \ f"'{meeting.name} ({meeting.mrbs_entry_id})'" raise MeetingRequestError( msg, { "code": self.CHECK_FIXED_ROOM_CONFLICT, "timeslot": t, "room": meeting.room_name, "meeting1": existing_booking.name, "meeting1_id": existing_booking.mrbs_entry_id, "meeting2": meeting.name, "meeting2_id": meeting.mrbs_entry_id }) else: room_booked_by[meeting.room_name] = meeting if len(tr) > self.rooms.num_rooms: msg = 'Too many meetings (total: {}) are booked at time {}'.format( len(tr), t) raise MeetingRequestError(msg, { "code": self.CHECK_FAILED_NO_OF_MEETINGS, "timeslot": t }) sizes_sorted = sorted([(m, m.size) for m in tr], key=lambda x: x[1], reverse=True) for i in range(len(sizes_sorted)): if sizes_sorted[i][1] > room_caps_sorted[i]: msg = 'Meeting {m} cannot find a fitting room (of size {s}) at time {t}\n'.format( m=sizes_sorted[i][0].name, s=sizes_sorted[i][1], t=t) + \ 'Caps of all rooms: \n{}'.format(room_caps_sorted) + \ 'Sizes of meeting at time {t}: \n{s}'.format(t=t, s=str([x[1] for x in sizes_sorted])) raise MeetingRequestError(msg, { "code": self.CHECK_FAILED_MEETING_SIZE, "timeslot": t }) @staticmethod def checkRoomFit(room, meeting): # By avoiding assigning BIG rooms to SMALL meeting, we reduce the number of branches when calculating the soln. if room.room_cap < meeting.size: return False elif meeting.size <= 10: return room.room_cap <= 50 elif meeting.size <= 30: return room.room_cap <= 100 else: return True @staticmethod def bookingWaste(room, meeting): waste = room.room_cap - meeting.size if waste <= 5: # Help reduce number of branches return 0 return int((room.room_cap / meeting.size) * 10) @staticmethod def getBooking(bookings, model, meeting, time, room): id = getid(meeting, time, room) if id not in bookings: bookings[id] = model.NewBoolVar('{}'.format(id)) return bookings[id] def createBookingModel(self, solution=None, no_min_waste=False): print(f"createBookingModel: start - {datetime.now()}") model = cp_model.CpModel() bookings = {} # If we have a set of allocations we should follow, let's set condition for them first if solution: print("createBookingModel: we have a past solution to follow") for m in self.meetings: for r in self.rooms: for t in self.timeslots: if getid(m, t, r) in solution["alloc"]: # This exact [meeting, timeslot, room] combination has been set in the past solution model.Add( self.getBooking(bookings, model, m, t, r) == 1) # A meeting must happen at its specified time slots for m in self.meetings: for t in self.timeslots: if t in m.meeting_times: if m.room_name: # The meeting already has a room specified room_found = False for r in self.rooms: if m.room_name == r.name: model.Add( self.getBooking( bookings, model, m, t, self.rooms.get_room(m.room_name)) == 1) room_found = True break assert room_found, f'The room specified in {str(m)} cannot be found' else: # if meeting m needs timeslot t, we need to book exactly one room at timeslot t # for those rooms that fit the size model.Add( sum( self.getBooking(bookings, model, m, t, r) for r in self.rooms if self.checkRoomFit(r, m)) == 1) # A room cannot hold more than one meeting at the same time for t in self.timeslots: for r in self.rooms: bookings_temp = [] for m in self.meetings: if self.checkRoomFit(r, m) and t in m.meeting_times: bookings_temp.append( self.getBooking(bookings, model, m, t, r)) if len(bookings_temp) > 0: # Each room can be assigned only to one meeting model.Add(sum(bookings_temp) <= 1) # A meeting must use the same room in all its required timeslots. That is, a meeting cannot 轉房. for m in self.meetings: for r in self.rooms: if self.checkRoomFit(r, m): for i in range(len(m.meeting_times) - 1): # For room r, if the current timeslot is TRUE, then the next one must be true too model.Add( self.getBooking(bookings, model, m, m.meeting_times[i + 1], r) == True).OnlyEnforceIf( self.getBooking(bookings, model, m, m.meeting_times[i], r)) # Facility checking for m in self.meetings: if not m.room_name and m.facilities: # The meeting isn't assigned a room yet, and it needs facility for t in m.meeting_times: for r in self.rooms: room_has_all_needed_fac = all([ needed_fac in r.facilities for needed_fac in m.facilities ]) if not room_has_all_needed_fac: # if the room doesn't have all needed facility, don't allow it to hold the meeting model.Add( self.getBooking(bookings, model, m, t, r) == 0) # All the things we want to minimize to_minimize = [] # 細房 and the combined 大房 cannot be booked at the same time for combined_info in self.rooms.combined_rooms: small_rooms = combined_info['small_rooms'] large_room = combined_info['combined_room'] whole_day_bookings_for_small_rooms = [] whole_day_bookings_for_large_room = [] for t in self.timeslots: booking_large = self.getBooking(bookings, model, m, t, large_room) # Boost the cost factor, so that it has higher priority over "room waste" during minimize whole_day_bookings_for_large_room.append(50 * booking_large) for small_room in small_rooms: # All the bookings for this small room and its corresponding large room at time t bookings_temp = [] for m in self.meetings: if t in m.meeting_times: bookings_temp.append(booking_large) booking_small = self.getBooking( bookings, model, m, t, small_room) # Boost the cost factor, so that it has higher priority over "room waste" during minimize whole_day_bookings_for_small_rooms.append( 50 * booking_small) bookings_temp.append(booking_small) # At this timeslot, the 細房 and its corresponding 大房 can't be booked at the same time. model.Add(sum(bookings_temp) <= 1) if combined_info['normal'] == 'combined': # Throughout the whole day, 拆細房 should be avoided to_minimize.extend(whole_day_bookings_for_small_rooms) else: assert combined_info['normal'] == 'split' # Throughout the whole day, 變大房 should be avoided to_minimize.extend(whole_day_bookings_for_large_room) # Minimize room space waste to_minimize.extend( self.bookingWaste(r, m) * bookings[getid(m, t, r)] for m in self.meetings for t in self.timeslots for r in self.rooms if getid(m, t, r) in bookings and m.room_name is None and not m.fixed) # Now call model.Minimize to minimize everything model.Minimize(sum(to_minimize)) print(f"createBookingModel: end - {datetime.now()}") return model, bookings def save_one_solution(self): allocations = [] for m in self.meetings: for r in self.rooms: for t in self.timeslots: id = getid(m, t, r) if id in self._lastBookings: if self._solver.Value(self._lastBookings[id]): allocations.append(getid(m, t, r)) return {"alloc": allocations} def export_solution(self, solution, fn): df = pd.DataFrame(columns=["Time"]) for t, i in zip(self.timeslots, range(len(self.timeslots))): df = df.append({'Time': Util.timeslot_to_str(t)}, ignore_index=True) for m in self.meetings: for r in self.rooms: if getid(m, t, r) in solution["alloc"]: room = f'{r.name} ({r.room_cap_original}/{r.room_cap})' if r.facilities: room += f' ({", ".join(r.facilities)})' if room not in df: df[room] = "" waste = r.room_cap - m.size s = f'{m.name} ({m.size}) (-{waste})' if m.fixed or m.room_name: s += " *" # Fixed, or has room specified in the request already elif waste > 10: print( f"Big waste detected: {Util.timeslot_to_str(t)} - {s}" ) df.at[i, room] = s room_cols = set(df.columns) room_cols.remove('Time') room_cols = list(room_cols) room_cols.sort() cols = ["Time"] cols.extend(room_cols) df[cols].to_csv(fn, index=False, na_rep='', encoding='utf_8_sig') def export_new_bookings(self, solution, filename=None, write_to_db=False): df = pd.DataFrame(columns=[ "Date", "Name", "Time", "Require", "Size", "Room", "Cap", "Cap_Real", "Facilities" ]) new_meetings_info = [] for m in self.meetings: if m.fixed: continue for r in self.rooms: times = [] for t in self.timeslots: if getid(m, t, r) in solution["alloc"]: times.append(t) if times: assert times == m.meeting_times df = df.append( { "Date": self._dateLoaded.strftime("%Y-%m-%d"), "Name": m.name, "Size": m.size, "Time": Util.timeslot_to_str([times[0], times[-1] + 1]), "Require": "" if not m.facilities else ", ".join(m.facilities), "Room": r.name, "Facilities": "" if not r.facilities else ", ".join(r.facilities), "Cap_Real": r.room_cap_original, "Cap": r.room_cap }, ignore_index=True) new_meetings_info.append({"meeting": m, "room": r}) break if filename: df.to_csv(filename, index=False, na_rep='', encoding='utf_8_sig') if write_to_db: new_entry_ids = self.rmbs.insert_meetings(new_meetings_info, self._dateLoaded) else: new_entry_ids = [] return df, new_entry_ids @staticmethod def send_new_bookings_email(df_new_bookings: pd.DataFrame): df_new_bookings = df_new_bookings.fillna("").rename( { "Date": "日期", "Name": "名稱", "Time": "時間", "Require": "所需設備", "Size": "人數", "Room": "房間", "Cap": "容量(2/3)", "Cap_Real": "容量(全)", "Facilities": "設備" }, axis='columns') booking_table = df_new_bookings.to_html(index=False, justify='left', border=1) html = f"""\ <html> <head> <style> body {{font-size:10p}} table, th, td {{font-size:10pt; border:1px solid black; border-collapse:collapse; text-align:left;}} th, td {{padding: 5px;}} </style> </head> 房間預約自動分配結果 <br/><br/> <body> {booking_table} </body> </html> """ Util.send_email("房間預約分配結果 - 成功", html) def send_no_solution_email(self): df = pd.DataFrame(columns=["Date", "Name", "Time", "Require"]) for m in self.requests: df = df.append( { "Date": self._dateLoaded.strftime("%Y-%m-%d"), "Name": m.name, "Size": m.size, "Time": Util.timeslot_to_str( [m.meeting_times[0], m.meeting_times[-1] + 1]), "Require": "" if not m.facilities else ", ".join(m.facilities), "Size": m.size }, ignore_index=True) df = df.fillna("").rename( { "Date": "日期", "Name": "名稱", "Time": "時間", "Require": "所需設備", "Size": "人數" }, axis='columns') booking_table = df.to_html(index=False, justify='left', border=1) html = f"""\ <html> <head> <style> body {{font-size:10p}} table, th, td {{font-size:10pt; border:1px solid black; border-collapse:collapse; text-align:left;}} th, td {{padding: 5px;}} </style> </head> 房間預約自動分配結果: 未能成功分配 <br/><br/> <body> {booking_table} </body> </html> """ Util.send_email("房間預約分配結果 - 失敗", html) def print_one_solution(self, solution): booking_allocated = set() for t in self.timeslots: print(f'Time {t} ({Util.timeslot_to_str(t)})') for m in self.meetings: for r in self.rooms: if getid(m, t, r) in solution["alloc"]: booking_allocated.add(m.name) extra_rm = mtg_times_info = final_info = '' name = str(m.name) if len(m.meeting_times) > 1: if m.meeting_times[0] == t: mtg_times_info = '*' else: mtg_times_info = '.' if extra_rm: extra_rm = '(' + extra_rm + ')' print( f' Mtg-{name}{mtg_times_info} (size:{m.size}) assigned to {r.name}{extra_rm} ' f'(cap:{r.room_cap}) (waste={(r.room_cap - m.size)}) {final_info}' ) print() print("Meeting allocated total: {}".format(len(booking_allocated))) def resolve(self, past_solution=None, max_time=180, no_min_waste=False): model, bookings = self.createBookingModel(solution=past_solution, no_min_waste=no_min_waste) self._lastBookings = bookings self._solver.parameters.max_time_in_seconds = max_time status = self._solver.Solve(model) status = self._solver.StatusName(status) self._lastStatus = status if status != 'INFEASIBLE': solution = self.save_one_solution() else: solution = None return status, solution def printStats(self): print("Solve returns: " + self._lastStatus) print() print('Statistics') print(' - conflicts : %i' % self._solver.NumConflicts()) print(' - branches : %i' % self._solver.NumBranches()) print(' - wall time : %f s' % self._solver.WallTime()) print() @staticmethod def send_too_late_bookings_email(df_apps: pd.DataFrame): df_apps = df_apps.fillna("").rename( { "register_time": "交表時間", "description": "申請詳情", "event_site": "地點", "event_date": "聚會日期", "start_time": "開始時間", "stop_time": "結束時間", "size": "人數", "application_file": "申請檔案" }, axis='columns') table = df_apps.to_html(index=False, justify='left', border=1) html = f"""\ <html> <head> <style> body {{font-size:10p}} table, th, td {{font-size:10pt; border:1px solid black; border-collapse:collapse; text-align:left;}} th, td {{padding: 5px;}} </style> </head> 過來日子限期的申請 <br/><br/> <body> {table} </body> </html> """ Util.send_email("過來日子限期的申請", html)
}) def stop(self): """ Stop tcp data """ self.sock.close() if __name__ == "__main__": """ Start a game server """ parser = argparse.ArgumentParser(description='Simple game server') parser.add_argument('--tcpport', dest='tcp_port', help='Listening tcp port', default="1234") parser.add_argument('--udpport', dest='udp_port', help='Listening udp port', default="1234") parser.add_argument('--capacity', dest='room_capacity', help='Max players per room', default="2") args = parser.parse_args() rooms = Rooms(int(args.room_capacity)) main_loop(args.tcp_port, args.udp_port, rooms)
def startGame(): p_invent = Inventory() room_count = Rooms() Rooms.hallway(room_count, p_invent)