Exemple #1
0
    def run( self ):
        for line in sys.stdin:
            state = State( line )

            own = []
            for i in range( 0, state.R ):
                for j in range( 0, state.C ):
                    if state.is_my_cell( i, j ):
                        own.append( ( i, j ) )

            possible = []
            for i in range( 0, state.R ):
                for j in range( 0, state.C ):
                    if not state.is_empty( i, j ): continue
                    for p in own:
                        if State.distance( p, ( i, j ) ) < 2:
                            possible.append( ( i, j ) )
                            break

            xx = ( -1, -1 )
            my = ( state.R, state.C )
            mx = ( state.R, state.C )
            for p in possible:
                if p[ 0 ] > xx[ 0 ]:
                    xx = p
                if p[ 1 ] < my[ 1 ]:
                    my = p
                if p[ 0 ] < mx[ 0 ]:
                    mx = p

            Bot.send_move( [ mx, my, xx ] )
Exemple #2
0
 def loadState(self):
     try:
         state = State().fromFile(os.path.join(self.appdir, 'state.json'))
     except IOError:
         state = State().fromJSON(self.createDefaultState())
     finally:
         state.load()
Exemple #3
0
def test_eu_loop():
    t = Transition([0], [0])
    p = PetriNet([t])
    s1 = State([1], p)
    prop = EUProposition(TrueProposition(), FalseProposition())
    s1.evaluate(prop) # should terminate
    assert len(p._states_cache) == 1
Exemple #4
0
 def tone(self,who,val,unused=None,force=False):
     # who is 'M','A','B','C','D','TR'
     if force or val != self.preset.currentDict[who][1]:
         State.printT('TONE:\t' + str(who) +'\t' + str(val))
         trVal = 'Off'
         toneVal = '0'
         if who =='TR':
             trVal =  str(val-1) if val else 'Off'
             targ = 'M'
             toneVal = None
         elif who == 'M':
             targ = who
             trVal = None
             toneVal = str(val-1) if val else 'Off'
             self.mEval.set(1,val)
         else:
             targ = who
             trVal = '0' if val else 'Off'
             toneVal = str(val-1) if val else 'Off'
         if trVal != None:
             #self.outgoing.append("a.set('%s',State.ToneRange,State.l%s)"%(targ,trVal))
             self.set(targ,State.ToneRange,eval('State.l%s'%trVal))
         if toneVal != None:
             #self.outgoing.append("a.set('%s',State.Tone,State.l%s)"%(targ,toneVal))
             self.set(targ,State.Tone,eval('State.l%s'%toneVal))
         self.preset.currentDict[who][1] = val
         return True
     return False
Exemple #5
0
def test_eg_loop():
    t = Transition([0], [0])
    p = PetriNet([t])
    s1 = State([1], p)
    prop = EGProposition(TrueProposition())
    assert s1.evaluate(prop) == True
    assert len(p._states_cache) == 1
Exemple #6
0
def test_eu_proposition_1():
    t = Transition([0], [1])
    p = PetriNet([t])
    s1 = State([0,2], p)
    prop = EUProposition(LessProposition(NumericExpression(0),
    VariableExpression(0)), EqualsProposition(VariableExpression(1), NumericExpression(3)))
    assert s1.evaluate(prop) == False
Exemple #7
0
def test_eu_proposition_2():
    t = Transition([0], [1])
    p = PetriNet([t])
    s1 = State([0,2], p)
    prop = EUProposition(LessProposition(NumericExpression(0),
    VariableExpression(0)), TrueProposition())
    assert s1.evaluate(prop) == True
Exemple #8
0
def test_dual_loop():
    t1 = Transition([0], [1])
    t2 = Transition([1], [0])
    p = PetriNet([t1, t2])
    s1 = State([0, 1], p)
    prop = EGProposition(TrueProposition())
    assert s1.evaluate(prop) == True
Exemple #9
0
def test_ex_proposition():
    t1 = Transition([0], [1])
    t2 = Transition([1], [2])
    p = PetriNet([t1, t2])
    s1 = State([1,1,0], p)
    prop = EXProposition(EqualsProposition(VariableExpression(0), NumericExpression(0)))
    assert s1.evaluate(prop) == True
 def __init__(self, screen, inputManager, character):
     State.__init__(self, screen, inputManager)
     self.character = character;
     
     
     self.width, self.height = self.screen.get_width(), self.screen.get_height()    
     pygame.display.set_caption("StepFight - Items Menu")
     
     #Items Box dimensions
     self.item_width, self.item_height = 250, 200
     self.item_screen = self.item_width, self.item_height
     
     #Fonts
     self.font_object = pygame.font.Font('freesansbold.ttf', 20)
     
     self.next_state = Consts.STATE_CONTINUE
     
     #Play button
     self.play_button = self._createButton(self.PLAY_BUTTON, Consts.PLAY, (700,525,200,50))
     self.play_button.font_object = pygame.font.Font('freesansbold.ttf', 35)
     self.play_button._setPadding(50, 10)
 
     #Buy buttons
     self.buy_sword_button = self._createButton(self.BUY_SWORD_BUTTON, Consts.BUY, (75, 200,100,40))
     self.buy_helmet_button = self._createButton(self.BUY_HELMET_BUTTON, Consts.BUY, (525, 200,100,40))
     self.buy_armor_button = self._createButton(self.BUY_ARMOR_BUTTON, Consts.BUY, (75, 450,100,40))
     self.buy_spell_button = self._createButton(self.BUY_SPELL_BUTTON, Consts.BUY, (525, 450,100,40))
Exemple #11
0
    def install(self, target: Target, state: State, output: Output):
        """Installs CMake.

        CMake is not easily buildable on Windows so we rely on a binary
        distribution

        Parameters
        ----------
        target: Target
            The target platform and architecture.
        state: State
            The state of the bootstrap build.
        output: Output
            The output helper.
        """
        print("")
        output.print_step_title("Installing CMake")
        if state.cmake_path == "":
            self._install(target)
            print("    CMake installed successfully")
        else:
            self.path = state.cmake_path
            print("    Using previous installation: " + self.path)
        state.set_cmake_path(self.path)
        output.next_step()
Exemple #12
0
 def __init__(self, current=0):
     State.__init__(self)
     self.ui = UI(self, Jules_UIContext)
     self.nextState = GameStart
     logo_duration = 20 * 1000
     scores_duration = 5 * 1000
     self.displays = [(logo_duration, self.draw_logo),
                     (scores_duration, self.draw_high_scores)]
     self.eventid = TimerEvents.SplashScreen
     self.current = current
     self.draw = self.displays[self.current][1]
     self.instructions = ['Can you think fast and react faster?',
                          'This game will test both.',
                          'Your job is to destroy the bonus blocks.',
                          'Sounds easy, right?... Wrong!',
                          'There are several problems.',
                          'If you destroy a penalty block you lose 200 pts.',
                          'If you get 3 penalties, you lose a life.',
                          'If you lose 3 lives, the game is over.',
                          'The bonus and penalty blocks',
                          'change colors every 5 seconds.',
                          'And on top of that, if you do not destroy a',
                          'random non-bonus, non-penalty block every',
                          'couple of seconds, that will give you a',
                          'penalty too. Think you got all that?']
Exemple #13
0
def loadconfig():
    state = State()
    data = yaml.safe_load(file.read(file(configfile, 'r')))

    for k in data:
        try:
            for rule in data[k]['rules']:
                try:
                    source = getattr(network, rule['source'])
                    state.add_rule(source, rule['value'], k, rule['confidence'])
                except TypeError:
                    pass
        except TypeError:
            pass
        except KeyError:
            pass

        try:
            state[k].in_actions.extend(map(lambda string:getattr(actions, string), data[k]['in_actions']))
        except TypeError:
            pass

        try:
            state[k].out_actions.extend(map(lambda string:getattr(actions, string), data[k]['out_actions']))
        except TypeError:
            pass
    return state
Exemple #14
0
    def extend_state(self, state):
        if len(state) == self.d:
            if state.calc_info() >= self.j:
                self.T.append(state)
            return

        Q = []
        g_i = state.most_recent_gene()

        for g_k in self.graph.neighbors_iter(g_i):
            if g_k < g_i: continue
            if g_k in state: continue

            for e in self.C:
                new_state = State(state.genes + [g_k], state.expr_ptn + [e], self.expression)
                redundant = False

                for g_j in state.iter_gene():
                    if new_state.calc_info_omit(g_j) >= new_state.calc_info():
                        redundant = True
                        break

                if not redundant and new_state.calc_info_bound() >= self.j:
                    Q.append(new_state)

        if not Q:
            if state.calc_info() >= self.j:
                self.T.append(state)
            return

        Q_b = sorted(Q, key=lambda s: - s.calc_info())[:self.b]

        for new_state in Q_b:
            self.extend_state(new_state)
 def setUp(self):
     pygame.mixer.init()
     self.player = PlayerSprite()
     self.invScreen = InventoryScreen(self.player)
     self.invScreen.lines = ['Test1', 'Test2']
     State.screens = []
     State.push_screen(self.invScreen)
 def _render(self):
     State._render(self) 
     #background
     self.screen.fill(Consts.BLACK_COLOR)
     self.screen.blit(self.background, self.background.get_rect())
     
     #buttons
     if(self.is_in_resolution):
         self.continue_button._render(self.screen)
         if(self.fight_ended):
             self.font_object = pygame.font.Font('freesansbold.ttf', 30)
             if(self.character.isDead()):
                 self.screen.blit(self.font_object.render("YOU LOST...", False, Consts.RED_COLOR), (270,520))
             else:
                 self.screen.blit(self.font_object.render("YOU WON!!!!!", False, Consts.GOLD_COLOR), (270,520))
         else:
             message = "You"+self.text_character_attack
             self.screen.blit(self.font_object.render(message, False, Consts.WHITE_COLOR), (50,510))
             message = "Dragon"+self.text_dragon_attack
             self.screen.blit(self.font_object.render(message, False, Consts.WHITE_COLOR), (50,550))
     else:
         self.attack_button._render(self.screen)
         self.defense_button._render(self.screen)
         self.spell_button._render(self.screen)
         self.charge_button._render(self.screen)
     
     self.run_button._render(self.screen)
     #characters
     self.dragon.render(self.screen)
     self.character.render(self.screen)
     
     self.print_level()
 def _update(self):
     State._update(self)
     
     if(not self.fight_ended and (self.character.isDead() or self.dragon.isDead()) ):
         self.fight_ended = True
         self.enterResolutionMode()
         if(not self.character.isDead()):
             self.character.level += 1
             Consts.MONEY += 150
     
     #buttons
     if(self.is_in_resolution):
         self.continue_button._update()
     else:
         self.attack_button._update()
         self.defense_button._update()
         self.spell_button._update()
         self.charge_button._update()
     
     self.run_button._update()
     #characters
     self.character.update()
     self.dragon.update()
     
     return self.next_state
Exemple #18
0
 def draw_to_screen(self):
     State.draw_to_screen(self)
     screen = game.screen()
     for shape in self._shapes:
         shape.draw_to_screen(screen)
     self._sprites.draw(screen)
     return
Exemple #19
0
    def handle_keyboard(self, events):
        '''
        Function to handle the keyboard events and act accordingly

        @param events - The list of events from the game
        '''

        self.keyboard_input = { 
            key: (new_val, new_val) 
            for key, (old_val, new_val) 
            in self.keyboard_input.items() 
        }
	

        for event in events:
            if not hasattr(event, 'key'): 
                continue
            if event.type == KEYDOWN:
                if event.key == K_p:
                    State.push_screen(PauseScreen(self.player, self.tileMap))
                if event.key == K_i:
                    State.push_screen(InventoryScreen(self.player))

            if event.key in self.keyboard_input:
                (old_val, new_val) = self.keyboard_input[event.key]
                self.keyboard_input[event.key] = (new_val, event.type)
Exemple #20
0
class Manuscript(object):

    def __init__ (self, num, acc_date = date.today()):
        self.number = num
        self.state = State(acc_date)
        self.eng_rev = False

    def new_state(self, state, date = date.today()):
        '''Indicates that the manuscript reached a new state in the
        publication process'''
        self.state.set_state(state, date)

    def is_revised(self):
        '''Returns true if the manuscript was submitted to language revision'''
        return self.eng_rev

    def set_revised(self):
        '''Informs that a manuscript has been submitted to language revision'''
        self.eng_rev = True

    def __str__(self):
        if self.is_revised():
            eng_status = 'Abstract revised'
        else:
            eng_status = 'Abstract NOT revised'

        return ('\n%s:\n\n%s%s\n' % (self.number, self.state, eng_status))
Exemple #21
0
 def update(self):
     self._sword.y_index = self._sword_top_index + self._current_choice
     State.update(self)
     for shape in self._shapes:
         shape.update()
     self._sprites.update()
     return
Exemple #22
0
 def loadConf(self, conf):
     try:
         #res = self.doParse(conf[self.conf.vocab.configKeys[7]])
         self.doParse(conf[self.conf.vocab.configKeys[7]])
         """
         for e in res:
             #print(e)
             self.outgoing.append(e)
         """
         for key in self.preset.currentDict.keys():
             self.preset.currentDict[key] = conf[key]
     except Exception as e:
         print (e)
         self.doParse(self.conf.presetConf.defaultConfDict[self.conf.vocab.configKeys[7]])
         for key in self.conf.presetConf.defaultConfDict.keys():
             self.preset.currentDict[key] = self.conf.presetConf.defaultConfDict[key]
         self.preset.currentDict[self.conf.vocab.configKeys[0]] = 'DEFAULT PRESET'
     
     self.tone('TR',self.preset.currentDict['TR'][1],force=True)
     for c in ['A','B','C','D','M']:
         self.vol(c,self.preset.currentDict[c][0],force=True)
         self.tone(c,self.preset.currentDict[c][1],force=True)
     self.lcdMgr.loadConf()
     self.trem(self.preset.currentDict[self.conf.vocab.configKeys[8]])
     self.vib(self.preset.currentDict[self.conf.vocab.configKeys[9]])
     self.tracking(self.preset.currentDict[self.conf.vocab.configKeys[10]])
     State.printT(self.outgoing)
