def run_simulation(forms, sigma_settings): """ Takes in the variables to be written from the client forms and the params from the settings file which determines the order of the parameters. Returns a parsed SigmaOutput instance of output file. """ f_name = int(time()) outfile_name = "%s.out" % f_name expfile_name = "%s.exp" % f_name randomseed = randint(0, 65534) exp = [outfile_name, "n", str(randomseed), forms['RUNTIME'], "1"] for param in sigma_settings['parameters']: exp.append(forms[param['name']]) expstr = " ".join(exp) expfile = open(expfile_name, 'w') expfile.write(expstr) expfile.flush() expfile.close() p = Popen(["%s\\sigma\\bin\\%s.exe" % (PROJECT_PATH, sigma_settings['model']), expfile_name], stdout=PIPE, stderr=STDOUT) p.wait() out_inst = parse(outfile_name) os.remove(expfile_name) os.remove(outfile_name) return out_inst
def test_1_parse_cmd(self): cmd = "CMD_{}".format(str(uuid4())) cmd_key, parsed_data = parse(cmd) self.assertIsNone( cmd_key, msg="{} is invalid command, command key must be null".format(cmd)) self.assertIsNone( parsed_data, msg="{} is invalid command, command info must be null".format(cmd)) cmd = "status" cmd_key, parsed_data = parse(cmd) self.assertIsNotNone( cmd_key, msg="{} is valid command, command key must not be null".format( cmd)) self.assertIsNotNone( parsed_data, msg="{} is valid command, command info must not be null".format( cmd))
def runSigma(sigma_settings, fileNames, inputs): p = Popen( ["%s\\sigma\\bin\\%s.exe" % (PROJECT_PATH, sigma_settings["model"]), fileNames["expfile_name"]], stdout=PIPE, stderr=STDOUT, ) p.wait() out_inst = parse(fileNames["outfile_name"], time(), inputs) os.remove(fileNames["expfile_name"]) os.remove(fileNames["outfile_name"]) return out_inst
def run_simulation(forms, sigma_settings, array): """ Takes in the variables to be written from the client forms and the params from the settings file which determines the order of the parameters. Returns a parsed SigmaOutput instance of output file. """ f_name = int(time()) outfile_name = "%s.out" % f_name expfile_name = "%s.exp" % f_name randomseed = randint(0, 65534) exp = [outfile_name, "n", str(randomseed), forms['RUNTIME'], "1"] for j in range(0, len(array) - 1): exp.append(array[j]) #for param in sigma_settings['parameters']: # exp.append(forms[param['name']]) #If I want to split it up going to have to make a bunch of these. expstr = " ".join(exp) # This is where I should create the data file? expfile = open(expfile_name, 'w') #This is where it appends to a file that it will give to the c when it asks for it. expfile.write(expstr) expfile.flush() expfile.close() p = Popen([ "%s\\sigma\\bin\\%s.exe" % (PROJECT_PATH, sigma_settings['model']), expfile_name ], stdout=PIPE, stderr=STDOUT) #Opens the sigma MOdel specified in the settings. and the file to give to the c. p.wait() out_inst = parse(outfile_name) os.remove(expfile_name) os.remove(outfile_name) return out_inst
def run_simulation(forms, sigma_settings): """ Takes in the variables to be written from the client forms and the params from the settings file which determines the order of the parameters. Returns a parsed SigmaOutput instance of output file. """ f_name = int(time()) outfile_name = "%s.out" % f_name expfile_name = "%s.exp" % f_name randomseed = randint(0, 65534) exp = [outfile_name, "n", str(randomseed), forms['RUNTIME'], "1"] for param in sigma_settings['parameters']: exp.append(forms[param['name']]) expstr = " ".join(exp) expfile = open(expfile_name, 'w') expfile.write(expstr) expfile.flush() expfile.close() p = Popen([ "%s\\sigma\\bin\\%s.exe" % (PROJECT_PATH, sigma_settings['model']), expfile_name ], stdout=PIPE, stderr=STDOUT) p.wait() out_inst = parse(outfile_name) os.remove(expfile_name) os.remove(outfile_name) return out_inst
def run_simulation(forms, sigma_settings, array): """ Takes in the variables to be written from the client forms and the params from the settings file which determines the order of the parameters. Returns a parsed SigmaOutput instance of output file. """ f_name = int(time()) outfile_name = "%s.out" % f_name expfile_name = "%s.exp" % f_name randomseed = randint(0, 65534) exp = [outfile_name, "n", str(randomseed), forms['RUNTIME'], "1"] for j in range(0, len(array) - 1): exp.append(array[j]) #for param in sigma_settings['parameters']: # exp.append(forms[param['name']]) #If I want to split it up going to have to make a bunch of these. expstr = " ".join(exp) # This is where I should create the data file? expfile = open(expfile_name, 'w') #This is where it appends to a file that it will give to the c when it asks for it. expfile.write(expstr) expfile.flush() expfile.close() p = Popen(["%s\\sigma\\bin\\%s.exe" % (PROJECT_PATH, sigma_settings['model']), expfile_name], stdout=PIPE, stderr=STDOUT) #Opens the sigma MOdel specified in the settings. and the file to give to the c. p.wait() out_inst = parse(outfile_name) os.remove(expfile_name) os.remove(outfile_name) return out_inst
import sys from util.printer import print_board, print_attacked_pieces from util.parser import parse from util.interface import show_file_prompt, show_landing_screen, show_option_prompt, choose_algorithm from algorithm import annealing, genetic, hill_climbing if (__name__ == '__main__'): show_landing_screen() # Get external file filename = show_file_prompt() try: chess_pieces = parse(filename) # Show options option = show_option_prompt() result = choose_algorithm(option, chess_pieces) print_board(result) print_attacked_pieces(result) except IOError as error: sys.exit("Error : " + str(error)) # Parse external file # hill_climbing(chess_pieces, generate_move, 25)
def run_simulation(forms, sigma_settings): """ Takes in the variables to be written from the client forms and the params from the settings file which determines the order of the parameters. Returns a parsed SigmaOutput instance of output file. """ f_name = int(time()) outfile_name = "%s.out" % f_name expfile_name = "%s.exp" % f_name xlsfile_name = "%s.xls" % f_name randomseed = randint(0, 65534) exp = [outfile_name, "n", str(randomseed), forms['RUNTIME'], "1"] for param in sigma_settings['parameters']: exp.append(forms[param['name']]) expstr = " ".join(exp) expfile = open(expfile_name, 'w') expfile.write(expstr) expfile.flush() expfile.close() print "hello exe" p = Popen(["%s\\sigma\\bin\\%s.exe" % (PROJECT_PATH, sigma_settings['model']), expfile_name], stdout=PIPE, stderr=STDOUT) print "did it" #p.wait() sleep(3) print "finished" #delete rows in out file fin=open(outfile_name,"r") tmp_list=fin.readlines() fin.close() #print tmp_list # delete rows from tmp list del tmp_list[1:140] # Vertical length of file doc_length= len(tmp_list)-12 last_line= tmp_list[-142] # write back in fout=open(outfile_name,"w") fout.writelines(tmp_list) fout.close() fcsv=open(xlsfile_name,"w") fcsv.writelines(tmp_list) fcsv.close out_inst = parse(outfile_name) #os.remove(expfile_name) #os.remove(outfile_name) return out_inst, xlsfile_name, last_line
def dispatch(self, cmd): """ Executes the command. :param cmd: command to be executed. """ response = Response() cmd_key, parsed_cmd = parse(cmd) if cmd_key and parsed_cmd: if cmd_key != Command.CREATE and self.parking_service.parking_lot is None: logger.error("No parking lot found, please create a parking lot first.") response = Response(success=False, error=ErrorConstant.NO_PARKING_LOT_FOUND, message="Parking lot must be created first") return response if cmd_key == Command.CREATE: size = int(parsed_cmd.get('size', 0)) self.parking_service.create_parking_lot(size) logger.info("Created a parking lot with {} slots".format(size)) response = Response(data={'size': size}, message="Parking lot created successfully.") elif cmd_key == Command.PARK: reg_num = parsed_cmd.get('reg_num') color = parsed_cmd.get('color') # Create vehicle. vehicle = Vehicle.create(Car, reg_num, color) # Park the vehicle. response = self.parking_service.park(vehicle) if response.success: logger.info("Allocated slot number: {}".format(response.data['slot_num'])) else: if response.error == ErrorConstant.PARKING_LOT_OVERFLOW: logger.info("Sorry, parking lot is full") elif cmd_key == Command.LEAVE: slot_num = int(parsed_cmd.get('slot_num')) # Vehicle is leaving the slot. response = self.parking_service.leave(SmallSlot(int(slot_num))) if response.success: logger.info("Slot number {} is free".format(slot_num)) elif cmd_key == Command.STATUS: logger.info("Slot No.\tRegistration No.\tColour") for slot_num, reg_num, col in self.parking_service.status(): logger.info('{}\t\t\t{}\t\t{}'.format(slot_num, reg_num, col)) elif cmd_key == Command.REG_NUMS_BY_COLOR: color = parsed_cmd.get('color') # Will be better to print reg_num one-by-one, as it will save the space. reg_nums = [reg_num for reg_num in self.parking_service.registration_numbers_by_color(color)] logger.info(', '.join(reg_nums)) response = Response(data={'reg_nums': reg_nums}) elif cmd_key == Command.SLOT_NUMS_BY_COLOR: color = parsed_cmd.get('color') # Will be better to print slot_num one-by-one, as it will save the space. slot_nums = [str(slot_num) for slot_num in self.parking_service.slot_numbers_by_color(color)] logger.info(', '.join(slot_nums)) response = Response(data={'slot_nums': slot_nums}) elif cmd_key == Command.SLOT_NUM_BY_REG_NUM: reg_num = parsed_cmd.get('reg_num') slot_num = self.parking_service.slot_number_by_registration_number(reg_num) logger.info(str(slot_num or 'Not found')) response = Response(success=slot_num is not None, data={'slot_num': slot_num}) else: logger.error("Invalid command: {}".format(cmd)) response = Response(success=False, error=ErrorConstant.INVALID_COMMAND) else: logger.error("Invalid command: {}".format(cmd)) response = Response(success=False, error=ErrorConstant.INVALID_COMMAND) return response