def switchEvent(self): message = 'switch policy' self.log.info(message) Signal.get_signal().emit_signal_str(message) self.window = FramelessWindow('switch policy') self.dialog = QDialog() # tab item name list_str = [ strings.ALGORITHM_COMA, strings.ALGORITHM_COMMNET_COMA, strings.ALGORITHM_QMIX, strings.ALGORITHM_QTRAN_ALT, strings.ALGORITHM_QTRAN_BASE, strings.ALGORITHM_VDN ] # item content list_item = [ strings.CLASS_ALGORITHM_COMA, strings.CLASS_ALGORITHM_COMMNET_COMA, strings.CLASS_ALGORITHM_QMIX, strings.CLASS_ALGORITHM_QTRAN_ALT, strings.CLASS_ALGORITHM_QTRAN_BASE, strings.CLASS_ALGORITHM_VDN ] # title name self.listDialog = ListDialog(list_str, list_item, strings.OPERATIONAL_PLANNING_TITLE, strings.TYPE_POLICY) self.listDialog.setupUi(self.dialog, self.window) self.window.setWidget(self.dialog) self.initFrameLessWindow( QSize(700, 600), 'Operational Planning', QIcon('../resource/drawable/logo.png') ) self.window.show()
def mapDesignEvent(): message = 'open the map designer to customize the map' log = logging.getLogger('StarCraftII') log.info(message) Signal.get_signal().emit_signal_str(message) # os.system(strings.SCMDRAFT) subprocess.Popen(strings.SCMDRAFT)
def stopEvent(self): message = 'stop the simulation' self.log.info(message) Signal.get_signal().emit_signal_str(message) self.qtime.stop() OperationalPlanning.start_or_pause = False OperationalPlanning.stop = True globalInformation.set_value(strings.IS_STOP, True)
def map_choose(self, row): message = 'choose {}: {}'.format(self.name, self.list_str[row]) log = logging.getLogger('StarCraftII') log.info(message) Signal.get_signal().emit_signal_str(message) if self.name == strings.TYPE_POLICY: globalInformation.set_value(strings.TYPE_POLICY, self.list_str[row]) elif self.name == strings.TYPE_MAP: globalInformation.set_value(strings.TYPE_MAP, self.list_str[row])
def run(self): self.log = logging.getLogger('StarCraftII') message = "start rl algorithm" self.log.info(message) Signal.get_signal().emit_signal_str(message) env = StarCraft2Env( map_name="3m", window_size_x=1418, window_size_y=890, window_loc=(5, 155), ) env_info = env.get_env_info() n_actions = env_info["n_actions"] n_agents = env_info["n_agents"] message = "n_actions : {}".format(n_actions) self.log.info(message) Signal.get_signal().emit_signal_str(message) message = "n_agents : {}".format(n_agents) self.log.info(message) Signal.get_signal().emit_signal_str(message) n_episodes = 100 for e in range(n_episodes): env.reset() terminated = False episode_reward = 0 if self.stop: break while not terminated: obs = env.get_obs() state = env.get_state() if globalInformation.get_value(strings.IS_STOP): self.stop = True break actions = [] for agent_id in range(n_agents): avail_actions = env.get_avail_agent_actions(agent_id) avail_actions_ind = np.nonzero(avail_actions)[0] action = np.random.choice(avail_actions_ind) actions.append(action) reward, terminated, _ = env.step(actions) episode_reward += reward message = "Total reward in episode {} = {}".format( e, episode_reward) self.log.info(message) Signal.get_signal().emit_signal_str(message) env.close() Signal.get_signal().emit_signal_gameover()
def show_memory(self): message = "agent {}".format(self.index) self.log.info(message) Signal.get_signal().emit_signal_str(message) for _, _, t in self.trajectories.queue: message = "len: {}; score: ".format(len(t['observation']), t['score']) self.log.info(message) Signal.get_signal().emit_signal_str(message)
def update_policy(self): message = '\tagents update his target' self.log.info(message) Signal.get_signal().emit_signal_str(message) if self.policy == self.random_policy: message = "change policy to greedy" self.log.info(message) Signal.get_signal().emit_signal_str(message) self.policy = self.greedy_policy return
def pauseEvent(self): message = 'pause the simulation' self.log.info(message) Signal.get_signal().emit_signal_str(message) if OperationalPlanning.stop: return if OperationalPlanning.start_or_pause: self.qtime.stop() OperationalPlanning.start_or_pause = False else: self.qtime.start(1000) OperationalPlanning.start_or_pause = True
def modelTrainEvent(self): message = 'the setting of model train' self.log.info(message) Signal.get_signal().emit_signal_str(message) self.window = FramelessWindow('model train') self.dialog = QDialog() description = "a description of model train" model = ModelTrainDescriptionDialog(description) model.setupUi(self.dialog) self.window.setWidget(self.dialog) self.initFrameLessWindow(QSize(700, 600), 'Model Train', QIcon('../resource/drawable/logo.png')) self.window.show()
def outputEvent(self): message = 'situation information: output' self.log.info(message) Signal.get_signal().emit_signal_str(message) self.window = FramelessWindow('output') self.dialog = QDialog() description = "a description of output" situation = SituationDescriptionDialog(description) situation.setupUi(self.dialog) self.window.setWidget(self.dialog) self.initFrameLessWindow(QSize(700, 600), 'Situation Information', QIcon('../resource/drawable/logo.png')) self.window.show()
def mapEvent(self): message = 'open the map description' self.log.info(message) Signal.get_signal().emit_signal_str(message) self.window = FramelessWindow('map description') self.dialog = QDialog() mapDialog = MapDescriptionDialog() mapDialog.setupUi(self.dialog) self.dialog.setModal(True) self.window.setWidget(self.dialog) self.initFrameLessWindow( QSize(700, 600), 'Operational Planning', QIcon('../resource/drawable/logo.png') ) self.window.show()
def radioClicked(self): sender = self.sender() if sender == self.vs_group: message = "" if self.vs_group.checkedId() == 1: message = "change pattern: human vs. machine" # print(message) self.log.info(message) globalInformation.set_value('pattern', strings.HUMAN_VS_MACHINE) elif self.vs_group.checkedId() == 2: message = "change pattern: machine vs. machine" # print(message) self.log.info(message) globalInformation.set_value('pattern', strings.MACHINE_VS_MACHINE) Signal.get_signal().emit_signal_str(message)
def run(self): self.log = logging.getLogger('StarCraftII') for i in range(8): args = get_common_args() map_name = globalInformation.get_value(strings.TYPE_MAP) alg_name = globalInformation.get_value(strings.TYPE_POLICY) if map_name is not None: args.map = map_name if alg_name is not None: args.alg = alg_name args.evaluate_epoch = 100 if args.alg == 'coma': args = get_coma_args(args) elif args.alg == 'commnet_coma': args = get_commnet_args(args) else: args = get_mixer_args(args) env = StarCraft2Env(map_name=args.map, step_mul=args.step_mul, difficulty=args.difficulty, game_version=args.game_version, replay_dir=args.replay_dir, window_size_x=1418, window_size_y=890, window_loc=(5, 155)) env_info = env.get_env_info() args.n_actions = env_info["n_actions"] args.n_agents = env_info["n_agents"] args.state_shape = env_info["state_shape"] args.obs_shape = env_info["obs_shape"] args.episode_limit = env_info["episode_limit"] runner = Runner(env, args) if args.learn: runner.run(i) else: win_rate = runner.evaluate_sparse() message = 'The win rate of {} is {}'.format( args.alg, win_rate) self.log.info(message) Signal.get_signal().emit_signal_str(message) break env.close()
def replayEvent(self): message = 'watch the replay' self.log.info(message) Signal.get_signal().emit_signal_str(message) p = subprocess.Popen(strings.VIEW_REPLAY, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # stdout, _ = p.communicate() # Signal.get_signal().emit_signal_str(str(stdout, encoding="utf-8")) # QApplication.processEvents() returncode = p.poll() while returncode is None: line = p.stdout.readline() returncode = p.poll() line = line.strip() send_str = str(line, encoding="utf-8") if send_str != '': Signal.get_signal().emit_signal_str(send_str) self.log.info(send_str) """
def chooseMapEvent(self): message = 'choose new map' log = logging.getLogger('StarCraftII') log.info(message) Signal.get_signal().emit_signal_str(message) self.window = FramelessWindow('choose map') self.dialog = QDialog() # tab item name list_map = self.list_str if globalInformation.get_value(strings.TYPE_POLICY) is None \ else globalInformation.get_value(strings.ALGORITHM_MAP)[globalInformation.get_value(strings.TYPE_POLICY)] # title name self.listDialog = ListDialog(list_map, None, strings.MAPS_TITLE, strings.TYPE_MAP) self.listDialog.setupUi(self.dialog, self.window) self.window.setWidget(self.dialog) self.initFrameLessWindow(QSize(700, 600), 'Maps', QIcon('../resource/drawable/logo.png')) self.dialog.setModal(True) self.window.show()
def item_choose(self): message = self.list_str[self.list_widget.currentRow()] log = logging.getLogger('StarCraftII') log.info('item change: {}'.format(message)) Signal.get_signal().emit_signal_str('item change: {}'.format(message))
class FightView(QWidget): # define a signal signal_str = Signal.get_signal().signal_str signal_none = Signal.get_signal().signal_none def __init__(self): """ :rtype: """ super(FightView, self).__init__() self.setObjectName('FightView') self.setStyleSheet(GetQssFile.readQss('../resource/qss/fight.qss')) # set the size attribute sizePolicy = QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.sizePolicy().hasHeightForWidth()) self.setSizePolicy(sizePolicy) self.frame = QFrame(self) self.frame.setGeometry(QDesktopWidget().screenGeometry()) # set main layout self.main_layout = QHBoxLayout(self) self.main_layout.setContentsMargins(10, 10, 10, 20) self.main_layout.setSpacing(10) # self.game_widget = EmbeddedWindow() self.game_widget = QWidget() self.main_layout.addWidget(self.game_widget) # set widget of layout self.view = QLabel(self) self.view.setObjectName('view') self.view.setScaledContents(True) self.view.setPixmap(QPixmap('../resource/drawable/view.png')) self.view.setMinimumSize(600, 700) self.main_layout.addWidget(self.view) # self.game_widget.add_widget(self.view) self.information = QPlainTextEdit(self) self.information.setObjectName('information') self.information.setPlaceholderText('the output of simulation') self.information.setReadOnly(True) self.information.setMaximumBlockCount(1000) self.information.setLineWrapMode(QPlainTextEdit.WidgetWidth) self.information.setFrameShape(QFrame.Panel) self.information.setFrameShadow(QFrame.Sunken) self.information.setLineWidth(3) self.information.setMinimumSize(300, 700) self.information.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded) self.information.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded) self.main_layout.addWidget(self.information) # set the stretch of two widget # self.main_layout.addWidget(self.scroll_area) self.main_layout.setStretchFactor(self.view, 3) # self.main_layout.setStretchFactor(self.game_widget, 3) self.main_layout.setStretchFactor(self.information, 1) self.setLayout(self.main_layout) # initialization self.initUI() def initUI(self): self.signal_str.connect(self.updateTextMessage) self.signal_none.connect(self.embeddedWindow) def checkWindow(self): rect = win32gui.GetWindowRect(self.hwnd) self.move(rect[2], rect[1]) @pyqtSlot() def embeddedWindow(self): while win32gui.FindWindow("StarCraft II", None) == 0: pass hwnd = win32gui.FindWindow("StarCraft II", None) hwnd_qwindow = QWindow.fromWinId(hwnd) # hwnd_qwindow.setFlags(Qt.FramelessWindowHint) # self.game_widget.createWindowContainer(hwnd_qwindow, self.game_widget) # self.game_widget.show() phwnd = win32gui.FindWindow('FightView', None) print('hwnd: ', hwnd, ' phwnd: ', phwnd) win32gui.SetParent(hwnd, phwnd) # self.main_layout.addWidget(hwnd_widget) QApplication.processEvents() @pyqtSlot(str) def updateTextMessage(self, message): # self.information.appendPlainText('{}: {}'.format( # time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), message)) self.information.appendPlainText(message) QApplication.processEvents()
def startEvent(self): message = 'start the simulation' self.log.info(message) Signal.get_signal().emit_signal_str(message) # fix rl algorithm globalInformation.set_value(strings.IS_STOP, False) OperationalPlanning.interval = 0 OperationalPlanning.start_or_pause = True OperationalPlanning.stop = False self.qtime.start(1000) def playWithHuman(): subprocess.Popen(strings.PLAY_WITH_HUMAN) def playWithMachine(): """ map_name = '3m' difficulty = '3' param_set = {} param_set['gamma'] = 0.99 param_set['td_lambda'] = 0.8 param_set['learning_rate'] = 0.0005 param_set['alpha'] = 0.99 param_set['eps'] = 1e-05 param_set['epsilon_start'] = 1 param_set['epsilon_end'] = 0.01 param_set['time_length'] = 100000 param_set['grad_norm_clip'] = 10 param_set['before_learn'] = 50 param_set['batch_size'] = 16 param_set['target_update_interval'] = 400 # # # iql set # param_set['algorithm'] = 'iql_CT' # path = '../model/' + map_name + '_iql_CT_3/' # COMA set param_set['algorithm'] = 'COMA' path = '../model/' + map_name + '_COMA_3/' param_set['map_name'] = map_name param_set['difficulty'] = difficulty param_set['path'] = path param_set['load_model'] = True param_set['test'] = True # self.algorithm = MARL() # self.algorithmThread = threading.Thread( # target=self.algorithm.algorithm(param_set), # name='StarCraft2Thread') """ # self.algorithm = AlgorithmAgent() # self.algorithm.start() self.algorithm = MasterAgent() self.algorithm.start() if globalInformation.get_value('pattern') == strings.HUMAN_VS_MACHINE: playWithHuman() elif globalInformation.get_value('pattern') == strings.MACHINE_VS_MACHINE: playWithMachine()
class OperationalPlanning(QWidget): signal_gameover = Signal.get_signal().signal_gameover def __init__(self): super(OperationalPlanning, self).__init__() self.setObjectName('OperationalPlanning') self.setStyleSheet(GetQssFile.readQss('../resource/qss/operationalPlanning.qss')) self.log = logging.getLogger('StarCraftII') # test fix algorithm self.algorithm = None self.algorithmThread = None # font font = QFont() font.setWeight(50) font.setPixelSize(15) # set widget of layout self.frame = QFrame(self) self.frame.setGeometry(QDesktopWidget().screenGeometry()) self.main_layout = QHBoxLayout(self) self.main_layout.setSpacing(20) self.setLayout(self.main_layout) # start action self.start = QPushButton() self.start.setObjectName('start') self.startLabel = QLabel('start') self.startLabel.setFont(font) self.start_layout = QVBoxLayout() self.start_layout.addWidget(self.start, alignment=Qt.AlignCenter) self.start_layout.addWidget(self.startLabel, alignment=Qt.AlignCenter) self.main_layout.addLayout(self.start_layout) # pause action # self.pause = QPushButton() # self.pause.setObjectName('pause') # self.pauseLabel = QLabel('pause') # self.pauseLabel.setFont(font) # self.pause_layout = QVBoxLayout() # self.pause_layout.addWidget(self.pause, alignment=Qt.AlignCenter) # self.pause_layout.addWidget(self.pauseLabel, alignment=Qt.AlignCenter) # self.main_layout.addLayout(self.pause_layout) # stop action self.stop = QPushButton() self.stop.setObjectName('stop') self.stopLabel = QLabel('stop') self.stopLabel.setFont(font) self.stop_layout = QVBoxLayout() self.stop_layout.addWidget(self.stop, alignment=Qt.AlignCenter) self.stop_layout.addWidget(self.stopLabel, alignment=Qt.AlignCenter) self.main_layout.addLayout(self.stop_layout) # switch policy action self.switch = QPushButton() self.switch.setObjectName('switch') self.switchLabel = QLabel('switch policy') self.switchLabel.setFont(font) self.switch_layout = QVBoxLayout() self.switch_layout.addWidget(self.switch, alignment=Qt.AlignCenter) self.switch_layout.addWidget(self.switchLabel, alignment=Qt.AlignCenter) self.main_layout.addLayout(self.switch_layout) # simulation time self.lcd = QLCDNumber() self.lcd.setObjectName('lcd') self.lcd.setDigitCount(10) self.lcd.setMode(QLCDNumber.Dec) self.lcd.setSegmentStyle(QLCDNumber.Flat) self.lcd.setFrameStyle(QFrame.Panel | QFrame.Raised) self.lcd.display(time.strftime("%X", time.localtime())) self.lcdLabel = QLabel('simulation time') self.lcdLabel.setFont(font) self.lcd_layout = QVBoxLayout() self.lcd_layout.addWidget(self.lcd, alignment=Qt.AlignBottom) self.lcd_layout.addWidget(self.lcdLabel, alignment=Qt.AlignBottom) self.main_layout.addLayout(self.lcd_layout) # define time self.qtime = QTimer() self.qtime.timeout.connect(self.refresh) # define global variable global interval interval = 0 global start_or_pause start_or_pause = False global stop stop = True # map description self.map = QPushButton() self.map.setObjectName('map') self.mapLabel = QLabel('map description') self.mapLabel.setFont(font) self.map_layout = QVBoxLayout() self.map_layout.addWidget(self.map, alignment=Qt.AlignCenter) self.map_layout.addWidget(self.mapLabel, alignment=Qt.AlignCenter) self.main_layout.addLayout(self.map_layout) # add stretch self.main_layout.addStretch(1) # popup window self.dialog = None # initialization self.initUI() def refresh(self): timeworker = TimeWorker(self.lcd, OperationalPlanning.interval) OperationalPlanning.interval += 1 timeworker.run() def initUI(self): # connect the slot function self.start.clicked.connect(self.buttonEvent) # self.pause.clicked.connect(self.buttonEvent) self.stop.clicked.connect(self.buttonEvent) self.switch.clicked.connect(self.buttonEvent) self.map.clicked.connect(self.buttonEvent) self.signal_gameover.connect(self.stopEvent) def buttonEvent(self): sender = self.sender() if sender == self.start: self.startEvent() # elif sender == self.pause: # self.pauseEvent() elif sender == self.stop: self.stopEvent() elif sender == self.switch: self.switchEvent() elif sender == self.map: self.mapEvent() # start simulation def startEvent(self): message = 'start the simulation' self.log.info(message) Signal.get_signal().emit_signal_str(message) # fix rl algorithm globalInformation.set_value(strings.IS_STOP, False) OperationalPlanning.interval = 0 OperationalPlanning.start_or_pause = True OperationalPlanning.stop = False self.qtime.start(1000) def playWithHuman(): subprocess.Popen(strings.PLAY_WITH_HUMAN) def playWithMachine(): """ map_name = '3m' difficulty = '3' param_set = {} param_set['gamma'] = 0.99 param_set['td_lambda'] = 0.8 param_set['learning_rate'] = 0.0005 param_set['alpha'] = 0.99 param_set['eps'] = 1e-05 param_set['epsilon_start'] = 1 param_set['epsilon_end'] = 0.01 param_set['time_length'] = 100000 param_set['grad_norm_clip'] = 10 param_set['before_learn'] = 50 param_set['batch_size'] = 16 param_set['target_update_interval'] = 400 # # # iql set # param_set['algorithm'] = 'iql_CT' # path = '../model/' + map_name + '_iql_CT_3/' # COMA set param_set['algorithm'] = 'COMA' path = '../model/' + map_name + '_COMA_3/' param_set['map_name'] = map_name param_set['difficulty'] = difficulty param_set['path'] = path param_set['load_model'] = True param_set['test'] = True # self.algorithm = MARL() # self.algorithmThread = threading.Thread( # target=self.algorithm.algorithm(param_set), # name='StarCraft2Thread') """ # self.algorithm = AlgorithmAgent() # self.algorithm.start() self.algorithm = MasterAgent() self.algorithm.start() if globalInformation.get_value('pattern') == strings.HUMAN_VS_MACHINE: playWithHuman() elif globalInformation.get_value('pattern') == strings.MACHINE_VS_MACHINE: playWithMachine() # Signal.get_signal().emit_signal_none() # pause simulation def pauseEvent(self): message = 'pause the simulation' self.log.info(message) Signal.get_signal().emit_signal_str(message) if OperationalPlanning.stop: return if OperationalPlanning.start_or_pause: self.qtime.stop() OperationalPlanning.start_or_pause = False else: self.qtime.start(1000) OperationalPlanning.start_or_pause = True # stop simulation def stopEvent(self): message = 'stop the simulation' self.log.info(message) Signal.get_signal().emit_signal_str(message) self.qtime.stop() OperationalPlanning.start_or_pause = False OperationalPlanning.stop = True globalInformation.set_value(strings.IS_STOP, True) # a description of current map def mapEvent(self): message = 'open the map description' self.log.info(message) Signal.get_signal().emit_signal_str(message) self.window = FramelessWindow('map description') self.dialog = QDialog() mapDialog = MapDescriptionDialog() mapDialog.setupUi(self.dialog) self.dialog.setModal(True) self.window.setWidget(self.dialog) self.initFrameLessWindow( QSize(700, 600), 'Operational Planning', QIcon('../resource/drawable/logo.png') ) self.window.show() # self.dialog.show() # switch policy of a dialog # there is a description of each algorithm def switchEvent(self): message = 'switch policy' self.log.info(message) Signal.get_signal().emit_signal_str(message) self.window = FramelessWindow('switch policy') self.dialog = QDialog() # tab item name list_str = [ strings.ALGORITHM_COMA, strings.ALGORITHM_COMMNET_COMA, strings.ALGORITHM_QMIX, strings.ALGORITHM_QTRAN_ALT, strings.ALGORITHM_QTRAN_BASE, strings.ALGORITHM_VDN ] # item content list_item = [ strings.CLASS_ALGORITHM_COMA, strings.CLASS_ALGORITHM_COMMNET_COMA, strings.CLASS_ALGORITHM_QMIX, strings.CLASS_ALGORITHM_QTRAN_ALT, strings.CLASS_ALGORITHM_QTRAN_BASE, strings.CLASS_ALGORITHM_VDN ] # title name self.listDialog = ListDialog(list_str, list_item, strings.OPERATIONAL_PLANNING_TITLE, strings.TYPE_POLICY) self.listDialog.setupUi(self.dialog, self.window) self.window.setWidget(self.dialog) self.initFrameLessWindow( QSize(700, 600), 'Operational Planning', QIcon('../resource/drawable/logo.png') ) self.window.show() # self.dialog.setModal(True) # self.dialog.show() def initFrameLessWindow(self, size, title, icon): self.window.resize(size) self.window.setWindowTitle(title) self.window.setWindowIcon(icon)