Exemple #23
0
    def _apply_change(self, change, remote_id):
        """Apply the change provided to the server state,
        then tell all of the remotes about it
        Params:
        change -- the Change object to apply
        remote_id -- the id number of the remote providing the change
        """
        # find the source state of the change
        source_node = self.root.find_source_of(change, remote_id)
        source_operation = Operation.from_change(change, None)

        # transform down to the tip
        new_tip_operation = source_node.transform_to_tip(source_operation, remote_id)
        new_tip_operation.apply(self.value)
        new_tip = new_tip_operation.end
        
        youngest_root = State(new_tip.pos)

        # tell the remotes about the change
        for cur_remote_id, remote in self.remotes.iteritems():
            if cur_remote_id == remote_id:
                remote.server_ack_to_client(new_tip.make_ack(cur_remote_id), new_tip.pos)
            else:
                remote.server_change_available(self.tip.make_change(cur_remote_id))
            youngest_root.age_to_include(remote.last_acked_state)

        self.tip = new_tip

        # see if I can move the root to a younger node
        while self.root.operation is not None and \
                not self.root.operation.end.pos.is_younger_than(youngest_root):
            self.root = self.root.operation.end
Exemple #24
0
    def __init__(self, surf, prev):
        """

        :param surf:
        :param prev:
        """
        State.__init__(self, surf, prev)
Exemple #25
0
class Game():   
    
    def __init__(self, gru_file=None):
        
        self.compiler = Compiler()
        if gru_file:
            self.stream = self.compiler.decompile(gru_file) 
        else:            
            self.stream = self.compiler.compile(None)   
        self.metadata = self.stream.metadata   
        self.flags = Flags(self.stream)
        self.wheel = Wheel(config)
        self.title = Title(config)
        self.inventory = Inventory(self.stream, self.flags, config)
        self.combiner = Combiner(self.stream, self.flags, self.inventory)
        self.page = Page(config, self.flags, self.combiner, self.inventory)
        self.state = State(self.stream, self.flags, self.inventory, self.wheel, self.combiner, self.title, self.page)               
        if self.metadata.has_key("start"):
            start = self.metadata["start"]
            self.state.update(start)
        else:
            self.state.update("start")

    def draw(self, tick):
        self.inventory.draw()
        self.wheel.draw()
        self.title.draw()
        self.page.draw(tick)

    
        
        
        
Exemple #26
0
class Game:
	def __init__(self, host, port):
		self.gameId = 0
		self.state = None
		self.host = host
		self.port = port

	def init(self, nickname, level):
        response = self.get_post_request(self.host, self.port, '/init/' + str(level), {'nickname': nickname, 'scaffold': 'python'})
		json_root = json.loads(response)
		self.gameId = json_root['gameId']
		self.update_board(json_root['board'])
	
	def move(self, col):
		response = self.get_post_request(self.host, self.port, '/game/move/' + str(self.gameId), {'move': col})
		json_root = json.loads(response)
		self.update_board(json_root['board'])

	def update_board(self, board):
		cols = len(board)
		rows = len(board[0])
		self.state = State(rows, cols)
		for col_i, col in enumerate(board):
			for row_i, slot in enumerate(col):
				self.state.setSlot(row_i, col_i, slot)

	def get_post_request(self, host, port, url, params):
		params = urllib.urlencode(params)
		headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
		conn = httplib.HTTPConnection(host, port)
		conn.request('POST', url, params, headers)
		return conn.getresponse().read()
Exemple #27
0
class StateTests(unittest.TestCase):
    def setUp(self):
        self.state = State('S', ['VP', 'NP'], 0, 0, 0)

    def test_is_complete(self):
        self.assertFalse(self.state.is_complete())

        self.state.dot = 1
        self.assertFalse(self.state.is_complete())

        self.state.dot = 2
        self.assertTrue(self.state.is_complete())

        self.state.dot = 3
        self.assertFalse(self.state.is_complete())

    def test_next_cat(self):
        self.assertEqual(self.state.next_cat(), 'VP')

        self.state.dot = 1
        self.assertEqual(self.state.next_cat(), 'NP')

        self.state.dot = 2
        self.assertEqual(self.state.next_cat(), None)

    def test_after_dot(self):
        self.assertEqual(self.state.after_dot(), 'VP')

        self.state.dot = 1
        self.assertEqual(self.state.after_dot(), 'NP')

        self.state.dot = 2
        self.assertEqual(self.state.after_dot(), None)
Exemple #28
0
    def update(self,name, att, state=State.connectionUpdateOnly):
        """ To call update(...) on name, att, state
        >>> update('A',State.Inverter,State.l2)
        To call update(...) on connections
        >>> update(('A',0),('B',1))
        this method sets up the call to doSettingMasking 
        which makes the member variable assignments
        ---
        Note that there is a procedural difference between updating
        a Vol, Tone,ToneRang, Inverter setting, and updating a connection
        setting.
        In the former case, the non-affecting attributes are maintained. 
        In the latter case, all the connection settings are reset prior to 
        updating. Examples:
        If we had  'A', Vol, l3 already, then we set 'A', Inverter, 1, then
        both the vol and inverter settings are maintained.
        But if we have some connections and we add are starting a new one then
        the previous ones are erased. However, if we have already started
        adding connections, then the previous NEW ones are maintained.
        """
        if state == State.connectionUpdateOnly:
            self.doSettingMasking(connectionsDict[(name,att)],[])
        else:
            # all states can be 'State.lOff', ie None !
            onOff = not state == State.lOff
            (setting, masking) = BitMgr.baseFunc(onOff,
                                                 name,
                                                 att,
                                                 state)
            State.printT(setting,masking)  # this is ok!
            # for a.set('A',State.Inverter,State.l0)
            # ((4, 0), (4, 3)) ((4, 240),)

            self.doSettingMasking(setting,masking)
Exemple #29
0
 def pb0Func(self):
     if self.sequencing:
         State.printT('pb0Func:\tstepping the sequence...')
         #State.debug and input('Press Return:')
         return self.doNextSeq()
     else:
         return false
Exemple #30
0
    def sa_search(graph, budget, timer):
        # Might be able to simplify this.
        cycle = list(range(0, len(graph.nodelist)))
        #random.shuffle(cycle)
        state = State(graph, cycle, [])
        best = state.get_cpy()

        while timer.get_secs() < budget:
            temp = Search.schedule(timer.get_secs(), budget)

            if temp <= 0:
                break

            #nextop = random.choice(oplist)
            nextstate = state.get_cpy()
            nextstate.swap_nodes()
            deltae =  state.cost - nextstate.cost

            if deltae > 0:
                state = nextstate
            else:
                if math.exp(float(deltae)/float(temp)) > random.uniform(0,1):
                    state = nextstate

            if state.cost < best.cost:
                best = state.get_cpy()

        return best
Exemple #31
0
 def __init__(self, name):
     State.__init__(self, name)
Exemple #32
0
 def onPlayBackError(self):  # pylint: disable=invalid-name
     """Will be called when when playback stops due to an error"""
     self.reset_queue()
     self.api.reset_addon_data()
     self.state = State()  # Reset state
Exemple #33
0
 def onPlayBackStopped(self):  # pylint: disable=invalid-name
     """Will be called when user stops playing a file"""
     self.reset_queue()
     self.api.reset_addon_data()
     self.state = State()  # Reset state
Exemple #34
0
 def __init__(self):
     self.api = Api()
     self.state = State()
     self.monitor = Monitor()
     Player.__init__(self)
Exemple #35
0
    while not player_1_name:
        player_1_name = input('\tPlease enter Player 1\'s name: ')

    player_1_char = None
    while player_1_char not in ['X', 'O']:
        player_1_char = input('\tPlease enter Player 1\'s char: ')

    player_2_name = None
    while not player_2_name:
        player_2_name = input('\tPlease enter Player 2\'s name: ')

    player_2_char = 'X' if player_1_char is 'O' else 'O'

    print('Let\'s Begin!\n')

    init_state = State(' ', player_1_char, Grid(tuple([None] * 9)))
    init_node = GameNode(init_state)

    current_node = init_node

    while not current_node.state.is_final():
        print(current_node.state)
        move = input("Enter move (eg. 'a1'): ")
        new_node = deepcopy(current_node)

        if move[0] == 'a':
            col_ix = 3
        elif move[0] == 'b':
            col_ix = 4
        elif move[0] == 'c':
            col_ix = 5
