def right(self, event): """ If right state of the current state does not exist, We make a right move. """ if not (self.history.history[self.history.index - 1][0].right is None): self.matrix = self.history.history[self.history.index - 1][0].right.gamegrid self.history.history[self.history.index - 1][0].destroychildren() self.score = self.history.history[self.history.index - 1][0].right.score self.tilesum = self.history.history[self.history.index - 1][0].right.tilesum self.history.push( self.history.history[self.history.index - 1][0].right, "right") self.gui.update_GUI(self.matrix, self.score) self.changeflag = False return self.reverse() self.stack() self.combine() self.stack() self.reverse() if self.changeflag == True: self.add_new_tile() self.history.history[self.history.index - 1][0].right = State( self.matrix, self.score, self.tilesum) self.history.history[self.history.index - 1][0].destroychildren() self.history.push( self.history.history[self.history.index - 1][0].right, "right") self.gui.update_GUI(self.matrix, self.score) self.changeflag = False
def down(self, event): """ If down state of the current state does not exist, We make a down move. """ if not (self.history.history[self.history.index - 1][0].down is None): self.matrix = self.history.history[self.history.index - 1][0].down.gamegrid self.history.history[self.history.index - 1][0].destroychildren() self.score = self.history.history[self.history.index - 1][0].down.score self.tilesum = self.history.history[self.history.index - 1][0].down.tilesum self.history.push( self.history.history[self.history.index - 1][0].down, "down") self.gui.update_GUI(self.matrix, self.score) self.changeflag = False return self.transpose() self.reverse() self.stack() self.combine() self.stack() self.reverse() self.transpose() if self.changeflag == True: self.add_new_tile() self.history.history[self.history.index - 1][0].down = State( self.matrix, self.score, self.tilesum) self.history.history[self.history.index - 1][0].destroychildren() self.history.push( self.history.history[self.history.index - 1][0].down, "down") self.gui.update_GUI(self.matrix, self.score) self.changeflag = False
def setUp(self): super(StorageTest, self).setUp() self.useFixture(State(self.fakes)) self.fakes.fs.add(os.path.dirname(paths.HOME)) os.makedirs(paths.HOME) os.chown(paths.HOME, 123, 123) self.storage_dir = paths.HOME + '-storage' os.makedirs(self.storage_dir)
def create_statechart(self, param): state_chart = Statechart(param) start = StartState(state_chart) A = HierarchicalState(state_chart, TestEntryClassAction("A"), TestDoClassAction("A"), TestExitClassAction("A")) end = EndState(state_chart) start_a = StartState(A) B = HierarchicalState(A, TestEntryClassAction("B"), TestDoClassAction("B"), TestExitClassAction("B")) E = HierarchicalState(A, TestEntryClassAction("E"), TestDoClassAction("E"), TestExitClassAction("E")) start_b = StartState(B) history_b = HistoryState(B) C = State(B, TestEntryClassAction("C"), TestDoClassAction("C"), TestExitClassAction("C")) D = State(B, TestEntryClassAction("D"), TestDoClassAction("D"), TestExitClassAction("D")) start_e = StartState(E) history_e = HistoryState(E) F = State(E, TestEntryClassAction("F"), TestDoClassAction("F"), TestExitClassAction("F")) G = State(E, TestEntryClassAction("G"), TestDoClassAction("G"), TestExitClassAction("G")) TestTransition(start, 'start', A, 'A', None, None) TestTransition(start_a, 'start_a', B, 'B', None, None) TestTransition(start_b, 'start_b', history_b, 'history_b', None, None) TestTransition(history_b, 'history_b', C, 'C', None, None) TestTransition(start_e, 'start_e', history_e, 'history_e', None, None) TestTransition(history_e, 'history_e', F, 'F', None, None) TestTransition(C, 'C', D, 'D', Event(1), None) TestTransition(D, 'D', C, 'C', Event(2), None) TestTransition(C, 'C', E, 'E', Event(3), None) TestTransition(F, 'F', B, 'B', Event(4), None) TestTransition(F, 'F', G, 'G', Event(5), None) TestTransition(G, 'G', F, 'F', Event(6), None) TestTransition(D, 'D', G, 'G', Event(7), None) TestTransition(E, 'E', D, 'D', Event(8), None) TestTransition(D, 'D', end, 'end', Event(9), None) TestTransition(A, 'A', end, 'end', Event(10), None) return state_chart
def populate_state_space(): candidates = [(s1, s2, s3) for s1 in constants.ACTIONS for s2 in constants.ACTIONS for s3 in constants.ACTIONS] viable_states = [] for candidate in candidates: s = State(candidate) if s.is_viable: viable_states.append(s) return viable_states
def setUp(self): super(StorageTest, self).setUp() self.useFixture(State(self.fakes)) self.fakes.fs.add(os.path.dirname(paths.HOME)) self.fakes.processes.add(fake_mv, name='mv') os.makedirs(paths.HOME) os.chown(paths.HOME, 123, 123) with open(os.path.join(paths.HOME, "jenkins_config"), 'w') as f: f.write("fake config") self.storage_dir = paths.HOME + '-storage' os.makedirs(self.storage_dir)
def init_extra_points_state_graph(verbosity=0): # Construct tap inflow = Quantity("inflow", derivative="+") tap = Tap(inflow=inflow) # Construct container volume = Quantity("volume") height = Quantity("height") pressure = Quantity("pressure") container = Container(volume=volume, height=height, pressure=pressure) # Construct drain outflow = Quantity("outflow") drain = Drain(outflow=outflow) # Set up rules inter_state = [ PositiveInfluence(source="tap.inflow", target="container.volume"), NegativeInfluence(source="drain.outflow", target="container.volume"), PositiveProportion(source="container.volume", target="container.height"), PositiveProportion(source="container.height", target="container.pressure"), PositiveProportion(source="container.pressure", target="drain.outflow"), ] intra_state = [ PositiveConsequence(target="tap.inflow"), NegativeConsequence(target="tap.inflow"), PositiveConsequence(target="container.volume"), NegativeConsequence(target="container.volume"), PositiveConsequence(target="container.height"), NegativeConsequence(target="container.height"), PositiveConsequence(target="container.pressure"), NegativeConsequence(target="container.pressure"), PositiveConsequence(target="drain.outflow"), NegativeConsequence(target="drain.outflow"), VCmax(source="container.volume", target="container.height"), VCzero(source="container.volume", target="container.height"), VCmax(source="container.height", target="container.pressure"), VCzero(source="container.height", target="container.pressure"), VCmax(source="container.pressure", target="drain.outflow"), VCzero(source="container.pressure", target="drain.outflow") ] # Create initial state init_state = State(tap=tap, container=container, drain=drain) # Create state graph state_graph = StateGraph(initial_state=init_state, inter_state=inter_state, intra_state=intra_state, verbosity=verbosity) return state_graph
def get_end_state(self): # Calculates the end state or current state the node is in after applying the players actions end_state_content = list() begin_state_content = self.begin_state.state_content for i, state in enumerate(begin_state_content): end_state_content.append(self.actions[i] if not self.actions[i] == None else begin_state_content[i]) end_state = State(tuple(end_state_content)) return end_state
def create_statechart(self, param): state_chart = Statechart(param) start = StartState(state_chart) A = State(state_chart, TestEntryClassAction("A"), TestDoClassAction("A"), TestExitClassAction("A")) B = State(state_chart, TestEntryClassAction("B"), TestDoClassAction("B"), TestExitClassAction("B")) C = State(state_chart, TestEntryClassAction("C"), TestDoClassAction("C"), TestExitClassAction("C")) end = EndState(state_chart) TestTransition(start, 'start', A, 'A', None, None) TestTransition(A, 'A', B, 'B', Event(1), None) TestTransition(B, 'B', B, 'B', Event(2), None) TestTransition(B, 'B', end, 'end', Event(3), None) TestTransition(B, 'B', C, 'C', Event(4), None) TestTransition(C, 'C', C, 'C', Event(5), None) TestTransition(C, 'C', B, 'B', Event(6), None) TestTransition(C, 'C', A, 'A', Event(7), None) TestTransition(C, 'C', A, 'A', Event(8), None) return state_chart
def start_game(self): """ Initialise game matrix and add 2 tiles of value 2 in the game grid. """ self.matrix = [[0] * 4 for _ in range(4)] self.changeflag = False self.ai = AI() self.score = 0 self.tilesum = 0 self.agentgameover = False self.add_new_tile() self.add_new_tile() self.gui.update_GUI(self.matrix, self.score) self.history.push(State(self.matrix, self.score, self.tilesum), None)
def restart(self, event): """ Remake the GUI of the grid and reset all variables and score. """ for x in self.gui.main_grid.winfo_children(): x.destroy() self.gui.make_GUI() self.matrix = [[0] * 4 for _ in range(4)] self.changeflag = False self.score = 0 self.add_new_tile(2) self.add_new_tile(2) self.gui.update_GUI(self.matrix, self.score) self.history.delete() self.history.push(State(self.matrix, self.score, self.tilesum), None)
def populate_action_dictionary(turn_state_space, state_space): #Populates actions to all possible turn states action_dictionary = dict() for turn_state in turn_state_space: current_player = turn_state.player_turn current_state = turn_state.state current_claim = current_state[current_player] action_candidates = lower_claims(current_claim) viable_actions = list() for action in action_candidates: new_state_contents = list(current_state.state_content) new_state_contents[current_player] = action new_state = State(tuple(new_state_contents)) if new_state in state_space: viable_actions.append(action) action_dictionary[turn_state] = tuple(viable_actions) return action_dictionary
def init_minimum_viable_state_graph(verbosity=0): # Construct tap inflow = Quantity("inflow", derivative="+") tap = Tap(inflow=inflow) # Construct container volume = Quantity("volume") container = Container(volume=volume) # Construct drain outflow = Quantity("outflow") drain = Drain(outflow=outflow) # Set up relationships inter_state = [ PositiveInfluence(source="tap.inflow", target="container.volume"), NegativeInfluence(source="drain.outflow", target="container.volume"), PositiveProportion(source="container.volume", target="drain.outflow") ] intra_state = [ PositiveConsequence(target="tap.inflow"), NegativeConsequence(target="tap.inflow"), PositiveConsequence(target="container.volume"), NegativeConsequence(target="container.volume"), PositiveConsequence(target="drain.outflow"), NegativeConsequence(target="drain.outflow"), VCmax(source="container.volume", target="drain.outflow"), VCzero(source="container.volume", target="drain.outflow") ] # Create initial state init_state = State(tap=tap, container=container, drain=drain) # Create state graph state_graph = StateGraph(initial_state=init_state, inter_state=inter_state, intra_state=intra_state, verbosity=verbosity) return state_graph
def apply(self, state): return State( {atom for atom in state.atoms if ~atom not in self.effects} | {literal for literal in self.effects if literal.sign})
def create_statechart(self, param): state_chart = Statechart(param) start = StartState(state_chart) X = ConcurrentState(state_chart, TestEntryClassAction("X"), TestDoClassAction("X"), TestExitClassAction("X")) TestTransition(start, 'start', X, 'X', None, None) A = HierarchicalState(X, TestEntryClassAction("A"), TestDoClassAction("A"), TestExitClassAction("A")) start_a = StartState(A) history_a = HistoryState(A) D = State(A, TestEntryClassAction("D"), TestDoClassAction("D"), TestExitClassAction("D")) E = State(A, TestEntryClassAction("E"), TestDoClassAction("E"), TestExitClassAction("E")) TestTransition(start_a, 'start_a', history_a, 'history_a', None, None) TestTransition(history_a, 'history_a', D, 'D', None, None) TestTransition(D, 'D', E, 'E', Event(1), None) TestTransition(E, 'E', D, 'D', Event(2), None) B = HierarchicalState(X, TestEntryClassAction("B"), TestDoClassAction("B"), TestExitClassAction("B")) start_b = StartState(B) history_b = HistoryState(B) F = State(B, TestEntryClassAction("F"), TestDoClassAction("F"), TestExitClassAction("F")) G = State(B, TestEntryClassAction("G"), TestDoClassAction("G"), TestExitClassAction("G")) H = State(B, TestEntryClassAction("H"), TestDoClassAction("H"), TestExitClassAction("H")) TestTransition(start_b, 'start_b', history_b, 'history_b', None, None) TestTransition(history_b, 'history_b', F, 'F', None, None) TestTransition(F, 'F', H, 'H', Event(1), None) TestTransition(H, 'H', G, 'G', Event(6), None) TestTransition(G, 'G', F, 'F', Event(2), None) TestTransition(F, 'F', G, 'G', Event(8), None) C = HierarchicalState(state_chart, TestEntryClassAction("C"), TestDoClassAction("C"), TestExitClassAction("C")) start_c = StartState(C) history_c = HistoryState(C) I = State(C, TestEntryClassAction("I"), TestDoClassAction("I"), TestExitClassAction("I")) J = State(C, TestEntryClassAction("J"), TestDoClassAction("J"), TestExitClassAction("J")) K = State(C, TestEntryClassAction("K"), TestDoClassAction("K"), TestExitClassAction("K")) TestTransition(start_c, 'start_c', history_c, 'history_c', None, None) TestTransition(history_c, 'history_c', I, 'I', None, None) TestTransition(J, 'J', X, 'X', Event(13), None) TestTransition(I, 'I', J, 'J', Event(10), None) TestTransition(J, 'J', I, 'I', Event(9), None) TestTransition(J, 'J', K, 'K', Event(12), None) TestTransition(K, 'K', J, 'J', Event(11), None) TestTransition(E, 'E', K, 'K', Event(3), None) TestTransition(B, 'B', C, 'C', Event(5), None) TestTransition(G, 'G', C, 'C', Event(14), None) TestTransition(C, 'C', X, 'X', Event(15), None) return state_chart
def transition_state(state, action, player): state_content = list(state.state_content) state_content[player] = action transitioned_state = State(tuple(state_content)) return transitioned_state
# gettin q0 first_state = lines[2].strip() # getting F finall_nfa_states = lines[3].strip().split(" ") # gettin Q Q = lines[1].strip().split(" ") finall_dfa_stated = set() nfa_states = {} dfa_states = {} for x in Q: # making the State objects according to Q nfa_states[x] = State(x) # speify the real states of the states in Q nfa_states[x].add_real_state(x) for x in finall_nfa_states: # specify the states in nfa_states which are final nfa_states[x].set_final() for i in range(len(lines[4:])): # getting the transitions and add them to the relevant state deltas splited = lines[i + 4].split() nfa_states[splited[0]].add_delta(splited[2], splited[1]) for x in nfa_states: # check if the following state has the transition char 't'
def trade(self, portfolio: Portfolio, stock_market_data: StockMarketData) -> List[Order]: """ Generate action to be taken on the "stock market" Args: portfolio : current Portfolio of this traders stock_market_data : StockMarketData for evaluation Returns: A OrderList instance, may be empty never None """ assert portfolio is not None assert stock_market_data is not None assert stock_market_data.get_companies() == [Company.A, Company.B] # TODO Compute the current state state = State(portfolio, stock_market_data) current_state = state.get_actual_state_for_NN(self.expert_a, self.expert_b) reward = self.get_reward(current_state, self.last_state) self.s = len(current_state) - self.state_size # TODO Train Model if self.train_while_trading is True: # TODO Store state as experience (memory) and train the neural network only if trade() was called before at least once if len(self.memory) == 2000: self.memory.popleft() if len(self.memory) >= self.min_size_of_memory_before_training: # train model actual, target = self.get_actual_and_target( self.batch_size, self.memory, self.model) self.model.fit(actual, target, batch_size=self.batch_size) self.decay_epsilon = True # TODO Create actions for current state and decrease epsilon for fewer random actions action = Action(self.model, self.epsilon, np.array(current_state[self.s:]), self.batch_size).get_action() print(self.epsilon, self.learning_rate) if (self.epsilon > self.epsilon_min and self.decay_epsilon): self.epsilon *= self.epsilon_decay #elif self.epsilon < self.epsilon_min: # self.epsilon = 0.2 if (self.learning_rate > self.learning_rate_min and self.decay_epsilon): self.learning_rate *= self.learning_rate_decay elif self.learning_rate < self.learning_rate_min: # reset the learning rate to its default value self.learning_rate = 0.001 else: action = Action(self.model, 0, np.array(current_state[self.s:]), self.batch_size).get_action() order_list = state.get_order_list_new(action, stock_market_data, portfolio) # TODO Save created state, actions and portfolio value for the next call of trade() if self.last_state is not None and self.last_action is not None: self.memory.append( [self.last_state, self.last_action, reward, current_state]) self.last_state = copy.copy(current_state) self.last_action = copy.copy(action) return order_list
chainList = [] level = int(input('Enter starting level (0-19): ')) gravity = gravityTable[level] pieceList = [] rowList = [] clearList = [] fetchData(1) fetchData(2) print("Initializing possible seeds...") with tqdm(total=N * 8 * 4, leave=True) as pbar: for i in range(N): for j in range(8): for k in range(4): newChain = StateChain(State(seed, j, k, pieceList[-1])) newChain.addFrames(rowList[-1], clearList[-1], gravity) chainList.append(newChain) pbar.update(8 * 4) seed = State.prng(seed, 1) pieceNum = 3 while len(chainList) > 1: print(f'{len(chainList)} possible third piece seeds.') if len(chainList) < 10: for chain in chainList: print(f'{chain.thirdState}, {chain.tailState}') fetchData(pieceNum) newChainList = []
def main(): gameObject = Game() gameObject.gui.after( 200, eventmaker, visited, State(gameObject.matrix, gameObject.score, gameObject.tilesum)) gameObject.gui.mainloop()