def _start(self): # Some common tasks that need to be done when an operation starts Log.log("Executing %s to %s" % (self.CommandType, self.DestIP)) self.DestName = IP.reverseDNS(self.DestIP) self.DestAddress = IP.ip2int(self.DestIP) # Current time self.StartTime = str(datetime.utcnow()).strip("0") self.LocalStartTime = str(datetime.now()).strip("0")
def play(self, show_steps=True): current_player = State.BLACK start = clock() while self.chessboard.state.get_end_state() is None: move = self.players[current_player].get_next_step( self.chessboard.state) self.chessboard.place_chess(move[0], move[1], current_player) if show_steps: self.chessboard.update() current_player = State.BLACK if current_player == State.WHITE else State.WHITE winner = self.chessboard.state.get_end_state() end = clock() Log.log('winner: {}, used {:.3f}s'.format(winner, (end - start))) return winner
def __init__(self, scriptPath): # Load the script into memory self.scriptPath = scriptPath self.xml = etree.parse(self.scriptPath) self.id = self.xml.find("Script").get("id") self.exid = self.xml.find("Script").get("ExID") # Parse the script operations operations = self.xml.find("Script").text.splitlines() for operation in operations: if len(operation) == 0: continue operation = operation.split() if operation[0] == "PING": op = PingOperation.PingOperation(self, operation[1]) elif operation[0] == "TRACEROUTE": op = TracerouteOperation.TracerouteOperation(self, operation[1]) else: raise Exceptions.ScriptParseException("Unknown operation: %s in script %s, PLEASE REPORT THIS!" % (operation[0], self.scriptPath)) self.operationQ.put(op) Log.log("Loaded script %s, %d operations to run" % (self.scriptPath, self.operationQ.qsize()), 1)
def self_play(self, show_steps=True): states = [] mcts_probs = [] current_players = [] current_player = State.BLACK start = clock() while self.chessboard.state.get_end_state() is None: # temperature parameter set to 1.0 when self-play move, action_probs = self.players[ current_player].get_next_step_and_action_prob( self.chessboard.state, add_noise=True, temperature_param=1.0) # action_probs = action_probs.reshape([action_probs.size]) # Log.log('move: {}'.format(move), end='\n', print_to_stdout=True) Log.log('move: {}'.format(move), end=' ', print_to_stdout=False) states.append(self.chessboard.state.get_nn_input()) mcts_probs.append(action_probs) current_player = self.chessboard.state.get_next_player() current_players.append(current_player) self.chessboard.place_chess(move[0], move[1], current_player) if show_steps: self.chessboard.update() current_player = State.BLACK if current_player == State.WHITE else State.WHITE winner = self.chessboard.state.get_end_state() end = clock() Log.log('winner: {}, used {:.3f}s'.format(winner, (end - start))) winner_label = np.zeros(len(current_players)) if winner != State.END_STATE_DRAW: winner_label[np.array(current_players) == winner] = 1.0 winner_label[np.array(current_players) != winner] = -1.0 for p in self.players.values(): p.mcts.move(-1) return winner, zip(states, mcts_probs, winner_label)
def evaluate(eval_plays=10, max_simulate_count=500): global player Log.log('max_sim: {}'.format(max_simulate_count)) win_count = 0 for _ in range(eval_plays): chessboard = ChessBoard() player1 = player # 1: +nn player2 = PlayerMCTS(role=State.WHITE, max_simulate_count=max_simulate_count) game = Game(chessboard=chessboard, player_offensive=player1, player_defensive=player2) winner = game.play(show_steps=False) Log.log('state: {}'.format(game.chessboard.state), print_to_stdout=False) if winner == State.END_STATE_BLACK: win_count += 1 win_ratio = float(win_count) / eval_plays Log.log('win_ratio: {}'.format(win_ratio)) return float(win_count) / eval_plays # 胜率
def __init__(self, script): threading.Thread.__init__(self) self.script = script Log.log("Thread is %s, script is %s" % (self.name, self.script.id))
def train(global_step): kl_targ = 0.025 def _kl(old_probs, new_probs): kl = np.mean( np.sum(old_probs * (np.log(old_probs + 1e-10) - np.log(new_probs + 1e-10)), axis=1)) return kl global train_batch_size global self_play_data global player global max_sim global best_win_ratio global train_step global lr_multiplier self_play() if len(self_play_data) >= train_batch_size: train_data = random.sample(self_play_data, k=train_batch_size) state_batch = [d[0] for d in train_data] action_prob_batch = [d[1].reshape([d[1].size]) for d in train_data] winner_batch = np.array([d[2] for d in train_data]) winner_batch = winner_batch.reshape((winner_batch.size, 1)) nn = player.mcts.net old_probs, old_v = nn.session.run( [nn.action_prob, nn.value], feed_dict={nn.state_input: state_batch}) for _ in range(5): __, loss, lr = nn.session.run( [nn.train_op, nn.loss, nn.learning_rate], feed_dict={ nn.state_input: state_batch, nn.action_prob_input: action_prob_batch, nn.value_input: winner_batch, nn.train_step: train_step + 1, # nn.learning_rate: 1e-5 nn.learning_rate: lr_multiplier * 0.005 }) train_step += 1 new_probs, new_v = nn.session.run( [nn.action_prob, nn.value], feed_dict={nn.state_input: state_batch}) kl = _kl(old_probs, new_probs) print('lr_mul={:.5f} lr={:.5f} kl={:.5f} loss={:.5f}'.format( lr_multiplier, float(lr), kl, loss)) Log.log(loss, end=', ', print_to_stdout=False) if kl > kl_targ * 4: break Log.log('lr={:.7f}'.format(float(lr)), print_to_stdout=False) # adjust learning rate if kl > kl_targ * 2 and lr_multiplier > 0.1: lr_multiplier /= 1.5 elif kl < kl_targ / 2 and lr_multiplier < 10: lr_multiplier *= 1.5 Log.log('') if global_step % evaluate_interval == 0: win_ratio = evaluate(max_simulate_count=max_sim) print('best win_ratio: {}'.format(best_win_ratio)) if win_ratio > best_win_ratio: best_win_ratio = win_ratio Log.log('new network') player.mcts.net.save_network(global_step) if win_ratio >= 0.9 and max_sim < 4000: max_sim += 100 best_win_ratio = 0.0 Log.flush() else: Log.log(len(self_play_data))
from dimes import Script from util import Log from util import Config import threading, time script = Script.Script("default_script.xml") script.execute() try: while threading.active_count() > 1: time.sleep(0.1) #Log.log("%d pending, %d complete" % (script.operationQ.qsize(), script.resultQ.qsize())) Log.log("All done, exiting!", 2) except KeyboardInterrupt: Log.log("Received Ctrl+C, ending %d threads... (This may take a moment)" % threading.active_count(), 2)
def load_network(self, model_name): self.saver.restore(self.session, model_name) Log.log('restored:', model_name)
def save_network(self, global_step): save_path = os.path.join('models', 'policy_net') save_path = self.saver.save(self.session, save_path, global_step=global_step) Log.log('saved in:', save_path)
def ValidateArgs(args): # Firstly, make sure that all of the relevant input files exist and are # accessible. time_str = datetime.now().strftime("%Y-%m-%d_%H:%M:%S") if args.gpu_affinity < 0: print("Negative gpu affinity specified.") return 1 if args.thread_count < 0: print("Negative thread count specified.") return 1 if args.log_path != "": try: with open(args.log_path, 'w') as file: file.write('test') os.remove(args.log_path) except: print("The log file does not appear to be writable.") return 1 else: args.log_path = 'pyfit_log.%s.txt'%time_str if args.nvidia_smi_log != '': try: with open(args.nvidia_smi_log, 'w') as file: file.write('test') os.remove(args.nvidia_smi_log) except: print("The nvidia-smi log file does not appear to be writable.") return 1 if args.unique: # We need to modify all of the output file names to contain the date # & time string. def number(name, num): parts = name.split('.') result = parts[0] result += '.%05i.%s'%(num, '.'.join(parts[1:])) return result def unique(name): if name != "" and not name.isspace(): idx = 0 path_to = '/'.join(name.split('/')[:-1]) if path_to == '': path_to = None # This loop looks bad but I sincerely doubt that anyone will # every let 2**32 runs go by without cleaning the directory. while idx < 2**32: all_files = os.listdir(path_to) test_name = number(name, idx) if test_name not in all_files: return test_name idx += 1 else: return name args.training_set_output_file = unique(args.training_set_output_file) args.neural_network_out = unique(args.neural_network_out) args.network_backup_dir = unique(args.network_backup_dir) args.loss_log_path = unique(args.loss_log_path) args.validation_log_path = unique(args.validation_log_path) args.energy_volume_file = unique(args.energy_volume_file) args.log_path = unique(args.log_path) args.nvidia_smi_log = unique(args.nvidia_smi_log) if not os.path.isfile(args.training_set_in) and args.run_training: # It's ok if the training set doesn't exist, as long as # we are about to generate it. args.training_set_in = args.training_set_output_file log = Log(args.log_path) # ============================== # Logging # ============================== log.log("run starting %s"%(time_str)) # Log the configuration file for this run. del args.as_dictionary['as_dictionary'] arg_dict = args.as_dictionary max_len = max([len(k) for k in arg_dict]) log.log('='*10 + ' Run Configuration ' + 10*'=') log.indent() for k in arg_dict: k_str = k + (max_len - len(k))*' ' log.log('%s = %s'%(k_str, arg_dict[k])) log.unindent() log.log('='*39) # ============================== # End Logging # ============================== if not args.run_training and not args.generate_training_set: print("No task was specified. Use -g and/or -t.\n") PrintHelp(None) return 1 if args.neural_network_in == "": msg = "No input neural net was specified. Please specify one via " msg += "either the configuration value \'neural_network_in\' or one " msg += "of the following arguments:\n" msg += "\t--network-input-file=<nn file>\n" msg += "\t-e=<nn file>\n\n" print(msg) PrintHelp(None) return 1 if not os.path.isfile(args.neural_network_in): print("The specified input neural network does not exist.") return 1 try: with open(args.neural_network_in, 'r') as file: file.read(1) except: print("Could not read the specified neural net input file.") return 1 if args.generate_training_set: # If the system is generating a training set and neither option is # specified, this doesn't make sense. if args.dft_input_file == "": msg = "You have configured the program to generate a training set" msg += "but not specified a value for dft_input_file. " msg += "Please specify it in the config file or use " msg += "the following option: \n" msg += "\t--dft-file=<some file>\n\n" print(msg) PrintHelp(None) return 1 if args.training_set_output_file == "": msg = "You have not specified a value for " msg += "training_set_output_file. Please do this in the " msg += "configuration file or with one of the following options:" msg += "\n\t--training-set-out=<some file to write>\n" msg += "\t-a=<some file to write>\n\n" print(msg) PrintHelp(None) return 1 if os.path.isfile(args.training_set_output_file) and not args.overwrite: msg = "The specified training set output file already exists. " msg += "pyfit does not overwrite large files. Please change the " msg += "name or delete it in order to continue." print(msg) return 1 # Make sure that nothing is going to stop the system from writing the # output file at the end. try: with open(args.training_set_output_file, 'w') as file: file.write('test') os.remove(args.training_set_output_file) except: msg = "The training set output file does not appear to be " msg += "writable." print() return 1 if args.dft_input_file != "": # Make sure it exists and that we can read it. if not os.path.isfile(args.dft_input_file): print("The specified dft input file does not exist.") return 1 try: with open(args.dft_input_file, 'r') as file: file.read(1) except: print("Could not read the specified dft input file.") return 1 if args.run_training: if args.training_iterations < 0: print("Negative number of training iterations specified.") return 1 if args.training_set_in == "": msg = "No input training set was specified. Please specify one " msg += "via either the configuration value \'training_set_in\' or " msg += "one of the following arguments:\n" msg += "\t--training-set-in=<training set file>\n" msg += "\t-s=<training set file>\n\n" print(msg) PrintHelp(None) return 1 if args.neural_network_out == "": msg = "No output neural net was specified. Please specify one " msg += "via either the configuration value \'neural_network_out\' " msg += "or one of the following arguments:\n" msg += "\t--network-output-file=<nn file>\n" msg += "\t-y=<nn file>\n\n" print(msg) PrintHelp(None) return 1 # Make sure the input files are there and readable. Also make sure that # the output file doesn't already exist. if not os.path.isfile(args.training_set_in): # It's ok if the training set doesn't exist, as long as # we are about to generate it. ok = args.generate_training_set ok &= args.training_set_output_file == args.training_set_in if not ok: print("The specified training set file does not exist.") return 1 try: if os.path.isfile(args.training_set_in): with open(args.training_set_in, 'r') as file: file.read(1) except: print("Could not read the specified training set input file.") return 1 if os.path.isfile(args.neural_network_out) and not args.overwrite: print("The specified neural network output file already exists.") return 1 # Make sure that nothing is going to stop the system from writing the # output file at the end. try: with open(args.neural_network_out, 'w') as file: file.write('test') os.remove(args.neural_network_out) except: print("The neural net output file does not appear to be writable.") return 1 if args.network_backup_interval > 0: if args.network_backup_dir == "": msg = "No network backup directory was specified, but the " msg += "backup interval was set. Please either set the " msg += "backup interval to 0 or specify a value for " msg += "network_backup_dir in the config file." print(msg) PrintHelp(None) return 1 if args.network_backup_dir[-1] != '/': args.network_backup_dir += '/' # If the network backup directory already exists, make sure it is # empty. If it doesn't exist, create it. Also make sure that we # can create files in it and write to them. if os.path.isdir(args.network_backup_dir): try: contents = os.listdir(args.network_backup_dir) except: msg = "The specified network backup directory is not " msg += "accessible. Please check the permissions for it." print(msg) return 1 if len(contents) != 0 and not args.overwrite: print("The specified backup directory is not empty.") return 1 else: try: os.mkdir(args.network_backup_dir) except: print("Could not create the network backup directory.") return 1 try: # Make sure that we can write to the backup directory. with open(args.network_backup_dir + 'test', 'w') as file: file.write('test') os.remove(args.network_backup_dir + 'test') except: msg = "The network backup directory does not appear " msg += "to be writable." print(msg) return 1 elif args.network_backup_interval < 0: args.network_backup_interval = 0 if args.validation_interval < 0: print("Negative validation interval specified.") return 1 if args.loss_log_path == "": msg = "No path was specified for logging the loss of the neural " msg += "network. Please specify a value for \'loss_log_path\' in " msg += "the config file." print(msg) PrintHelp(None) return 1 if args.validation_log_path == "": msg = "No path was specified for logging the validation loss of " msg += "the neural network. Please specify a value for " msg += "\'loss_log_path\' in the config file." print(msg) PrintHelp(None) return 1 if args.energy_volume_file != "": try: with open(args.energy_volume_file, 'w') as file: file.write('test') os.remove(args.energy_volume_file) except: msg = "An energy vs. volume output file was specified, but " msg += "is not writable." print(msg) return 1 if args.energy_volume_interval < 0: msg = "Negative value specified for energy vs. volume record " msg += "interval." print(msg) return 1 if args.energy_volume_interval > 0: if args.energy_volume_file == "": msg = "The energy vs. volume record interval is set, but no " msg += "file is specified to record it in. Please specify a " msg += "value for \'energy_volume_file\' in the config file." print(msg) return 1 # Make sure that all of the output files for the training process are # writable. try: with open(args.loss_log_path, 'w') as file: file.write('test') os.remove(args.loss_log_path) except: print("The loss log file does not appear to be writable.") return 1 try: with open(args.validation_log_path, 'w') as file: file.write('test') os.remove(args.validation_log_path) except: print("The validation log file does not appear to be writable.") return 1 if args.validation_ratio < 0.0 or args.validation_ratio > 1.0: print("Validation ratio must be in [0.0, 1.0]") return 1 if args.learning_rate < 0.0 or args.learning_rate > 10: print("The learning rate value is illogical, please correct it.") return 1 if args.learning_rate < 0.0 or args.learning_rate > 10: print("The learning rate value is illogical, please correct it.") return 1 if args.error_restart_level < 0.0: print("The error_restart_level value is illogical.") return 1 if args.validation_ratio == 1.0 and args.validation_interval != 0: msg = "No validation data used, but validation interval specified." print(msg) return 1 if args.l2_regularization_prefactor < 0.0: print("The l2_regularization_prefactor value is illogical.") return 1 return 0, log
class ircController(serverDispatcher, viewHandler): def __init__(self, servername): viewHandler.__init__(self) serverDispatcher.__init__(self) self.model = model.ircModel() # local, view related state self.channels = casedict() self.queries = casedict() # # Add ourself to the model as a server self.serverState = model.server() # save serverid in case we ever need to refer to it self.serverid = self.model.addServer(self.serverState) self.view = ircView.getIrcView() self.viewtext = self.view.new_view(servername) self.viewtext.setname(servername) self.cfg = getCfg() self.logger = Log(servername) self.notify = self.cfg('servers.notify', []) for i in self.notify: self.viewtext.notify_offline(i) self.notify_delayer = delay.Delayed() self.notify_delayer.ticktime = 10 # 1 tick = 10 sec self.notify_delayer.loop(self.send_notify, 2) main.addDelayed(self.notify_delayer) def setcallback(self, cb): self.handler = cb def set_handler(self, handler): self.new_handler = handler def run(self, nick, name, host, port=6667): self.viewtext.announce("Welcome to Most %s" % version) self.viewtext.set_handler(self) self.connect(host, port) self.serverState.state = model.server.STATE_CONNECTING self.serverState.name = name self.serverState.nick = nick def quit(self, msg = None): """ invoked to terminate this connection / the entire client (?) """ if self.serverState.state != model.server.STATE_NOTCONNECTED: self.irc.quit(msg or default_quit) self.close() def connect(self, host, port=6667): """ connect to a new server """ # what about server.STATE_CONNECTING ? if self.serverState.state in (model.server.STATE_CONNECTED, model.server.STATE_CONNECTING): self.viewtext.announce("Changing to server %s %d" % (host, port)) self.quit("Changing servers") self.serverState.reset() self.viewtext.announce("Connecting to server %s %d" % (host, port)) serverDispatcher.connect(self, host, port) self.serverState.state = model.server.STATE_CONNECTING # connect to new server def disconnect(self, msg="User disconnect"): """ simple disconnect, no reconnect, no quit """ if self.serverState.state in (model.server.STATE_CONNECTED, model.server.STATE_CONNECTING): self.viewtext.announce("Disconnecting...") self.quit(msg) self.stop() self.serverState.reset() self.reset() def reset(self): """ adjust widgets, etc after connection is closed """ self.serverState.reset() # resets notify, etc. self.viewtext.reset() def send_notify(self): """ send notifies. This method runs in a Delayed """ str = "" if self.serverState.state == model.server.STATE_CONNECTED: for i in self.notify: if len(str) + len(i) + 1 + 4 <= 512: str = "%s %s" % (str, i) else: self.irc.ison(str) str = "" if str != "": self.irc.ison(str) # utility methods def isme(self, nick): return lower(self.nick()) == lower(nick) def nick(self): """ a shortcut """ return self.serverState.nick ## ## Methods that implement view handlers def view_winclose(self, source, target): if target == None: self.viewtext.close_msgwin() elif self.channels.has_key(target): # if we're active on the channel, part it which will indirectly # cause the window to close. c = self.serverState.getChannel(target) if c and (c.getstate() == model.channel.STATE_ACTIVE): self.irc.part(target) else: # we're not active - remove the window self.channels[target].destroy() del self.channels[target] self.viewtext.delete_channel(target) elif self.queries.has_key(target): # queries can be destroyed self.queries[target].destroy() del self.queries[target] self.viewtext.delete_query(target) else: debug(ERROR, "Received WIN_CLOSE event for unknown" \ "window: %s" % target) ## ## The parsing done here should be done elsewhere... def view_new(self, source, text): # the parsing that's done here should be moved elsewhere if text == None: self.viewtext.announce("Please specify a hostname") return text = strip(text) if text == "": # use source appropriately? self.viewtext.announce("Please specify a hostname") idx = find(text, ' ') if idx > -1: host = strip(text[:idx]) try: port = int(text[idx+1:]) except ValueError: # use source appropriately? self.viewtext.announce("Invalid port specified") return else: host = text port = 6667 self.new_handler.meta_new(source, (host, port)) def view_newquery(self, source, nick): if not self.queries.has_key(nick): self.queries[nick]= self.viewtext.create_query(nick) def view_whois(self, source, nick): ## XXX todo: schedule reply handling, send (optionally) back to ## source window self.irc.whois(nick) def view_kick(self, nick, channel, reason): self.irc.kick(nick, channel, reason) def view_nick(self, source, nick): self.irc.nick(nick) def view_topic(self, source, topic): self.irc.topic(source, topic) def view_mode(self, target, change, args): self.irc.mode(target, change, args) def view_sendmsg(self, source, msg): """ handle MSG events, i.e. the sending of a message to a target, and the proper displaying of this action in the UI """ ## ## If target starts with = - use dcc? for line in filter(None, split(msg, "\n")): # \r\n for windows? # consult state.channels in stead of own dictionary? if self.channels.has_key(source): self.irc.privmsg(source, line) self.channels[source].msg(self.nick(), line, isme=1) elif self.queries.has_key(source): q = self.queries[source] use_dcc = q.use_dcc() if use_dcc: print "USE DCC", line self.dcc_chat_send(source, line) else: self.irc.privmsg(source, line) self.queries[source].msg(self.nick(), line, isme=1, dcc=use_dcc) elif self.cfg('client.use_msg_win', 1): m = self.viewtext.get_msgwin() m.sendmsg(source, line) # how about dcc? self.irc.privmsg(source, line) else: self.queries[source] = self.viewtext.create_query(source) self.queries[source].msg(self.nick(), line, isme=1) self.irc.privmsg(source, line) # log the message. Log differently for dcc? self.logger.log(source, "<%s> %s" % (self.nick(), line)) def view_msg(self, source, msg): if ' ' in msg: target, msg = split(msg, ' ', 1) else: target, msg = msg, "" self.view_sendmsg(target, msg) def view_join(self, source, rest): if ' ' in rest: channel, rest = split(rest, " ", 1) else: channel, rest = rest, None self.irc.join(channel, rest) def view_leave(self, source, rest): if ' ' in rest: channel, rest = split(rest, " ", 1) else: channel, rest = rest, None self.irc.part(channel, rest) def view_action(self, source, rest): # XXX # action is implemented by class ctcp, inherited from serverDispatcher self.action(source, rest) if self.channels.has_key(source): self.channels[source].action(self.nick(), rest, isme=1) elif self.queries.has_key(source): self.queries[source].action(self.nick(), rest, isme=1) else: m = self.viewtext.get_msgwin() m.sendaction(self.nick(), source, rest) self.logger.log(source, "-> %s %s" % (self.nick(), rest)) def view_whois(self, source, rest): self.irc.whois(rest) def view_whowas(self, source, rest): self.irc.whowas(rest) def view_away(self, source, rest): self.irc.away(rest) def view_who(self, source, rest): self.irc.who(rest) def view_invite(self, source, rest): self.irc.invite(rest, source) def view_raw(self, source, rest): self.irc.raw(rest) def view_connect(self, host, port): ## XXX won't work anymore (?!) self.connect(host, port)