Exemple #36
0
def main():

    # Read server messages from stdin.
    server_messages = sys.stdin

    # Use stderr to print to console through server.
    print(
        f'SearchClient initializing. I am sending this using the error output stream. {datetime.now()}',
        file=sys.stderr,
        flush=True)

    # Initialize level by sending client name
    ClientName = f"{config.client_name}\r".encode("ascii")
    sys.stdout.buffer.write(ClientName)
    sys.stdout.flush()

    # TO DO: Figure out if we have an multiagent or single agent level
    single_agent = False

    if single_agent:
        raise Exception("Not implemented")

    # Read level and create the initial state of the problem.
    client = SearchClient(server_messages)
    '''
    Testing assigner 
    '''
    preprocessing_current_state = State(client.initial_state)
    '''
    Create Dijkstras mapping for all goal location
    
    For all goal_locations do
        Create BFS From location and 
    
    '''
    current_state = State(client.initial_state)
    current_state.dijkstras_map = create_dijkstras_map(
        preprocessing_current_state)

    # Create list of agents
    list_agents = []
    del_agents = []
    for k, v in current_state.reverse_agent_dict().items():
        if k not in client.del_agents_ids:
            # Char int, color, connectedcomp, strategy
            list_agents.append(
                search_agent(k, v[0], v[3], v[2], StrategyBestFirst))
        else:
            del_agents.append(
                search_agent(k, v[0], v[3], v[2], StrategyBestFirst))

    del_locs = []
    for del_id in client.del_agents_ids:
        for loc, agt in current_state.agents.items():
            if agt[0][1] == del_id:
                del_locs.append(loc)

    for loc in del_locs:
        del current_state.agents[loc]

    list_agents_full = list_agents + del_agents
    list_agents_full.sort()
    list_agents.sort()

    #Creating internal id's
    a_inter_id = 0
    for agt in list_agents:
        agt.agent_internal_id = a_inter_id
        a_inter_id += 1
    b_inter_id = 0
    for _k, _v in current_state.boxes.items():
        _v[0][2] = b_inter_id
        b_inter_id += 1

    for k, v in current_state.agents.items():
        for agt in list_agents:
            if v[0][1] == agt.agent_char:
                current_state.agents[k][0][2] = agt.agent_internal_id

    # print(len(current_state.boxes),file=sys.stderr,flush=True)
    goal_assigner = GoalAssigner(current_state,
                                 goal_dependencies=client.goal_dependencies,
                                 list_of_agents=list_agents)
    goal_assigner.reassign_tasks()

    conflict_manager = ConflictManager(list_agents)

    #Fixing conflict in initial plans
    conflict_manager.world_state = current_state
    conflict_manager.blackboard_conflictSolver(list_agents)

    #Agent randomness execution variables
    agent_prev_category = [-1] * len(list_agents_full)
    agent_noop_counter = [0] * len(list_agents_full)
    include_categories = [
        config.goal_assigner_box, config.goal_assigner_location,
        config.self_helping, config.solving_help_task
    ]

    print(f"Data loaded - While enter - time: {datetime.now()}",
          file=sys.stderr,
          flush=True)

    # Whileloop
    counter = 0
    while True:
        '''
        Process of control loop

        1. Identify if any agents is receiving help and should No-Op this iter
        2. Reassign the agents not having a task
            This also handels if an agent is doing a twofold assignemt: move to box -> move box to goal
        3. Solve the conflicts in the prereq and effects of the agents actions
        4. Extract actions and merge irellevant agents
        5. Check if our offline planning is succesfull - if not then shit
        6. Check if we have reached the goal sate
        7. Update the world states in the different elements

        '''
        if testing:
            print(f'-----------------------{counter}-----------------------',
                  file=sys.stderr,
                  flush=True)

        # TODO: Check subgoal for agents - does it need to be updated

        # Keeps it from getting out of hand while testing
        if counter == config.while_counter:
            exit()

        if testing:
            for e in list_agents:
                if len(e.plan) > 0:
                    print(f'{e.agent_char} plan:{e.plan}',
                          flush=True,
                          file=sys.stderr)
                    print(
                        f'{e.plan[0]} {e.agent_char} before goal length: {len(e.plan)} category:{e.plan_category} help from: {e.helper_id} int well {e.in_well_await}',
                        file=sys.stderr,
                        flush=True)
                else:
                    print(
                        f'NoPlan for {e.agent_char} before goal length: {len(e.plan)} category:{e.plan_category} help from: {e.helper_id} int well {e.in_well_await}',
                        file=sys.stderr,
                        flush=True)
            print(f'\n', file=sys.stderr, flush=True)

        # Give task to unassigned agents
        goal_assigner.reassign_tasks()

        if testing:
            for e in list_agents:
                if len(e.plan) > 0:
                    # print(f'{e.agent_char} plan:{e.plan}', flush=True, file=sys.stderr)
                    print(
                        f'>>{e.plan[0]} {e.agent_char} after goal length: {len(e.plan)} category:{e.plan_category} help from: {e.helper_id} int well {e.in_well_await}',
                        file=sys.stderr,
                        flush=True)
                else:
                    print(
                        f'>>NoPlan for {e.agent_char} after goal length: {len(e.plan)} category:{e.plan_category} help from: {e.helper_id} int well {e.in_well_await}',
                        file=sys.stderr,
                        flush=True)
            print(f'\n', file=sys.stderr, flush=True)

        conflict_manager.blackboard_conflictSolver(list_agents)

        if testing:
            for e in list_agents:
                if len(e.plan) > 0:
                    # print(f'{e.agent_char} plan:{e.plan}', flush=True, file=sys.stderr)
                    print(
                        f'>>{e.plan[0]} {e.agent_char} after conflict length: {len(e.plan)} category:{e.plan_category} helping:{e.helper_agt_requester_id}, help from: {e.helper_id} int well {e.in_well_await}',
                        file=sys.stderr,
                        flush=True)
                else:
                    print(
                        f'>>NoPlan for {e.agent_char} after conflict length: {len(e.plan)} category:{e.plan_category}, helping:{e.helper_agt_requester_id}, help from: {e.helper_id} int well {e.in_well_await}',
                        file=sys.stderr,
                        flush=True)

        # Improve speed - find the agetns that are currently executing help tasks
        _helper_agents_ = [
            y for y in list_agents if y.helper_agt_requester_id is not None
        ]

        # Helper agents awaiting help
        # TODO: Check up on nested help correctness
        for x in list_agents:
            _awaiting_done = True
            if x.plan_category == config.solving_help_task and x.nested_help:
                for y in _helper_agents_:

                    # Very beta and f****d - should not happen
                    if x.helper_id is None:
                        x.agent_amnesia()
                        break
                    if x.helper_id[
                            0] == y.helper_agt_requester_id and x.agent_char != y.agent_char:
                        x.plan.appendleft(Action(ActionType.NoOp, None, None))
                        _awaiting_done = False
                if _awaiting_done:
                    x.nested_help = False

        #TODO: Sync here with deleted agents and get their latest actions

        # pop latest action from the full sorted list of actions
        # This functions gives a noop if it does not have a plan
        list_of_actions = [x.get_next_action() for x in list_agents_full]

        if config.initiate_random_agents:
            #Increase counters if agent have been stalling
            for idx, act in enumerate(list_of_actions):
                if act.action_type == ActionType.NoOp:
                    if (list_agents_full[idx].plan_category
                            in include_categories) and (
                                list_agents_full[idx].plan_category
                                == agent_prev_category[idx]):
                        agent_noop_counter[idx] += 1
                else:
                    agent_noop_counter[idx] = 0

            if max(agent_noop_counter) > config.agent_max_stall:
                #Create possible future state only if any value is actually above threshold
                temp_state = State(current_state)
                temp_state.world_state_update(list_of_actions,
                                              client.del_agents_ids)
                agentDict = current_state.reverse_agent_dict()

                #list of temp coordinates that are blocked along the way
                temp_blocked = []

                #Fix random actions
                for idx, num in enumerate(agent_noop_counter):

                    if num > config.agent_max_stall:
                        #If agent have been stalling too long, push random action
                        applicable_action = None
                        found_action = False

                        shuffle(MOVE_ACTIONS)

                        for action in MOVE_ACTIONS:
                            #get location
                            agt_row, agt_col = [
                                int(x) for x in agentDict[idx][1].split(',')
                            ]

                            new_agt_row = agt_row + action.agent_dir.d_row
                            new_agt_col = agt_col + action.agent_dir.d_col

                            loc_string = f'{new_agt_row},{new_agt_col}'

                            if current_state.random_is_free(loc_string) \
                                and temp_state.random_is_free(loc_string) \
                                    and (loc_string not in temp_blocked):
                                applicable_action = action
                                found_action = True

                                break
                        if found_action and list_agents_full[
                                idx].plan_category != config.solving_help_task:
                            #Ask agent to forget everything, but and then push the applicable action
                            agent_noop_counter[idx] = 0
                            list_agents_full[idx].agent_amnesia()

                            list_of_actions[idx] = applicable_action

                            #"Apply" action
                            temp_blocked.append(loc_string)
                        elif found_action and list_agents_full[
                                idx].plan_category == config.solving_help_task:
                            agent_noop_counter[idx] = 0
                            if list_agents_full[
                                    idx].helper_agt_requester_id is not None:
                                if list_agents_full[
                                        idx].helper_agt_requester_id == idx:
                                    list_agents_full[
                                        list_agents_full[idx].
                                        helper_agt_requester_id].agent_amnesia(
                                        )
                                else:
                                    list_agents_full[
                                        list_agents_full[idx].
                                        helper_agt_requester_id].agent_amnesia(
                                        )
                                    agent_noop_counter[list_agents_full[
                                        idx].helper_agt_requester_id] = 0
                                    list_agents_full[idx].agent_amnesia()

                            list_of_actions[idx] = applicable_action

                            # "Apply" action
                            temp_blocked.append(loc_string)

        # push to server -> list of actions
        my_string = ';'.join(list(str(x) for x in list_of_actions))
        print(my_string, flush=True)
        # getting the data from the client to asses validity of actions
        response = server_messages.readline().rstrip()

        if testing:
            print(response, file=sys.stderr, flush=True)

        for resp in response.split(";"):
            if resp == 'false':
                raise Exception(
                    "[FATAL ERROR] received false response from server")

        current_state.world_state_update(list_of_actions,
                                         client.del_agents_ids)

        # print(f'- World_satate after actions: {current_state.agents}',file=sys.stderr,flush=True)
        # print(f'- world_state boxes after actions:  {current_state.boxes}',file=sys.stderr,flush=True)

        if current_state.world_is_goal_state():
            print(
                f"Done with {current_state.levelname} actions used {counter}",
                file=sys.stderr,
                flush=True)
            exit()

        # Update the states of goalassigner and conflictmanager
        conflict_manager.world_state = State(current_state)
        GoalAssigner.world_state = State(current_state)

        #Randomizer updating - keeping track in next iteration of the type of category
        for ct, agt in enumerate(list_agents_full):
            agent_prev_category[ct] = agt.plan_category

        if testing:
            print("\n", file=sys.stderr, flush=True)

        # Stop search from going out for control
        counter += 1
Exemple #37
0
parser.add_argument("--normalize-weights", action='store_true', default=True, help="if set weights/biases are normalized like torch, with std scaled by fan in to the node")
parser.add_argument("--screen-capture-freq", type=int, default=250, help="record screens for a game this often")
parser.add_argument("--save-model-freq", type=int, default=10000, help="save the model once per 10000 training sessions")
parser.add_argument("--observation-steps", type=int, default=50000, help="train only after this many stesp (=4 frames)")
parser.add_argument("--learning-rate", type=float, default=0.00025, help="learning rate (step size for optimization algo)")
parser.add_argument("--target-model-update-freq", type=int, default=10000, help="how often (in steps) to update the target model.  Note nature paper says this is in 'number of parameter updates' but their code says steps. see tinyurl.com/hokp4y8")
parser.add_argument("--model", help="tensorflow model checkpoint file to initialize from")
parser.add_argument("rom", help="rom file to run")
args = parser.parse_args()

print 'Arguments: %s' % (args)

baseOutputDir = 'game-out-' + time.strftime("%Y-%m-%d-%H-%M-%S")
os.makedirs(baseOutputDir)

State.setup(args)

environment = AtariEnv(args, baseOutputDir)

dqn = dqn.DeepQNet(environment.getNumActions(), baseOutputDir, args)

replayMemory = replay.ReplayMemory(args)

maxReward = 0

def runEpoch(minEpochSteps, evalWithEpsilon=None):
    stepStart = environment.getStepNumber()
    isTraining = True if evalWithEpsilon is None else False
    startGameNumber = environment.getGameNumber()
    epochTotalScore = 0
Exemple #38
0
    }
    QCheckBox::indicator {
        width: 20px;
        height: 20px;
    }
    QGraphicsView {
        background-color: transparent; 
        min-height: 0;
    }
    .detector {
        background-color: yellow;
    }
    """)

    # Program state
    state = State()

    # Tray Icon
    tray_icon = SystemTrayIcon(state)
    tray_icon.show()

    # Start device thread
    device_thread = QThread()
    device = connect.Device()
    device.moveToThread(device_thread)
    device_thread.started.connect(device.run)

    # Login Window
    login = LoginWindow(state, device)

    # Calibration Window
def run_an_episode(nlu, w_in, w_s, w_i, w_sa, w_r):

    reward = 0
    currturn = 0
    maxturn = 12
    system = State()

    #user
    user = RuleSimulator()
    print("New episode, user goal:")
    user.goal.dump()
    print('_ _ _ _ _ _ _ _ _ _ _ _')
    over = user.episode_over
    nl_input = initializer(user.goal)
    turn_by_turn(currturn, nl_input, 'user')

    while (not over):
        #system side
        slot, intent = nlu.predict(nl_input, system)
        system.update(slot, intent, nl_input, w_in)
        frame_output, sys_nl = system.reply(w_s, w_i, w_sa)
        #turn_by_turn(currturn+1,sys_nl,'system')
        #print(frame_output)
        a = str(frame_output['system_action'][0])
        a += "("
        # if frame_output['system_action'][0] == "response" or frame_output['system_action'][0] == "confirm_answer":
        # 	for i in frame_output['slot'].keys():
        # 		a += str(i)
        # 		a += ', '
        # elif frame_output['system_action'][0] == 'request':
        # 	for i in frame_output['slot']:
        # 		a += str(i)
        # 		a += ', '

        a += ")\t"
        a += sys_nl
        turn_by_turn(currturn + 1, a, 'system')
        currturn += 2
        if currturn >= maxturn:
            print("[DM] maxturn reached")
            break
        reward -= 2

        #user side
        nl_input = user.next(frame_output)
        if user.syserr == True:
            print("GG")
            system = State()
            user.syserr = False
        turn_by_turn(currturn, nl_input, 'user')
        over = user.episode_over
        if (not over):
            w_r.write(str(reward) + '\n')

    if user.success:
        print("Successful")
        reward += 2 * maxturn
        w_r.write(str(reward) + '\n')
    else:
        print("Failing")
        reward -= maxturn
        w_r.write(str(reward) + '\n')

    return reward, user.success, currturn
Exemple #40
0
def mk_basic_state(alloc, header=None, env=None, executing_on_head=False):
    state = State(root = "6c08c2bdb7c09eb5a2524e9ed8f3dac707c7b0f6ca2116a173989a5370f77340".decode('hex'),env=environment, executing_on_head=executing_on_head)
    print(state.get_balance("3282791d6fd713f1e94f4bfd565eaa78b3a0599d"))
    if not header:
        header = {
            "number": 0, "gas_limit": environment.config['BLOCK_GAS_LIMIT'],
            "gas_used": 0, "timestamp": 1467446877, "difficulty": 1
        }
    h = BlockHeader()
    state.prev_headers = [h]

    for addr, data in alloc.items():
        addr = normalize_address(addr)
        assert len(addr) == 20
        if 'balance' in data:
            state.set_balance(addr, parse_as_int(data['balance']))
        if 'nonce' in data:
            state.set_nonce(addr, parse_as_int(data['nonce']))


    state.block_number = header["number"]
    state.timestamp = header["timestamp"]
    state.commit()

    environment.db.commit()
    return state
Exemple #41
0
    def reset(self, ite=0):
        # Generate node transient and permanent failure events from trace
        if self.use_trace:
            for i in xrange(self.num_nodes):
                self.nodes[i] = Node(None, None, None,
                                     Trace(self.trace_id, i, 'p'),
                                     Trace(self.trace_id, i, 't'),
                                     Trace(self.trace_id, i, 'r'))

        self.state = State(self.num_disks)

        for disk in self.disks:
            disk.init_clock(0)
            disk.init_state()
        for node in self.nodes:
            node.init_state()
        for rack in self.racks:
            rack.init_state()

        self.events_queue = []
        self.wait_repair_queue = []
        self.delayed_repair_dict = dict()

        # generate disk failures and put them into events_queue
        for disk_id in xrange(len(self.disks)):
            disk_fail_time = self.disk_fail_dists.draw()
            if disk_fail_time <= self.mission_time:
                self.events_queue.append(
                    (disk_fail_time, Disk.EVENT_DISK_FAIL, disk_id))
        # generate node failures and push them into events_queue
        for node_id in xrange(self.num_nodes):
            if not self.use_trace:
                self.events_queue.append((self.node_fail_dists.draw(),
                                          Node.EVENT_NODE_FAIL, node_id))
                if self.enable_transient_failures:
                    self.events_queue.append(
                        (self.node_transient_fail_dists.draw(),
                         Node.EVENT_NODE_TRANSIENT_FAIL, node_id))
            else:
                for node_failure_time in self.nodes[
                        node_id].node_fail_trace.get_trace_ls():
                    # push node failure event to event_queue
                    self.events_queue.append(
                        (node_failure_time, Node.EVENT_NODE_FAIL, node_id))
                node_transient_failure_ls = self.nodes[
                    node_id].node_transient_fail_trace.get_trace_ls()
                node_transient_repair_ls = self.nodes[
                    node_id].node_transient_repair_trace.get_trace_ls()
                for ls_idx in xrange(len(node_transient_failure_ls)):
                    node_transient_failure_time = node_transient_failure_ls[
                        ls_idx]
                    node_transient_repair_time = node_transient_repair_ls[
                        ls_idx]
                    self.events_queue.append(
                        (node_transient_failure_time,
                         Node.EVENT_NODE_TRANSIENT_FAIL, node_id))
                    self.events_queue.append(
                        (node_transient_failure_time +
                         node_transient_repair_time,
                         Node.EVENT_NODE_TRANSIENT_REPAIR, node_id))

        # generate rack failures and push them into events_queue
        if not self.use_power_outage and self.enable_transient_failures:
            for rack_id in xrange(len(self.racks)):
                self.events_queue.append((self.rack_fail_dists.draw(),
                                          Rack.EVENT_RACK_FAIL, rack_id))

        # correlated failures caused by power outage
        if (not self.use_trace) and self.use_power_outage:
            for rack_id in xrange(self.num_racks):
                occur_time = float(0) + self.power_outage_dist.draw()
                while occur_time < self.mission_time:
                    self.events_queue.append(
                        (occur_time, Rack.EVENT_RACK_FAIL, rack_id))
                    occur_time += random.expovariate(
                        (1 / float(self.power_outage_duration)))
                    self.events_queue.append(
                        (occur_time, Rack.EVENT_RACK_REPAIR, rack_id))
                    for i in xrange(self.nodes_per_rack):
                        # draw a bernoulli distribution
                        if nprandom.binomial(1, 0.01):
                            self.events_queue.append(
                                (occur_time, Node.EVENT_NODE_FAIL,
                                 (self.nodes_per_rack * rack_id + i)))
                    occur_time += self.power_outage_dist.draw()

        heapify(self.events_queue)
        self.placement = Placement(self.num_racks, self.nodes_per_rack,
                                   self.disks_per_node, self.capacity_per_disk,
                                   self.num_stripes, self.chunk_size,
                                   self.code_type, self.n, self.k,
                                   self.place_type, self.chunk_rack_config,
                                   self.l)

        self.network = Network(self.num_racks, self.nodes_per_rack,
                               self.network_setting)

        self.num_stripes_repaired = 0
        self.num_stripes_repaired_single_chunk = 0
        self.num_stripes_delayed = 0
Exemple #42
0
qui permettent de tester vos fonctions.
Pour cela il suffit de décommenter le code 
de la fonction que vous souhaiter tester
"""

from automate import Automate
from state import State
from transition import Transition
from parser import *
#from essai import Essai

#import projet

print "DEBUT PROGRAMME\n"

s = State(1, False, False)
s2 = State(1, True, False)
t = Transition(s, "a", s)
t2 = Transition(s, "a", s2)
s.insertPrefix(2)
a = Automate([t, t2])
a.prefixStates(3)
a.show("justep")
"""
print "etat s " + s.id
print "s "+ str(s)
print "t "+ str(t)
print "a "+ str(a)
"""

print "s=s2? " + str(s == s2)
Exemple #43
0
    def test_zero_position(self):
        state_array = np.array([[1, 2, 3], [4, 5, 0], [7, 8, 6]])

        state = State(state_array, None, None, (1, 2), ['U', 'L', 'D'])
        self.assertEqual(state.zero_position, (1, 2))
Exemple #44
0
def feature_vec(state, description_output=False):
    angle = state.direction.angle()
    position = (state.position.x, state.position.y)

    position_feature = heinrichs_feature_vec(position)
    position_len = position_feature.shape[0]

    rotation_feature = rotation_feature_vec(angle)
    rotation_len = rotation_feature.shape[0]

    feature_vec = np.append(position_feature, rotation_feature).reshape(
        (1, position_len + rotation_len))

    # TODO: add dictionary which includes all feature creation params
    # currently feature_vec is (872,) MultiBinary numpy array 864 for the field 8 for rotation

    feature_vec = np.transpose(feature_vec)

    if description_output:
        return feature_vec, (position_len, rotation_len)
    else:
        return feature_vec


if __name__ == "__main__":
    from state import State

    print callable(feature_vec)

    print feature_vec(State()).shape
Exemple #45
0
def solveMazeGeneral(maze, algorithm):
    #select the right queue for each algorithm
    if algorithm == "BFS":
        fr = Fringe("FIFO")
    elif algorithm == "DFS":
        fr = Fringe("STACK")
    elif algorithm == "UCS":
        fr = Fringe("PRIO")
        prioFunc = lambda state, room: state.getCost()
    elif algorithm == "GREEDY":
        fr = Fringe("PRIO")
        prioFunc = lambda state, room: room.getHeuristicValue()
    elif algorithm == "ASTAR":
        fr = Fringe("PRIO")
        prioFunc = lambda state, room: state.getCost(
        ) + room.getHeuristicValue()
    else:
        print("algorithm not found/implemented, exit")
        return

    room = maze.getRoom(*maze.getStart())
    state = State(room, None)

    if algorithm == "UCS" or algorithm == "GREEDY" or algorithm == "ASTAR":
        state.setPrio(prioFunc(state, room))

    fr.push(state)
    path = []
    visited = set()

    while not fr.isEmpty():

        while True:
            state = fr.pop()
            room = state.getRoom()
            if room not in visited: break

        path.append(room.getCoords())
        visited.add(room)

        if room.isGoal():
            print("solved")
            fr.printStats()
            state.printPath()
            maze.printMazeWithPath(state)
            return path

        # loop through every possible move
        for d in room.getConnections():
            # get new room after move and cost to get there
            newRoom, cost = room.makeMove(d, state.getCost())
            # if it is an unvisited room
            if newRoom not in visited:
                newState = State(newRoom, state, cost)

                if algorithm == "UCS" or algorithm == "GREEDY" or algorithm == "ASTAR":
                    newState.setPrio(prioFunc(state, room))

                fr.push(newState)

    print("not solved")
    fr.printStats()

    return path
Exemple #46
0
class RegularSimulation(Simulation):
    ##
    # __init__() from Simulation
    #

    ##
    # Initialize the simulation
    #
    def init(self):
        # Initialize the state of the system
        self.state = State(self.num_disks)

        # Employ priority queue to keep all the failures and repairs
        # The element in the queue is (event_time, event_type, device_id)
        self.events_queue = []

        # Keep failed disks awaiting repair
        self.wait_repair_queue = []

        # Keep delayed stripes due to unavailable nodes
        # Key is the disk_idx delayed, value is the list of delayed stripes
        self.delayed_repair_dict = dict()

        self.enable_transient_failures = False

        self.logger = logging.getLogger(__name__)
        self.logger.setLevel(logging.ERROR)
        # self.logger.setLevel(logging.INFO)
        self.logger.addHandler(console)
        self.logger.propagate = False

    ##
    # Reset the simulation
    #
    def reset(self, ite=0):
        # Generate node transient and permanent failure events from trace
        if self.use_trace:
            for i in xrange(self.num_nodes):
                self.nodes[i] = Node(None, None, None,
                                     Trace(self.trace_id, i, 'p'),
                                     Trace(self.trace_id, i, 't'),
                                     Trace(self.trace_id, i, 'r'))

        self.state = State(self.num_disks)

        for disk in self.disks:
            disk.init_clock(0)
            disk.init_state()
        for node in self.nodes:
            node.init_state()
        for rack in self.racks:
            rack.init_state()

        self.events_queue = []
        self.wait_repair_queue = []
        self.delayed_repair_dict = dict()

        # generate disk failures and put them into events_queue
        for disk_id in xrange(len(self.disks)):
            disk_fail_time = self.disk_fail_dists.draw()
            if disk_fail_time <= self.mission_time:
                self.events_queue.append(
                    (disk_fail_time, Disk.EVENT_DISK_FAIL, disk_id))
        # generate node failures and push them into events_queue
        for node_id in xrange(self.num_nodes):
            if not self.use_trace:
                self.events_queue.append((self.node_fail_dists.draw(),
                                          Node.EVENT_NODE_FAIL, node_id))
                if self.enable_transient_failures:
                    self.events_queue.append(
                        (self.node_transient_fail_dists.draw(),
                         Node.EVENT_NODE_TRANSIENT_FAIL, node_id))
            else:
                for node_failure_time in self.nodes[
                        node_id].node_fail_trace.get_trace_ls():
                    # push node failure event to event_queue
                    self.events_queue.append(
                        (node_failure_time, Node.EVENT_NODE_FAIL, node_id))
                node_transient_failure_ls = self.nodes[
                    node_id].node_transient_fail_trace.get_trace_ls()
                node_transient_repair_ls = self.nodes[
                    node_id].node_transient_repair_trace.get_trace_ls()
                for ls_idx in xrange(len(node_transient_failure_ls)):
                    node_transient_failure_time = node_transient_failure_ls[
                        ls_idx]
                    node_transient_repair_time = node_transient_repair_ls[
                        ls_idx]
                    self.events_queue.append(
                        (node_transient_failure_time,
                         Node.EVENT_NODE_TRANSIENT_FAIL, node_id))
                    self.events_queue.append(
                        (node_transient_failure_time +
                         node_transient_repair_time,
                         Node.EVENT_NODE_TRANSIENT_REPAIR, node_id))

        # generate rack failures and push them into events_queue
        if not self.use_power_outage and self.enable_transient_failures:
            for rack_id in xrange(len(self.racks)):
                self.events_queue.append((self.rack_fail_dists.draw(),
                                          Rack.EVENT_RACK_FAIL, rack_id))

        # correlated failures caused by power outage
        if (not self.use_trace) and self.use_power_outage:
            for rack_id in xrange(self.num_racks):
                occur_time = float(0) + self.power_outage_dist.draw()
                while occur_time < self.mission_time:
                    self.events_queue.append(
                        (occur_time, Rack.EVENT_RACK_FAIL, rack_id))
                    occur_time += random.expovariate(
                        (1 / float(self.power_outage_duration)))
                    self.events_queue.append(
                        (occur_time, Rack.EVENT_RACK_REPAIR, rack_id))
                    for i in xrange(self.nodes_per_rack):
                        # draw a bernoulli distribution
                        if nprandom.binomial(1, 0.01):
                            self.events_queue.append(
                                (occur_time, Node.EVENT_NODE_FAIL,
                                 (self.nodes_per_rack * rack_id + i)))
                    occur_time += self.power_outage_dist.draw()

        heapify(self.events_queue)
        self.placement = Placement(self.num_racks, self.nodes_per_rack,
                                   self.disks_per_node, self.capacity_per_disk,
                                   self.num_stripes, self.chunk_size,
                                   self.code_type, self.n, self.k,
                                   self.place_type, self.chunk_rack_config,
                                   self.l)

        self.network = Network(self.num_racks, self.nodes_per_rack,
                               self.network_setting)

        self.num_stripes_repaired = 0
        self.num_stripes_repaired_single_chunk = 0
        self.num_stripes_delayed = 0

    ##
    # Generate permanent disk failure event
    #
    def set_disk_fail(self, disk_idx, curr_time):
        heappush(self.events_queue, (self.disk_fail_dists.draw() + curr_time,
                                     Disk.EVENT_DISK_FAIL, disk_idx))

    ##
    # Generate repair event for permanent disk failure
    #
    def set_disk_repair(self, disk_idx, curr_time):
        if not self.use_network:
            # get the repair time from a pre-defined repair distribution
            heappush(self.events_queue,
                     (self.disk_repair_dists.draw() + curr_time,
                      Disk.EVENT_DISK_REPAIR, disk_idx))
        else:
            # repair time = cross-rack repair traffic / available cross-rack bandwidth
            rack_id = disk_idx / (self.nodes_per_rack * self.disks_per_node)

            # If there is no available bandwidth or the rack is under transient failure
            if self.network.get_avail_cross_rack_repair_bwth() == 0 or \
                self.racks[rack_id].get_curr_state() != Rack.STATE_RACK_NORMAL:
                heappush(self.wait_repair_queue, (curr_time, disk_idx))
            else:
                cross_rack_download = 0
                stripes_to_repair = self.placement.get_stripes_to_repair(
                    disk_idx)
                self.num_stripes_repaired += len(stripes_to_repair)
                stripes_to_delay = []

                # for each stripe to repair
                for stripe_id in stripes_to_repair:
                    num_failed_chunk = 0
                    num_alive_chunk_same_rack = 0
                    num_unavail_chunk = 0
                    idx = 0
                    fail_idx = 0
                    alive_chunk_same_rack = []

                    # check the status of each chunk in the stripe
                    for disk_id in self.placement.get_stripe_location(
                            stripe_id):
                        # get the total number of unavailable chunk (due to permanent/transient failures) in this stripe
                        if self.disks[disk_id].state != Disk.STATE_NORMAL:
                            num_unavail_chunk += 1

                        # for RS, DRC
                        if self.placement.code_type != Placement.CODE_TYPE_LRC:
                            if self.disks[disk_id].get_curr_state(
                            ) == Disk.STATE_CRASHED:
                                num_failed_chunk += 1
                            elif (disk_id / (self.nodes_per_rack *
                                             self.disks_per_node)) == rack_id:
                                num_alive_chunk_same_rack += 1
                        # for LRC
                        else:
                            if self.disks[disk_id].get_curr_state(
                            ) == Disk.STATE_CRASHED:
                                num_failed_chunk += 1
                                if disk_idx == disk_id:
                                    fail_idx = idx
                            elif (disk_id / (self.nodes_per_rack *
                                             self.disks_per_node)) == rack_id:
                                num_alive_chunk_same_rack += 1
                                alive_chunk_same_rack.append(idx)
                            idx += 1

                    # this is a single-chunk repair
                    if num_failed_chunk == 1:
                        self.num_stripes_repaired_single_chunk += 1
                    # the repair for this stripe is delayed
                    if num_unavail_chunk > (self.n - self.k):
                        stripes_to_delay.append(stripe_id)

                    # RS
                    if self.placement.code_type == Placement.CODE_TYPE_RS:
                        if num_alive_chunk_same_rack < self.k:
                            cross_rack_download += (self.k -
                                                    num_alive_chunk_same_rack)
                    # LRC
                    elif self.placement.code_type == Placement.CODE_TYPE_LRC:
                        if num_failed_chunk == 1:
                            # global parity
                            if fail_idx in self.placement.lrc_global_parity:
                                if num_alive_chunk_same_rack < self.k:
                                    cross_rack_download += self.k - num_alive_chunk_same_rack
                            # data chunk or local parity
                            else:
                                # find which group that the failed chunk is in
                                fail_gid = 0
                                for gid in xrange(self.l):
                                    if fail_idx in self.placement.lrc_data_group[gid] or \
                                        fail_idx == self.placement.lrc_local_parity[gid]:
                                        fail_gid = gid
                                        break
                                # find how many chunk in the same rack can be used for repair
                                num_alive_chunk_same_rack = 0
                                for each in alive_chunk_same_rack:
                                    if each in self.placement.lrc_data_group[fail_gid] or \
                                        each == self.placement.lrc_data_group[fail_gid]:
                                        num_alive_chunk_same_rack += 1
                                if num_alive_chunk_same_rack < self.k / self.l:
                                    cross_rack_download += self.k / self.l - num_alive_chunk_same_rack
                        else:
                            if num_alive_chunk_same_rack < self.k:
                                cross_rack_download += (
                                    self.k - num_alive_chunk_same_rack)
                    # DRC
                    elif self.placement.code_type == Placement.CODE_TYPE_DRC:
                        if num_failed_chunk == 1:
                            if self.k == 5 and self.n == 9:
                                cross_rack_download += 1.0
                            elif self.k == 6 and self.n == 9:
                                cross_rack_download += 2.0
                            else:
                                print "Only support DRC - (9,6,3), (9,5,3)"
                        else:
                            if num_alive_chunk_same_rack < self.k:
                                cross_rack_download += (
                                    self.k - num_alive_chunk_same_rack)
                    else:
                        print "Not correct code type in set_disk_repair()!"

                repair_bwth = self.network.get_avail_cross_rack_repair_bwth()
                self.network.update_avail_cross_rack_repair_bwth(0)
                repair_time = cross_rack_download * self.chunk_size / float(
                    repair_bwth)  # seconds
                repair_time /= float(3600)  # hours

                if len(stripes_to_delay) != 0:
                    self.num_stripes_delayed += len(stripes_to_delay)
                    self.delayed_repair_dict[disk_idx] = stripes_to_delay

                self.logger.debug("repair_time = %d, repair_bwth = %d" %
                                  (repair_time, repair_bwth))
                heappush(self.events_queue,
                         (repair_time + curr_time, Disk.EVENT_DISK_REPAIR,
                          disk_idx, repair_bwth))

    ##
    # Generate permanent node failure event
    #
    def set_node_fail(self, node_idx, curr_time):
        heappush(self.events_queue, (self.node_fail_dists.draw() + curr_time,
                                     Node.EVENT_NODE_FAIL, node_idx))

    ##
    # Generate repair event for permanent node failure
    # The repair for the failed node is conducted by the repair for the failed disks on that node
    #
    def set_node_repair(self, node_idx, curr_time):
        for i in xrange(self.disks_per_node):
            disk_idx = node_idx * self.disks_per_node + i
            self.set_disk_repair(disk_idx, curr_time)

    ##
    # Generate transient node failure event
    #
    def set_node_transient_fail(self, node_idx, curr_time):
        heappush(self.events_queue,
                 (self.nodes[node_idx].node_transient_fail_distr.draw() +
                  curr_time, Node.EVENT_NODE_TRANSIENT_FAIL, node_idx))

    ##
    # Generate repair event for transient node failure
    #
    def set_node_transient_repair(self, node_idx, curr_time):
        heappush(self.events_queue,
                 (self.nodes[node_idx].node_transient_repair_distr.draw() +
                  curr_time, Node.EVENT_NODE_TRANSIENT_REPAIR, node_idx))

    ##
    # Generate transient rack failure
    #
    def set_rack_fail(self, rack_idx, curr_time):
        heappush(self.events_queue, (self.rack_fail_dists.draw() + curr_time,
                                     Rack.EVENT_RACK_FAIL, rack_idx))

    ##
    # Generate repair for transient rack failure
    #
    def set_rack_repair(self, rack_idx, curr_time):
        heappush(self.events_queue, (self.rack_repair_dists.draw() + curr_time,
                                     Rack.EVENT_RACK_REPAIR, rack_idx))

    ##
    # Get the next event from the event queue
    #
    def get_next_event(self, curr_time):
        self.logger.debug(
            "len(delayed_repair_dict) = %d, len(wait_repair_queue) = %d" %
            (len(self.delayed_repair_dict), len(self.wait_repair_queue)))
        # If there are some stripes delayed
        if len(self.delayed_repair_dict) != 0:
            items_to_remove = []  # keep the key of the items to remove
            for key in self.delayed_repair_dict:
                tmp_dict_value = []
                for stripe_id in self.delayed_repair_dict[key]:
                    repair_delay = False
                    num_unavail_chunk = 0
                    for disk_idx in self.placement.get_stripe_location(
                            stripe_id):
                        if self.disks[disk_idx].state != Disk.STATE_NORMAL:
                            num_unavail_chunk += 1
                        if num_unavail_chunk > (self.n - self.k):
                            repair_delay = True
                            break
                    if repair_delay:  # stripe whose repair is delayed
                        tmp_dict_value.append(stripe_id)
                if len(tmp_dict_value) == 0:
                    items_to_remove.append(key)
                else:
                    self.delayed_repair_dict[key] = tmp_dict_value
            for key in items_to_remove:
                self.delayed_repair_dict.pop(key)

        # If there are some failed disks awaiting repair
        if len(self.wait_repair_queue) != 0:
            disk_id = self.wait_repair_queue[0][1]
            rack_id = disk_id / (self.nodes_per_rack * self.disks_per_node)
            if self.use_network and self.network.get_avail_cross_rack_repair_bwth() != 0 and \
                self.network.get_avail_intra_rack_repair_bwth(rack_id) != 0 and \
                self.racks[rack_id].get_curr_state() == Rack.STATE_RACK_NORMAL:
                heappop(self.wait_repair_queue)
                self.set_disk_repair(disk_id, curr_time)

        next_event = heappop(self.events_queue)
        next_event_time = next_event[0]
        next_event_type = next_event[1]
        if next_event_time > self.mission_time:
            return (next_event_time, None, None)

        device_idx_set = []
        device_idx_set.append(next_event[2])
        repair_bwth_set = []
        # If use network bandwidth to calculate repair_time
        if self.use_network and next_event_type == Disk.EVENT_DISK_REPAIR:
            repair_bwth_set.append(next_event[3])

        # Gather the events with the same occurring time and event type
        while self.events_queue[0][0] == next_event_time and self.events_queue[
                0][1] == next_event_type:
            next_event = heappop(self.events_queue)
            device_idx_set.append(next_event[2])
            if self.use_network and next_event_type == Disk.EVENT_DISK_REPAIR:
                repair_bwth_set.append(next_event[3])

        # disk permanent failure
        if next_event_type == Disk.EVENT_DISK_FAIL:
            fail_time = next_event_time
            for device_idx in device_idx_set:
                # avoid the case that this disk is under repair
                if self.disks[device_idx].get_curr_state(
                ) != Disk.STATE_CRASHED:
                    if self.delayed_repair_dict.has_key(device_idx):
                        self.delayed_repair_dict.pop(device_idx)
                    # update the state of the disk
                    self.disks[device_idx].fail_disk(fail_time)
                    # generate the repair event
                    self.set_disk_repair(device_idx, fail_time)
            return (fail_time, Disk.EVENT_DISK_FAIL, device_idx_set)

        # node permanent failure
        elif next_event_type == Node.EVENT_NODE_FAIL:
            failed_disks_set = set([])
            fail_time = next_event_time
            for device_idx in device_idx_set:
                # avoid the case that the node is under repair
                if self.nodes[device_idx].get_curr_state(
                ) != Node.STATE_NODE_CRASHED:
                    # update the state of node
                    self.nodes[device_idx].fail_node(fail_time)
                    for i in xrange(self.disks_per_node):
                        disk_idx = device_idx * self.disks_per_node + i
                        failed_disks_set.add(disk_idx)
                        # avoid the case that the disk is under repair
                        if self.disks[disk_idx].get_curr_state(
                        ) != Disk.STATE_CRASHED:
                            if self.delayed_repair_dict.has_key(device_idx):
                                self.delayed_repair_dict.pop(device_idx)
                            # update the state of the disk
                            self.disks[disk_idx].fail_disk(fail_time)
                            # generate the repair event
                            self.set_disk_repair(disk_idx, fail_time)
            return (fail_time, Node.EVENT_NODE_FAIL, failed_disks_set)

        # node transient failure
        elif next_event_type == Node.EVENT_NODE_TRANSIENT_FAIL:
            fail_time = next_event_time
            for device_idx in device_idx_set:
                if self.nodes[device_idx].get_curr_state(
                ) == Node.STATE_NODE_NORMAL:
                    # update the state of node
                    self.nodes[device_idx].offline_node()
                    for i in xrange(self.disks_per_node):
                        disk_id = device_idx * self.disks_per_node + i
                        if self.disks[disk_id].get_curr_state(
                        ) == Disk.STATE_NORMAL:
                            # update the state of disk
                            self.disks[disk_id].offline_disk(fail_time)
                # generate the repair event
                if not self.use_trace:
                    self.set_node_transient_repair(device_idx, fail_time)

            return (fail_time, Node.EVENT_NODE_TRANSIENT_FAIL, None)

        # transient rack failure
        elif next_event_type == Rack.EVENT_RACK_FAIL:
            fail_time = next_event_time
            for device_idx in device_idx_set:
                if self.racks[device_idx].get_curr_state(
                ) == Rack.STATE_RACK_NORMAL:
                    # update the state of the rack
                    self.racks[device_idx].fail_rack(fail_time)
                    for i in xrange(self.nodes_per_rack):
                        # update the state of the node
                        node_idx = device_idx * self.nodes_per_rack + i
                        if self.nodes[node_idx].get_curr_state(
                        ) == Node.STATE_NODE_NORMAL:
                            self.nodes[node_idx].offline_node()
                            for j in xrange(self.disks_per_node):
                                # update the state of the disk
                                disk_idx = node_idx * self.disks_per_node + j
                                if self.disks[disk_idx].get_curr_state(
                                ) == Disk.STATE_NORMAL:
                                    self.disks[disk_idx].offline_disk(
                                        fail_time)
                # generate the repair event
                if not self.use_power_outage:
                    self.set_rack_repair(device_idx, fail_time)

            return (fail_time, Rack.EVENT_RACK_FAIL, None)

        # repair for permanent disk failure
        elif next_event_type == Disk.EVENT_DISK_REPAIR:
            repair_time = next_event_time
            for repair_disk_idx in device_idx_set:
                if self.disks[repair_disk_idx].get_curr_state(
                ) == Disk.STATE_CRASHED:
                    # update the state of the disk
                    self.disks[repair_disk_idx].repair_disk(repair_time)
                    # generate next permanent disk failure
                    self.set_disk_fail(repair_disk_idx, repair_time)

                # if the repair event is caused by permanent node failure
                node_idx = repair_disk_idx / self.disks_per_node
                if self.nodes[node_idx].get_curr_state(
                ) == Node.STATE_NODE_CRASHED:
                    all_disk_ok = True
                    for i in xrange(self.disks_per_node):
                        disk = self.disks[node_idx * self.disks_per_node + i]
                        if disk.get_curr_state() != disk.STATE_NORMAL:
                            all_disk_ok = False
                            break
                    if all_disk_ok:
                        # update the state of the node
                        self.nodes[node_idx].repair_node()
                        # generate next permanent node failure
                        if not self.use_trace:
                            self.set_node_fail(node_idx, repair_time)
            # update the network status
            if self.use_network:
                idx = 0
                for repair_disk_idx in device_idx_set:
                    repair_bwth = repair_bwth_set[idx]
                    self.network.update_avail_cross_rack_repair_bwth(
                        self.network.get_avail_cross_rack_repair_bwth() +
                        repair_bwth)
                    idx += 1

            # return the set of repaired disks
            return (repair_time, Disk.EVENT_DISK_REPAIR, device_idx_set)

        # repair for node transient failure
        elif next_event_type == Node.EVENT_NODE_TRANSIENT_REPAIR:
            repair_time = next_event_time
            for repair_node_idx in device_idx_set:
                # update the state of the node
                if self.nodes[repair_node_idx].get_curr_state(
                ) == Node.STATE_NODE_UNAVAILABLE:
                    self.nodes[repair_node_idx].online_node()
                    # update the state of the disk
                    for i in xrange(self.disks_per_node):
                        disk_id = repair_node_idx * self.disks_per_node + i
                        if self.disks[disk_id].get_curr_state(
                        ) == Disk.STATE_UNAVAILABLE:
                            self.disks[disk_id].online_disk(repair_time)
                # generate the next transient node failure
                if not self.use_trace:
                    self.set_node_transient_fail(repair_node_idx, repair_time)
            return (repair_time, Node.EVENT_NODE_TRANSIENT_REPAIR, None)

        # repair for rack transient failure
        elif next_event_type == Rack.EVENT_RACK_REPAIR:
            repair_time = next_event_time
            for repair_rack_idx in device_idx_set:
                if self.racks[repair_rack_idx].get_curr_state(
                ) == Rack.STATE_RACK_UNAVAILABLE:
                    # update the state of the rack
                    self.racks[repair_rack_idx].repair_rack()
                    for i in xrange(self.nodes_per_rack):
                        node_idx = repair_rack_idx * self.nodes_per_rack + i
                        # update the state of the node
                        if self.nodes[node_idx].get_curr_state(
                        ) == Node.STATE_NODE_UNAVAILABLE:
                            self.nodes[node_idx].online_node()
                            for j in xrange(self.disks_per_node):
                                disk_idx = node_idx * self.disks_per_node + j
                                # update the state of the disk
                                if self.disks[disk_idx].get_curr_state(
                                ) == Disk.STATE_UNAVAILABLE:
                                    self.disks[disk_idx].online_disk(
                                        repair_time)
                # generate the next transient rack failure
                if not self.use_power_outage:
                    self.set_rack_fail(repair_rack_idx, repair_time)

            return (repair_time, Rack.EVENT_RACK_REPAIR, None)

        else:
            self.logger.error('Wrong type of next_event in get_next_event()!')
            return None

    ##
    # Run an iteration of the simulator
    #
    def run_iteration(self, ite=0):
        self.reset()
        curr_time = 0

        self.logger.info(
            "Regular Simulator: begin an iteration %d, num_failed_disks = %d, "
            "avail_cross_rack_bwth = %d" %
            (ite, len(self.state.get_failed_disks()),
             self.network.get_avail_cross_rack_repair_bwth()))

        while True:
            (event_time, event_type,
             disk_id_set) = self.get_next_event(curr_time)
            curr_time = event_time
            if curr_time > self.mission_time:
                break
            # update the whole status
            if not self.state.update_state(event_type, disk_id_set):
                self.logger.error('update_state failed!')
            if event_type != None:
                self.logger.debug(
                    "Time %s, Event type: %s, Number of failed disks: %s\n" %
                    (event_time, event_type,
                     self.state.get_num_failed_disks()))

            # Check durability when disk_failure/node_failure happens
            if event_type == Disk.EVENT_DISK_FAIL or event_type == Node.EVENT_NODE_FAIL:
                if ite == 1:
                    self.logger.info(
                        "Time %s, Event type: %s, Number of failed disks: %s\n"
                        % (event_time, event_type,
                           self.state.get_num_failed_disks()))
                failed_disks = self.state.get_failed_disks()
                if self.placement.check_data_loss(failed_disks):
                    # the number of failed stripes and the number of lost chunks
                    (num_failed_stripes, num_lost_chunks
                     ) = self.placement.get_num_failed_status(failed_disks)
                    # Count in the delayed stripes
                    if len(self.delayed_repair_dict) != 0:
                        for key in self.delayed_repair_dict:
                            num_failed_stripes += len(
                                self.delayed_repair_dict[key])
                            num_lost_chunks += len(
                                self.delayed_repair_dict[key])
                    # Calculate blocked ratio
                    sum_unavail_time = 0
                    for disk_id in xrange(self.num_disks):
                        sum_unavail_time += self.disks[disk_id].get_unavail_time(curr_time) * \
                                            self.placement.get_num_chunks_per_disk(disk_id)
                    blocked_ratio = sum_unavail_time / (
                        self.placement.num_chunks * curr_time)
                    # Calculate the single-chunk repair ratio
                    single_chunk_repair_ratio = 0
                    self.logger.info(
                        "num_stripes_repaired_single_chunk = %d, num_stripes_repaired = %d"
                        % (self.num_stripes_repaired_single_chunk,
                           self.num_stripes_repaired))

                    if self.num_stripes_repaired != 0:
                        single_chunk_repair_ratio = float(self.num_stripes_repaired_single_chunk) / \
                                                    float(self.num_stripes_repaired)

                    return (1, "(%d, %d, %f, %f)" %
                            (num_failed_stripes, num_lost_chunks,
                             blocked_ratio, single_chunk_repair_ratio))

        # No data loss
        # Calculate blocked ratio
        sum_unavail_time = 0
        for disk_id in xrange(self.num_disks):
            sum_unavail_time += self.disks[disk_id].get_unavail_time(self.mission_time) * \
                                self.placement.get_num_chunks_per_disk(disk_id)
        blocked_ratio = sum_unavail_time / (self.placement.num_chunks *
                                            self.mission_time)
        # Calculate the single-chunk repair ratio
        single_chunk_repair_ratio = 0
        if self.num_stripes_repaired != 0:
            single_chunk_repair_ratio = float(self.num_stripes_repaired_single_chunk) / \
                                        float(self.num_stripes_repaired)

        return (0,
                "(0, 0, %f, %f)" % (blocked_ratio, single_chunk_repair_ratio))
Exemple #47
0
 def __init__(self):
     self.initialState = State(0,0)
     self.goalState = State(0,0)
Exemple #48
0
from state import State
from menu import CLI
import sys

if __name__ == '__main__':

    if len(sys.argv) == 2:
        state = State(sys.argv[1].upper())
    else:
        state = State("B")

    menu = CLI(state)
    menu.cli_cycle()
Exemple #49
0
    def __init__(self, rows: int, cols: int, players: [IPlayer], fish_no: int = None) -> None:
        """
        Initializes a referee for a game with a board of size row x col and a given (ordered) list of IPlayer
        objects.

        :param rows: row dimension of the board
        :param cols: column dimension of the board
        :param players: list of IPlayer objects sorted in increasing order of age
        :param fish_no: Number of fish to be placed on each tile on the board
        :return: None
        """
        # Validate params
        if not isinstance(rows, int) or rows <= 0:
            raise TypeError('Expected positive int for rows!')

        if not isinstance(cols, int) or cols <= 0:
            raise TypeError('Expected positive int for cols!')

        if not isinstance(players, list):
            raise TypeError('Expected list for players!')

        # Make sure list consists of only IPlayer objects
        if not all(isinstance(x, IPlayer) for x in players):
            raise TypeError('All player list objects have to of type IPlayer!')

        # Make sure we weren't given too many players
        if len(players) < ct.MIN_PLAYERS or len(players) > ct.MAX_PLAYERS:
            raise ValueError(f'Invalid player length; length has to be between {ct.MIN_PLAYERS} and'
                             f' {ct.MAX_PLAYERS}')

        # Make sure dimensions are large enough to accommodate all players
        if cols * rows < len(players):
            raise ValueError('Board dimensions are too small to accomodate all players!')

        # Make sure fish is between 1 and 5 or is equal to None (default value, means that the user didn't specify a
        # fish number.
        if fish_no is not None and \
                (not isinstance(fish_no, int) or fish_no < ct.MIN_FISH_PER_TILE or fish_no > ct.MAX_FISH_PER_TILE):
            raise ValueError('Expected positive int between 1 and 5 inclusive for fish!')

        # Set properties
        self.__players: [IPlayer] = players
        self.__avatars_per_player = 6 - len(players)

        # Make up list of IPlayer holding failing players
        self.__failing_players: [IPlayer] = []
        # Make up list of IPlayer holding cheating players
        self.__cheating_players: [IPlayer] = []

        # Initialize game update callbacks as a list of callable items called every time
        # the state of the game changes
        self.__game_update_callbacks = []

        # Initializes game over callbacks as a list of callable items called at the end
        # of the game together with the game report
        self.__game_over_callbacks = []

        # Make up a board
        self.__board = self.__make_board(cols, rows, fish_no)

        # Send player's color information
        self.__notify_player_colors()

        # Make up state from board & list of PlayerEntity objects
        self.__state = State(self.__board, [PlayerEntity(p.name, p.color) for p in players])
        # Initialize game tree placeholder
        self.__game_tree = None

        # Make up flag to indicate whether the game has started
        self.__started = False
        # Make up flag to indicate whether the game has ended
        self.__game_over = False
        # Initialize empty game report that will be fleshed out at game end
        self.__report = {}
        # Initialize empty list of IPlayer to hold winners (player(s) with the highest score in the game)
        self.__winners = []
        # Initialize empty list of IPlayer to hold losers
        self.__losers = []
Exemple #50
0
 def _create_state_instance(words):
     state = State(int(words[0]), float(words[1]))
     return state
Exemple #51
0
from depth_first_search import depth_first_search
from bfs import bfs
from manhattan_a_start import Manhattan_A_Start
from euclidian_a_star import Euclidian_A_Start
import numpy as np


def getInitailPuzzle():
    print("Please enter start puzzle:")
    arr = list(map(int, input().split()))
    reshapedNumPyArray = np.array(arr).reshape(3, 3)
    return reshapedNumPyArray.tolist()


#initialStartList = getInitailPuzzle()
initialStartList = [[6, 4, 7], [8, 5, 0], [3, 2, 1]]
goal_list = [[0, 1, 2], [3, 4, 5], [6, 7, 8]]

start_state = State(initialStartList, 0)

dfs = depth_first_search(start_state, goal_list)
#dfs.get_dfs_report()

bfs = bfs(start_state, goal_list)
bfs.get_bfs_report()

manhattan_a_star = Manhattan_A_Start(start_state, goal_list)
#manhattan_a_star.get_manhattan_a_star_report()

euclidian_a_star = Euclidian_A_Start(start_state, goal_list)
#euclidian_a_star.get_euclidian_a_star_report()
Exemple #52
0
class Referee(object):
    """
    PURPOSE:        This class implements a Referee for the game fish. It is purported to provide all the
                    functionality required for running a game from placements to moves and game end. It
                    reports game updates and the final report (at game end) to observers. The latter includes a list
                    of cheating and failing players, as well as a leader board of all rule-abiding players.

                    A cheating player is one that attempts to perform either an illegal placement (placing on an already
                    occupied tile or outside the bounds of the board) or an illegal move (moving via a path that
                    is unclear of holes or avatars, moving to an occupied tile, moving across corners or tiles that
                    are not accessible in a straight line across parallel hexagon edges, moving in-place, and moving
                    outside the bounds of the board). This determination is for placements is made using the State and
                    whereas the one for moves (or actions) is made using the GameTree. Both State and GameTree will
                    raise appropriate exceptions to indicate abnormal conditions should any occur.

                    A failing player is one that fails to return either a placement or an action. More specifically,
                    if a player returns an object of the wrong type (something that is not a Position for
                    get_placement or something that is not an Action for get_action), it is marked as failing.
                    Similarly, if a player takes takes more than PLAYER_TIMEOUT seconds to respond to the referee or
                    throws any exception(s), it is marked out as failing.

                    The referee will prompt the players for moves and placements by passing a deep copy of its
                    game state. This means that exogenous players will not be able to affect the state maintained
                    by the referee.

                    The referee will remove the cheating and failing players' avatars from the game and prevent them
                    from taking any more turns (that includes placing and moving).

    INTERPRETATION: The referee could best be described as the engine that runs a game of Fish. It receives
                    a list of IPlayer objects that is sorted by age and row and column dimensions of the board from
                    the tournament manager, sets up a homogeneous game board of the specified size and assigns each
                    player a color. The board the referee creates is homogeneous (has the same random number of fish on
                    each tile) and may have holes in it (see paragraph on "difficulty factor"). When signaled to kick
                    off the game (via start()) it then prompts each player for a placement by having them return a
                    Position object containing the row and column number they wish to place their avatar. After it
                    finishes prompting users for placements it prompts each movable player for an Action object
                    (made up of a Position describing the place on the board the move is made from and another
                    describing the place the move is made to).

                    To setup the game board, the referee applies a "difficulty factor" - a Natural number that speaks to
                    the maximum number of Tiles the referee will try to remove. This factor is adjustable and can be
                    leveraged to make a game more challenging or less so. The referee will randomly pick the tiles to
                    remove and may even end up removing 0 tiles for a difficulty factor D > 0.

                    The referee also maintains a master copy of the game State which it updates throughout the course of
                    the game to reflect the players' positions, score etc.. Given the state's design, the current player
                    in it will always be unstuck (stuck players are automatically skipped) unless all players are stuck,
                    in which case the referee ends the game.

                    It also provides functionality that external observers can employ to follow the game. An
                    observer or tournament manager subscribe via `subscribe_game_updates` to receive an update with
                    the latest game state every time it changes (this happens whenever a player makes a placement or
                    move, or is kicked). They can also subscribe to an end game report via `subscribe_final_game_report`
                    to receive a copy of the final game report.

                    The final game report encompasses a list of the cheating players, a list of the failing
                    players and a list of dictionary objects sorted in decreasing order of score,
                    each object containing a rule-abiding player's name, color and score.

                    Here's an example of what the report may look like:

                    {
                        'cheating_players': [IPlayer],
                        'failing_players': [IPlayer],
                        'leaderboard': [
                            {'name': 'Winner', 'color': Color.BLACK, 'score': 99},
                            {'name': 'Runner-up', 'color': Color.WHITE, 'score': 40}
                        ]

                    }

                    Upon determining that no more moves can be made (by calling can_anyone_move() on the internal state)
                    , the referee ends the game and provides all players and subscribed observers with the final game
                    report.

                    At initialization, the referee is given a list of IPlayer objects with undefined colors (.color =
                    Color.UNDEFINED). After assigning colors, the referee creates a PlayerEntity for each object,
                    which contains the essential information needed for identification in the game (namely name,
                    color and placements). All other information pertaining to a player is scrapped. The referee
                    starts running the game (from the placement phase onwards) when start() is called on it (presumably
                    the tournament manager would call it to kick off the game).

                    Throughout the game, every time the internal game state is altered, the game tree is updated,
                    players are synchronized (by calling sync on them with the game state) and observers are notified
                    with a version of the latest one. This keeps all parties informed and the game tree up to date for
                    rule-checking.

    DEFINITIONS:    A losing player is one that does not obtain the largest number of fish in the game, or is one that
                    cheats or fails.

                    A winning player is one that obtains the largest number of fish in the game (and does not cheat).
                    There can be multiple winning players if multiple players obtain the same largest number of fish
                    in the game.
    """
    DEBUG = False

    # Initialize difficulty factor
    DIFFICULTY_FACTOR = 2
    # Initialize player timeout (number of seconds a player is allowd to take to make a move/placement)
    PLAYER_TIMEOUT = 1

    def __init__(self, rows: int, cols: int, players: [IPlayer], fish_no: int = None) -> None:
        """
        Initializes a referee for a game with a board of size row x col and a given (ordered) list of IPlayer
        objects.

        :param rows: row dimension of the board
        :param cols: column dimension of the board
        :param players: list of IPlayer objects sorted in increasing order of age
        :param fish_no: Number of fish to be placed on each tile on the board
        :return: None
        """
        # Validate params
        if not isinstance(rows, int) or rows <= 0:
            raise TypeError('Expected positive int for rows!')

        if not isinstance(cols, int) or cols <= 0:
            raise TypeError('Expected positive int for cols!')

        if not isinstance(players, list):
            raise TypeError('Expected list for players!')

        # Make sure list consists of only IPlayer objects
        if not all(isinstance(x, IPlayer) for x in players):
            raise TypeError('All player list objects have to of type IPlayer!')

        # Make sure we weren't given too many players
        if len(players) < ct.MIN_PLAYERS or len(players) > ct.MAX_PLAYERS:
            raise ValueError(f'Invalid player length; length has to be between {ct.MIN_PLAYERS} and'
                             f' {ct.MAX_PLAYERS}')

        # Make sure dimensions are large enough to accommodate all players
        if cols * rows < len(players):
            raise ValueError('Board dimensions are too small to accomodate all players!')

        # Make sure fish is between 1 and 5 or is equal to None (default value, means that the user didn't specify a
        # fish number.
        if fish_no is not None and \
                (not isinstance(fish_no, int) or fish_no < ct.MIN_FISH_PER_TILE or fish_no > ct.MAX_FISH_PER_TILE):
            raise ValueError('Expected positive int between 1 and 5 inclusive for fish!')

        # Set properties
        self.__players: [IPlayer] = players
        self.__avatars_per_player = 6 - len(players)

        # Make up list of IPlayer holding failing players
        self.__failing_players: [IPlayer] = []
        # Make up list of IPlayer holding cheating players
        self.__cheating_players: [IPlayer] = []

        # Initialize game update callbacks as a list of callable items called every time
        # the state of the game changes
        self.__game_update_callbacks = []

        # Initializes game over callbacks as a list of callable items called at the end
        # of the game together with the game report
        self.__game_over_callbacks = []

        # Make up a board
        self.__board = self.__make_board(cols, rows, fish_no)

        # Send player's color information
        self.__notify_player_colors()

        # Make up state from board & list of PlayerEntity objects
        self.__state = State(self.__board, [PlayerEntity(p.name, p.color) for p in players])
        # Initialize game tree placeholder
        self.__game_tree = None

        # Make up flag to indicate whether the game has started
        self.__started = False
        # Make up flag to indicate whether the game has ended
        self.__game_over = False
        # Initialize empty game report that will be fleshed out at game end
        self.__report = {}
        # Initialize empty list of IPlayer to hold winners (player(s) with the highest score in the game)
        self.__winners = []
        # Initialize empty list of IPlayer to hold losers
        self.__losers = []

    @property
    def game_over(self) -> bool:
        """
        Tells whether the game run by this referee has ended.
        """
        return self.__game_over

    @property
    def game_report(self) -> dict:
        """
        Retrieves game report for the game.
        """
        return self.__report.copy()

    @property
    def winners(self) -> [IPlayer]:
        """
        Retrieves the winners in this game.
        """
        return self.__winners

    @property
    def losers(self) -> [IPlayer]:
        """
        Retrieves the losers in this game.
        """
        return self.__losers

    @property
    def state(self) -> State:
        """
        Retrieves the current game state in this game.
        """
        return self.__state

    def start(self) -> None:
        """
        This method starts the game by first running a series of placement rounds and then
        prompting each player to make a move until game end. At game end, it provides all pertinent
        parties with a copy of the game report.

        :return: None
        """
        # Return if we already started
        if self.__started:
            return

        # RUN ON A SEPARATE THREAD
        # Indicate that game has started
        self.__started = True
        # Run placement rounds
        if self.__run_placements():
            # Initialize game tree for rule checking
            self.__game_tree = GameTree(self.__state)
            # Run game
            self.__run_game()

        # End game
        self.__fire_game_over()

    @property
    def players(self) -> [IPlayer]:
        """
        Returns (copy) collection of players referee oversees.
        """
        return pickle.loads(pickle.dumps(self.__players))

    @property
    def cheating_players(self) -> [IPlayer]:
        """
        Returns collection of IPlayer objects corresponding to cheating
        players.

        :return: resulting list of Color
        """
        return self.__cheating_players

    @property
    def failing_players(self) -> [IPlayer]:
        """
        Returns collection of IPlayer objects corresponding to failing
        players.

        :return: resulting list of Color
        """
        return self.__failing_players

    @property
    def started(self) -> bool:
        """
        Returns boolean flag indicating whether the referee has started
        the game. A game is started when the referee prompts the first player to
        make a placement.

        :return: boolean flag indicating the above
        """
        return self.__started

    def __notify_player_colors(self):
        """
        Assign each player the color that correspond to their position in the player list and notify
        each player which colors they will be playing against. If player's fail to acknowledge the color
        messages, their are marked as failing players.
        :return: None
        """
        # Assign each player the color that correspond to their position in the player list
        game_colors = []
        for index, p in enumerate(self.__players):
            ack = utils.timed_call(Referee.PLAYER_TIMEOUT, p, 'set_color', args=(Color(index),))
            game_colors.append(Color(index))
            # if the player doesn't ack, they are a failing player
            if ack is None or not ack:
                self.__failing_players.append(p)

        # Notify each player which colors they will be playing against
        for player in self.__players:
            colors = [color for color in game_colors if color != player.color]
            ack = utils.timed_call(Referee.PLAYER_TIMEOUT, player, 'notify_opponent_colors', args=tuple([colors]))
            # if the player doesn't ack, they are a failing player
            if ack is None or not ack:
                self.__failing_players.append(player)

    def __make_board(self, cols: int, rows: int, fish_no: int) -> Board:
        """
        Makes a board with the given dimensions. It also applies a difficulty factor to
        the board by removing at most DIFFICULTY_FACTOR tiles. What and how many tiles
        is something determined randomly.

        :param cols: number of columns for the board
        :param rows: number of rows for the board
        :param fish: number of fish to be placed on each tile on the board
        :return: resulting Board object
        """

        # number of fish as a range or set number
        fish_no = randrange(ct.MIN_FISH_PER_TILE, ct.MAX_FISH_PER_TILE) if fish_no is None else fish_no

        # Make up board
        board = Board.homogeneous(fish_no, rows, cols)
        # Determine number of tiles to remove given difficulty factor
        tiles_to_remove = min(Referee.DIFFICULTY_FACTOR,
                              rows * cols - len(self.__players) * self.__avatars_per_player)

        for k in range(tiles_to_remove):
            # Generate random row of tile to remove
            random_row = randrange(0, rows - 1)
            # Generate random col of tile to remove
            random_col = randrange(0, cols - 1)
            # Make up location of tile to remove
            tile_location = Position(random_row, random_col)

            # If it's a hole, skip
            if board.get_tile(tile_location).is_hole:
                continue

            # Remove tile
            board.remove_tile(tile_location)

        # Return resulting board
        return board

    def __run_placements(self) -> bool:
        """
        Runs placements rounds until everyone has placed their avatars. Players may
        get removed in the process for either failing or cheating. If all players
        get removed then the function returns False to indicate there is no point
        in pressing forward with the game. Otherwise, it returns True.

        :return: boolean indicating whether any players remain
        """

        # Determine how many avatars there are to place
        avatars_to_place = self.__avatars_per_player * len(self.__players)

        # Prompt players to place until we've exhausted all avatars
        while avatars_to_place > 0:
            # Cycle over players and have them provide a Position object describing where they
            # wish to place their avatars
            for p in self.__players:
                # Check if player has either failed or cheated; if they have, skip 'em over
                if p in self.__failing_players or p in self.__cheating_players:
                    avatars_to_place -= 1
                    continue

                # Get placement for player using a deep copy of state
                placement = utils.timed_call(Referee.PLAYER_TIMEOUT, p, 'get_placement',
                                             args=(self.__state.deepcopy(),))

                # Validate placement received
                if not isinstance(placement, Position):
                    # If it's not a Position, mark out player as failing & remove player from
                    # state
                    self.__kick_player(p, PlayerKickReason.FAILING)
                    # Decrement avatars needed to be placed
                    avatars_to_place -= 1
                    continue

                try:
                    # Try to place on board
                    self.__state.place_avatar(p.color, placement)
                except InvalidPositionException:
                    # Position is out-of-bounds, already occupied or a hole. Mark player
                    # as cheating & remove player from state.
                    self.__kick_player(p, PlayerKickReason.CHEATING)
                    # Decrement avatars needed to be placed
                    avatars_to_place -= 1
                    continue

                if Referee.DEBUG:
                    print(f'got placement of {placement} from player {p.color}')

                self.__fire_game_state_changed()
                # Decrement avatars needed to be placed
                avatars_to_place -= 1

        # Check if any players remain after placement (everyone might have gotten kicked)
        return self.__state.players_no != 0

    def __kick_player(self, player_obj: IPlayer, reason: PlayerKickReason):
        """
        Kicks provided Player from the game.

        :param player_obj: IPlayer object to kick
        :param reason: reason (str) they're being kicked
        """
        # Validate params
        if not isinstance(player_obj, IPlayer):
            raise TypeError('Expected IPlayer object for player_obj!')

        if not isinstance(reason, PlayerKickReason):
            raise TypeError('Expected PlayerKickReason for reason!')

        if Referee.DEBUG:
            print(f'Kicking {player_obj.color} for reason {reason}')

        if reason == PlayerKickReason.CHEATING:
            self.__cheating_players.append(player_obj)
        else:
            self.__failing_players.append(player_obj)

        # Notify player WHY they're being kicked
        player_obj.kick(reason.name)
        # Remove player from state
        self.__state.remove_player(player_obj.color)
        # Trigger event
        self.__fire_game_state_changed()

    def __run_game(self) -> None:
        """
        This method runs the game after placement by prompting each active player
        for an action. A player is active if they can move and have not been removed from
        the game.

        :return: None
        """
        # Run game by prompting players for actions until nobody can move
        while self.__state.can_anyone_move():
            self.__run_turn()

    def __run_turn(self):
        """
        This method runs a single turn by prompting the current player in the internal state
        to make a move.
        """
        current_player_obj = self.__get_player_by_color(self.__state.current_player)
        try:
            # Get action from player using a deep copy of state
            action = utils.timed_call(Referee.PLAYER_TIMEOUT, current_player_obj, 'get_action',
                                       args=(self.__state.deepcopy(),))

            # If call was not successful or anything but an Action object was returned, the player failed
            if not isinstance(action, Action):
                self.__kick_player(current_player_obj, PlayerKickReason.FAILING)
            else:
                # Use game tree to validate action (will throw InvalidPositionException if
                # action is illegal)
                self.__state = self.__game_tree.try_action(action)

                if Referee.DEBUG:
                    print(f'{current_player_obj.color} just moved from {action.src} to {action.dst}')
                self.__fire_game_state_changed()
        except AssertionError as e:
            # Raise assertion errors are these are used for testing
            raise e
        except InvalidActionException:
            self.__kick_player(current_player_obj, PlayerKickReason.CHEATING)

    def __get_player_by_color(self, color: Color) -> IPlayer:
        """
        Retrieves IPlayer object with provided color.

        :param color: Color of player to retrieve
        :return: associated IPlayer object
        """
        # Validate params
        if not isinstance(color, Color):
            raise TypeError('Expected Color for color!')

        for p in self.__players:
            if p.color == color:
                return p

        raise NonExistentPlayerException()

    def __fire_game_state_changed(self):
        """
        Signals that the game state has changed and it is time to update the game tree, sync all the players and
        notify all subscribed observers about the new state. It notifies observers so by calling their provided
        callbacks on a copy of the latest game state.
        """
        # Update game tree
        self.__game_tree = GameTree(self.__state)

        # Notify all parties subscribed for game updates
        state_to_broadcast = self.__state.deepcopy()

        # Cycle over players and sync them
        for p in self.__players:
            p.sync(state_to_broadcast)

        # Cycle over game update callbacks and call each one
        # with a copy of the latest state
        for callback in self.__game_update_callbacks:
            try:
                callback(state_to_broadcast)
            except AssertionError as e:
                # Raise assertion exceptions are these are used for testing
                raise e
            except Exception as e:
                print(f'Exception occurred, removing observer: {e}')

    def __get_game_report(self) -> dict:
        """
        Retrieves the final game report. It encompasses a list of the cheating players' colors,
        a list of the failing players' colors and a list of dictionary objects sorted in decreasing
        order of score, each object containing the respective player's name, color and score.

        Here's an example of what the report may look like:

        {
            'cheating_players': [Color.BROWN],
            'failing_players': [Color.RED],
            'leaderboard': [
                {'name': 'Winner', 'color': Color.BLACK, 'score': 99},
                {'name': 'Runner-up', 'color': Color.WHITE, 'score': 40}
            ]

        }

        :return: resulting dict object
        """
        # Make up array to hold leaderboard
        leaderboard = []

        # Cycle over rule-abiding players and collect their name, color & score
        for p in self.__state.players:
            # Only add player to leaderboard if they were rule-abiding
            if p not in self.__failing_players and p not in self.__cheating_players:
                leaderboard.append({'name': p.name, 'color': p.color, 'score': p.score})

        # Sort leader board in decreasing order of score
        leaderboard.sort(key=operator.itemgetter('score'), reverse=True)

        # Return report
        return {
            'cheating_players': self.__cheating_players,
            'failing_players': self.__failing_players,
            'leaderboard': leaderboard
        }

    def __get_player_by_name(self, name: str) -> IPlayer:
        for p in self.__players:
            if p.name == name:
                return p

    def __fire_game_over(self) -> None:
        """
        Signals the game is over and dispatches the final game report to all subscribed
        observers.
        """
        # Retrieve report
        self.__report = self.__get_game_report()

        # Set flag
        self.__game_over = True

        # Determine highest score in the game
        max_score = max([p['score'] for p in self.__report['leaderboard']]) \
            if len(self.__report['leaderboard']) > 0 else 0

        # Determine names of winners
        winner_names = [p['name'] for p in self.__report['leaderboard'] if p['score'] == max_score]
        loser_names = [p['name'] for p in self.__report['leaderboard'] if p['score'] < max_score]

        # Determine winners with the highest scores by name
        self.__winners = [self.__get_player_by_name(name) for name in winner_names]
        # Determine losers by adding players with scores < highest_score, failing & cheating players
        self.__losers = [self.__get_player_by_name(name) for name in loser_names]

        self.__losers.extend(self.__report['failing_players'])
        self.__losers.extend(self.__report['cheating_players'])

        if Referee.DEBUG:
            print(f'Game over report: {self.__report}')

        # Give each player a copy of the report
        for player in self.__players:
            player.game_over(self.__report['leaderboard'], self.__report['cheating_players'],
                             self.__report['failing_players'])

        # Cycle over game update callbacks and call each observer with the report
        for callback in self.__game_over_callbacks:
            try:
                callback(self.__report)
            except AssertionError as e:
                # Raise assertion exceptions are these are used for testing
                raise e
            except Exception as e:
                # Callback has failed
                if Referee.DEBUG:
                    print(f'Game over callback has failed: {e}')

    def subscribe_game_updates(self, callback: 'Callable') -> None:
        """
        Subscribes caller for game state updates by way of a callable
        object that is called with a copy of the internal game state
        every time said game state changes. i.e. callback(state)

        :param callback: callback function call state on
        :return: None
        """
        # Validate params
        if not callable(callback):
            raise TypeError('Expected callable for callback!')

        # Add to list of callbacks
        self.__game_update_callbacks.append(callback)

    def subscribe_final_game_report(self, callback: 'Callable'):
        """
        Subscribes observers for the final game report by way of a callable
        object that is called with a copy of the final game report
        when the game ends. i.e. callback(report).

        :param callback: callback function call report on
        :return: None
        """
        # Validate params
        if not callable(callback):
            raise TypeError('Expected callable for callback!')

        # Add to list of callbacks
        self.__game_over_callbacks.append(callback)
Exemple #53
0
    def isEmpty(self):
        return len(self.fnodes) == 0

    def check(self, n):
        return n in self.fnodes

    def print(self):
        print("<Frontier>")
        for n in self.fnodes:
            n.print()
        print("</Frontier>")


if __name__ == "__main__":
    f = Frontier()
    goalA = State(3, [[0, 1, 2], [3, 4, 5], [6, 7, 8]])

    n = Node(None, goalA, 1, 5)

    # n.print()
    f.add(n)
    # f.print()
    f.add(Node(None, goalA, 1, 3))
    # f.print()
    f.add(Node(None, goalA, 1, 7))
    # f.print()
    f.add(Node(None, goalA, 1, 4))
    f.add(Node(None, goalA, 1, 6))
    f.print()

    print(f.check(n))
import statistics as stats
import numpy as np

# If the disks are in different pins, we name the state first with where the big one is
statesString = ["b1s1", "b1s2", "b1s3", "s2b2", "s3b3", "b3s2", "b2s3", "b3s3", "b2s2", "b3s1", "b2s1", "s1b1"]
obeyProb = 0.9
moves = ["s1","s2","s3", "b1", "b2","b3"]
GAMMA = 0.9
# Creation of every state object
states = []
statesTable = {}
q_table = {}
q_table_all_values = {}
times_action_executed = {}
for state in statesString:
	new_state = State(state, obeyProb)
	states.append(new_state)
	statesTable[new_state.name] = new_state
	q_table[state] = [-9999 for x in range(0, len(moves))]
	q_table_all_values[state] = [[] for x in range(0, len(moves))]
	times_action_executed[state] = [1 for x in range(0, len(moves))]

state_to_action = {
    states[0].name: ["s2", "s3"],
    states[1].name: ["s1", "s3", "b2", "b3"],
    states[2].name: ["s1", "s2", "b2", "b3"],
    states[3].name: ["b1", "b3"],
    states[4].name: ["b1", "b2"],
    states[5].name: ["s1", "s3", "b1", "b2"],
    states[6].name: ["s1", "s2", "b1", "b3"],
    states[7].name: ["s3"],
    def __init__(self, embs, data, stage, model_dir = None):
        if model_dir is not None:
            self._classify = Classify(model_dir)
            self._labels = [item.strip() for item in open(model_dir + "/relations.txt").read().splitlines()]
        else:
            self._labels = None

        if stage == "ORACLETEST":
            assert(len(data) == 4)
            hooks = False
            tokens, dependencies, relations, alignments = data
            lemmas = None
            relations2 = []
            self.gold = relations
            for r in relations:
                if r[1].startswith(":snt"):
                    r2 = (Node(True),":top",r[2])
                else:
                    r2 = (r[0],r[1],r[2])
                if (r2[0].token is not None or r2[1] == ":top") and r2[2].token is not None:
                    relations2.append(r2)
            oracle = Oracle(relations2)
            self.variables = Variables()
    
        elif stage == "TRAIN" or stage == "COLLECT":
            assert(len(data) == 4)
            hooks = False
            tokens, dependencies, relations, alignments = data
            lemmas = None
            relations2 = []
            for r in relations:
                if r[1].startswith(":snt"):
                    r2 = (Node(True),":top",r[2])
                else:
                    r2 = (r[0],r[1],r[2])
                if (r2[0].token is not None or r2[1] == ":top") and r2[2].token is not None:
                    relations2.append(r2)
            oracle = Oracle(relations2)
            self.variables = None

        else: #PARSING
            assert(len(data) == 2)
            hooks = True
            tokens, dependencies = data
            relations2 = None
            alignments = None
            oracle = None
            self.variables = Variables()
        self.state = State(embs, relations2, tokens, dependencies, alignments, oracle, hooks, self.variables, stage, Rules(self._labels))
        self.history = History()
        while self.state.isTerminal() == False:
            #print self.state
            tok = copy.deepcopy(self.state.buffer.peek())
            if oracle is not None:
                action = oracle.valid_actions(self.state)
            else:
                action = self.classifier()
            #print action
            #raw_input()
            if action is not None:
                f_rel = []
                f_lab = []
                f_reentr = []
                if stage == "TRAIN":
                    f_rel = self.state.rel_features()
                    if action.name== "larc" or action.name == "rarc":
                        f_lab = self.state.lab_features()
                    if action.name == "reduce":
                        f_reentr = self.state.reentr_features()

                self.state.apply(action)
                self.history.add((f_rel, f_lab, f_reentr), action, tok)
            else:
                break
        assert (self.state.stack.isEmpty() == True and self.state.buffer.isEmpty() == True)
Exemple #56
0
RESIGN = np.zeros(shape=226, dtype=np.float32)
RESIGN[-1] = 1


def one_hot(action):
    h = np.zeros(shape=226, dtype=np.float32)
    h[np.ravel_multi_index(action, dims=(15, 15))] = 1.0
    return h


for name in os.listdir("minimax"):
    if os.path.isfile("minimax/" + name) and name.endswith(".pkl"):
        with open("minimax/" + name, "rb") as f:
            obj = pickle.load(f)
            moves = obj["history"]
            winner = obj["winner"]
            s = State()
            for i, move in enumerate(moves):
                X.append(s.featurize())
                if i == len(moves) - 2 and winner != 0:
                    Y.append(RESIGN)
                else:
                    Y.append(one_hot(move))
                V.append(winner)
                s.move(*move)
            stat[winner] += 1
            print("processed " + name)

savemat("dual_minimax", {"X": X, "Y": Y, "V": V}, do_compression=True)
print("black win %d white win %d even %d" % (stat[1], stat[-1], stat[0]))
Exemple #57
0
 def transition(self):
     TimerEvents().stop(self.eventid)
     State.transition(self)
Exemple #58
0
 def __init__(self, models, alphabet, seqlen):
     self.alphabet = alphabet
     self.modelchain = [ State(models[i], 0 if i == len(models)-1 else 1/seqlen, 0.1, self.alphabet) for i in range(len(models)) ]
     self.seqlen = seqlen
     self.states = [ [ markovState() for _ in range(len(models))] for _ in range(seqlen)]
Exemple #59
0
 def start(self):
     if self.replace == None:
         TimerEvents().start(self.eventid, self.countdown)
     State.start(self)
class TransitionSystem:

    def __init__(self, embs, data, stage, model_dir = None):
        if model_dir is not None:
            self._classify = Classify(model_dir)
            self._labels = [item.strip() for item in open(model_dir + "/relations.txt").read().splitlines()]
        else:
            self._labels = None

        if stage == "ORACLETEST":
            assert(len(data) == 4)
            hooks = False
            tokens, dependencies, relations, alignments = data
            lemmas = None
            relations2 = []
            self.gold = relations
            for r in relations:
                if r[1].startswith(":snt"):
                    r2 = (Node(True),":top",r[2])
                else:
                    r2 = (r[0],r[1],r[2])
                if (r2[0].token is not None or r2[1] == ":top") and r2[2].token is not None:
                    relations2.append(r2)
            oracle = Oracle(relations2)
            self.variables = Variables()
    
        elif stage == "TRAIN" or stage == "COLLECT":
            assert(len(data) == 4)
            hooks = False
            tokens, dependencies, relations, alignments = data
            lemmas = None
            relations2 = []
            for r in relations:
                if r[1].startswith(":snt"):
                    r2 = (Node(True),":top",r[2])
                else:
                    r2 = (r[0],r[1],r[2])
                if (r2[0].token is not None or r2[1] == ":top") and r2[2].token is not None:
                    relations2.append(r2)
            oracle = Oracle(relations2)
            self.variables = None

        else: #PARSING
            assert(len(data) == 2)
            hooks = True
            tokens, dependencies = data
            relations2 = None
            alignments = None
            oracle = None
            self.variables = Variables()
        self.state = State(embs, relations2, tokens, dependencies, alignments, oracle, hooks, self.variables, stage, Rules(self._labels))
        self.history = History()
        while self.state.isTerminal() == False:
            #print self.state
            tok = copy.deepcopy(self.state.buffer.peek())
            if oracle is not None:
                action = oracle.valid_actions(self.state)
            else:
                action = self.classifier()
            #print action
            #raw_input()
            if action is not None:
                f_rel = []
                f_lab = []
                f_reentr = []
                if stage == "TRAIN":
                    f_rel = self.state.rel_features()
                    if action.name== "larc" or action.name == "rarc":
                        f_lab = self.state.lab_features()
                    if action.name == "reduce":
                        f_reentr = self.state.reentr_features()

                self.state.apply(action)
                self.history.add((f_rel, f_lab, f_reentr), action, tok)
            else:
                break
        assert (self.state.stack.isEmpty() == True and self.state.buffer.isEmpty() == True)
        
    def classifier(self):
        digits, words, pos, deps = self.state.rel_features()
        constr = self.state.legal_actions()
        acttype = int(self._classify.action(digits, words, pos, deps, constr))
        assert(acttype > 0 and acttype < 5)

        if acttype == 1:
            sg = self.state.nextSubgraph()
            return Action("shift", sg)

        if acttype == 2:
            reentr_features = self.state.reentr_features()
            siblings = [item[0] for p in self.state.stack.relations.parents[self.state.stack.top()] for item in self.state.stack.relations.children[p[0]] if item[0] != self.state.stack.top()]
            for s, feats in zip(siblings,reentr_features):
                words, pos, deps = feats
                pred = int(self._classify.reentrancy(words, pos, deps))
                if pred == 1:
                    arg0_idx = 9
                    if self.state.legal_rel_labels("reent", (self.state.stack.top(), s))[arg0_idx] == 1:
                        return Action("reduce", (s, ":ARG0", None))
                break
            return Action("reduce", None)

        if acttype == 3:
            rel = "larc"
        elif acttype == 4:
            rel = "rarc"
        constr = self.state.legal_rel_labels(rel, 1)
        digits, words, pos, deps = self.state.lab_features()
        pred = int(self._classify.label(digits, words, pos, deps, constr))
        return Action(rel,self._labels[pred - 1])

    def statesactions(self):
        return self.history.statesactions()

    def relations(self):
        return self.state.stack.relations.triples()

    def alignments(self):
        return self.history.alignments