def round(self, state, action): if action == Actions.hit: card = self.draw_card() if card.color == Colors.black: value = card.value else: value = -card.value next_state = State(state.dealer_sum, state.player_sum + value) if next_state.player_sum < 1 or next_state.player_sum > 21: next_state.terminal = True next_state.reward = -1 else: next_state = State(state.dealer_sum, state.player_sum) draw_again = True while draw_again: card = self.draw_card() if card.color == Colors.black: value = card.value else: value = -card.value next_state.dealer_sum = next_state.dealer_sum + value if next_state.dealer_sum < 1 or next_state.dealer_sum > 21: next_state.reward = 1 draw_again = False elif next_state.dealer_sum > 16: if next_state.dealer_sum < next_state.player_sum: next_state.reward = 1 elif next_state.dealer_sum == next_state.player_sum: next_state.reward = 0 else: next_state.reward = -1 draw_again = False next_state.terminal = True return next_state
def __init__(self): threading.Thread.__init__(self) # Objects self.ball_measure = State(p_x=0, p_y=0, phi_z=0, v_x=0, v_y=0, w_z=0) self.ball_set = State(p_x=0, p_y=0, phi_z=0, v_x=0, v_y=0, w_z=0) self.robot = State(p_x=0, p_y=0, phi_z=0, v_x=0, v_y=0, w_z=0) self.ang_set_left = 0 self.ang_set_right = 0 self.V_set_left = 0 self.V_set_right = 0 self.impact_point = (0, 0) self.ang2impact_left = 0 self.ang2impact_right = 0
def matrix_to_states(V, QN, E=None): #Find dimensions of matrix matrix_dimensions = V.shape #Initialize a list for storing eigenstates eigenstates = [] for i in range(0, matrix_dimensions[1]): #Find state vector state_vector = V[:, i] #Ensure that largest component has positive sign index = np.argmax(np.abs(state_vector)) state_vector = state_vector * np.sign(state_vector[index]) state = State() #Get data in correct format for initializing state object for j, amp in enumerate(state_vector): state += amp * QN[j] if E is not None: state.energy = E[i] #Store the state in the list eigenstates.append(state) #Return the list of states return eigenstates
def main(): # Sets the random seed which is useful for debugging random.seed(int(sys.argv[1])) number_of_moves = int(sys.argv[2]) board = list(map(int,sys.stdin.read().split())) board = [[board[0],board[1],board[2]],[board[3],board[4],board[5]],[board[6],board[7],board[8]]] state = State(board) for i in range(0,3): for j in range(0,3): if board[i][j] == 0: state.setx(i) state.sety(j) x = 0 # Loops through until the number of moves requested have been made successfully while x < number_of_moves: move = random.randrange(4) # (up (0), down (1), left (2), right (3)). if move == 0 and state.up(): state = state.up() x += 1 elif move == 1 and state.down(): state = state.down() x += 1 elif move == 2 and state.left(): state = state.left() x += 1 elif move == 3 and state.right(): state = state.right() x += 1 print(state)
def matrix_to_states(V, QN, E=None): #Find dimensions of matrix matrix_dimensions = V.shape #Initialize a list for storing eigenstates eigenstates = [] for i in range(0, matrix_dimensions[1]): #Find state vector state_vector = V[:, i] data = [] #Get data in correct format for initializing state object for j, amp in enumerate(state_vector): data.append((amp, QN[j])) #Store the state in the list state = State(data) if E is not None: state.energy = E[i] eigenstates.append(state) #Return the list of states return eigenstates
def vector_to_state(state_vector, QN, E=None): data = [] #Get data in correct format for initializing state object for j, amp in enumerate(state_vector): data.append((amp, QN[j])) return State(data)
def vector_to_state(state_vector, QN, E=None): state = State() #Get data in correct format for initializing state object for j, amp in enumerate(state_vector): state += amp * QN[j] return state
def deriveQ(self): temp_Q = np.zeros((self.env.dealer_values, self.env.player_values, self.env.action_values)) for i in range(self.env.dealer_values): for j in range(self.env.player_values): for k in range(self.env.action_values): phi = self.feature_computation(State(i, j), k) temp_Q[i, j, k] = sum(phi * self.theta) return temp_Q
def test_state(): chan = "#chan" # join state = State() user = User("oreqizer", None) state.join(user, chan) assert len(state.channels) == 1, f"state.join: state.channels: {len(state.channels)} == 1" ch = state.channels[chan] assert len(user.channels) == 1, f"state.join: user.channels: {len(user.channels)} == 1" assert len(ch.users) == 1, f"state.join: ch.users: {len(ch.users)} == 1" # part state = State() user = User("oreqizer", None) state.join(user, chan) ch = state.channels[chan] state.part(user, chan) assert len(user.channels) == 0, f"state.part: user.channels: {len(user.channels)} == 0" assert len(ch.users) == 0, f"state.part: ch.users: {len(ch.users)} == 0" # get_channel state = State() user = User("oreqizer", None) assert_throw(lambda: state.get_channel(chan)) state.join(user, chan) ch = state.channels[chan] assert state.get_channel( chan) == ch, f"state.get_channel: {state.get_channel(chan).name} == {ch.name}" # get_user_channel state = State() user = User("oreqizer", None) assert_throw(lambda: state.get_user_channel(user, chan)) state.users[user.nickname] = user assert_throw(lambda: state.get_user_channel(user, chan)) ch = Channel(chan) state.channels[chan] = ch assert_throw(lambda: state.get_user_channel(user, chan)) state.join(user, chan) assert state.get_user_channel( user, chan) == ch, f"state.get_user_channel: {state.get_user_channel(user, chan).name} == {ch.name}" print("test_state OK")
def process_statement(self): token = self.current_token if token.type == RESERVED: if token.value == 'alphabet:': self.pop_token(RESERVED) while self.current_token.type == LETTER_SMALL: self.alphabet.append(self.current_token) if self.lexer.expr_end(): break self.pop_token(LETTER_SMALL) elif token.value == 'states:': self.pop_token(RESERVED) while self.current_token.type == LETTER_CAPITAL: self.states.append(State(name=self.current_token.value)) if self.lexer.expr_end(): break self.pop_token(LETTER_CAPITAL) if self.current_token.type == COMMA: self.pop_token(COMMA) elif token.value == 'final:': self.pop_token(RESERVED) while self.current_token.type == LETTER_CAPITAL: self.find_state_change_final(self.current_token.value) if self.lexer.expr_end(): break self.pop_token(LETTER_CAPITAL) if self.current_token.type == COMMA: self.pop_token(COMMA) elif token.value == 'transitions:': self.pop_token(RESERVED) while not self.current_token.value == 'end.': origin = self.current_token.value if type(self.initial_state) == str: for state in self.states: if state.state_name == origin: self.initial_state = state self.pop_token(LETTER_CAPITAL) self.pop_token(COMMA) edge = self.current_token.value if self.current_token.type == LETTER_SMALL: self.pop_token(LETTER_SMALL) else: self.pop_token(UNDERSCORE) self.pop_token(DASH) self.pop_token(DASH) self.pop_token(ANGLE_BRACKET) destination = self.current_token.value self.pop_token(LETTER_CAPITAL) self.transitions.append(Transition(origin=origin, edge=edge, destination=destination)) if self.lexer.expr_end(): break self.pop_token(RESERVED) else: print('Unexpected type!')
def main(): selection = int(sys.argv[1]) # Reads in the board from the random_board.py output board = list(map(int, sys.stdin.read().split())) board = [[board[0], board[1], board[2]], [board[3], board[4], board[5]], [board[6], board[7], board[8]]] state = State(board) # Setting the x and y position for the blank tile for i in range(0, 3): for j in range(0, 3): if board[i][j] == 0: state.setx(i) state.sety(j) # Initialize the relevent data structures curr_node = Node(state, h(state, selection)) closed = Set() frontier = PriorityQueue() frontier.push(curr_node) V = 0 # Loops through and creates the search tree while objective(curr_node.getstate()) == False: closed.add(curr_node.getstate()) # Creates new states based on the valid_moves returned by the successor for valid_state in successor(curr_node.getstate()): V += 1 # Checking that the state we are eveluating has not already been expanded. Only adds to the queue if false if closed.isMember(valid_state) == False: frontier.push( Node(valid_state, h(valid_state, selection), curr_node)) curr_node = frontier.pop() N = closed.length() + frontier.length() d = curr_node.getd() print("V=%d" % (V)) print("N=%d" % (N)) print("d=%d" % (d)) print("b=%f" % (N**(1 / d))) print() # Create an empty list in order to retrieve the path from the goal node's parents stack = [] while curr_node.getid() != 0: stack.append(curr_node) curr_node = curr_node.getparent() print(curr_node.getstate()) # Prints out the solution path for node in reversed(stack): if node != None: print(node.getstate())
def main_process(fo): fname = "state.pkl" dump_fieldstorage(fo) if os.path.isfile(fname): with open(fname, "rb") as f: st = pickle.load(f) else: st = State() st = edit_state(st, fo) bd = get_main_body(st) with open("tmp.pkl", "wb") as f: pickle.dump(st, f) shutil.move("tmp.pkl", fname) return bd
def perf_timesteps(n: int, use_srq: bool, infinite: bool = False) -> RunResult: """Simulate a day of n timesteps. Returns a RunResult """ # Initialize variables to keep track of data state = State(use_srq) results = RunResult(use_srq) for time in range(n): state = step_time(state, time, use_srq, infinite=infinite) # Step forward 1 timestep # Add data to df_timesteps. The time, line length and boat occupancy are tracked results.add_timestep(time, state) # Add data to df_groups. One row per group, keeping track of sizes and times. results.add_groups(time, state) state.departed_groups = None return results
def a_star_search(puzzle, start, args): openset = [] seenset = {} tiebreaker = 0 heapq.heappush(openset, (start.g + start.h_total, start.h_total, tiebreaker, start)) seenset[start.matrix.tobytes()] = start.g time, space = 0, 0 while len(openset) > 0: current = heapq.heappop(openset)[3] if args.verbose: print("current node heuristic value: ", current.h_total) time += 1 if current.h_total == 0: if not args.uniform and args.manhattan: return current, time, space elif np.array_equal(current.matrix, puzzle.goal_array): return current, time, space for matrix, zero_loc in current.get_neighbours(puzzle): move = State(matrix) move.zero_tile = zero_loc move.parent = current get_optimized_heuristics(move, puzzle, args) move.g = current.g + G key = (move.matrix.tobytes()) seen = key in seenset if not seen or move.g < seenset[key]: if not seen: space += 1 seenset[key] = move.g heapq.heappush( openset, (move.g + move.h_total, move.h_total, tiebreaker, move)) tiebreaker += 1 print("can't be solved") sys.exit()
def edit_state(st, fo): if "Add" in fo and "child" in fo: console("Add") cname = fo["child"].value if st.name_exists(cname): cid = st.id_by_name(cname) else: cid = generate_time_hash() pids = set() if "parent" in fo: na = fo["parent"].value if st.name_exists(na): pids = {st.id_by_name(na)} st.enqueue(Node(cid, cname, pids)) elif "Next" in fo and st.non_empty_queue(): console("Next") st.pop() elif "Clear" in fo and "option" in fo and fo["option"].value == "clear": console("Clear") st = State() elif "Rename" in fo and "edited" in fo and "To" in fo: console("Rename") if st.name_exists(fo["To"].value): return st if st.name_exists(fo["edited"].value): nid = st.id_by_name(fo["edited"].value) st.nodes[nid].name = fo["To"].value elif "Merge" in fo and "edited" in fo and "Mto" in fo: if st.name_exists(fo["edited"].value) and st.name_exists( fo["Mto"].value): st.merge_names(fo["edited"].value, fo["Mto"].value) elif "Delete" in fo and "edited" in fo: console("Delete") edited = fo["edited"].value if st.name_exists(edited): did = st.id_by_name(edited) st.delete_by_id(did) return st
def get_initial_state( sample_size=None, number_of_clusters=20, save=True, cache=True, initial_location_depot=True, ) -> State: # If this combination has been requested before we fetch a cached version if cache and os.path.exists( f"{STATE_CACHE_DIR}/c{number_of_clusters}s{sample_size}.pickle"): print( f"\nUsing cached version of state from {STATE_CACHE_DIR}/c{number_of_clusters}s{sample_size}.pickle\n" ) return State.load_state( f"{STATE_CACHE_DIR}/c{number_of_clusters}s{sample_size}.pickle") print( f"\nSetup initial state from entur dataset with {number_of_clusters} clusters and {sample_size} scooters" ) clustering = Bar("| Clustering data", max=4) # Get dataframe from EnTur CSV file within boundary entur_dataframe = methods.read_bounded_csv_file( "test_data/0900-entur-snapshot.csv") clustering.next() # Create clusters cluster_labels = methods.cluster_data(entur_dataframe, number_of_clusters) clustering.next() # Structure data into objects clusters = methods.generate_cluster_objects(entur_dataframe, cluster_labels) clustering.next() # generate depots and adding them to clusters list depots = methods.generate_depots(number_of_clusters=len(clusters)) clustering.next() # Choosing first cluster as starting cluster in state current_location = depots[0] if initial_location_depot else clusters[0] clustering.finish() # Choosing a default vehicle as the vehicle in the new state vehicle = Vehicle() # Create state object initial_state = State(clusters, depots, current_location, vehicle) # Sample size filtering. Create list of scooter ids to include sample_scooters = methods.scooter_sample_filter(entur_dataframe, sample_size) # Find the ideal state for each cluster initial_state.compute_and_set_ideal_state(sample_scooters) # Trip intensity analysis initial_state.compute_and_set_trip_intensity(sample_scooters) # Get probability of movement from scooters in a cluster probability_matrix = methods.scooter_movement_analysis(initial_state) initial_state.set_probability_matrix(probability_matrix) if sample_size: initial_state.sample(sample_size) if save: # Cache the state for later initial_state.save_state() print("Setup state completed\n") return initial_state
def SIRV_cluster_simulator(T, infect, λ, λ_s, β, n, p, λ_v, n_v): """ Simulates an epidemic clustered with the functionality of vaccinations over a arbitrary time range Arg(s): T: Amount of time to simulate over infect: String input that is the class of the infected entity λ: meet rate 2x2 matrix λ_s: meet rate for symptomatic and C1 beta: recovery rate n: dict of the size of each class p: dict of probabilities an infected agent is symptomatic λ_v: rate a vaccination event happens n_v: the number of people in a group that are vaccinated during a vaccination event (default is one) """ #initializing the state of the system based on the inputs sim_state = State(λ, λ_s, β, n, p, λ_v, n_v) for key, value in n.items(): for i in range(n[key]): agent = Agent(key) sim_state.S[key].append(agent) sim_state.V[key].append(agent) sim_state.V_tot.append(agent) if key == 'C1': sim_state.medical_class.append(agent) elif key == 'C2': sim_state.essential_nm.append(agent) elif key == 'C3': sim_state.high_risk_ne.append(agent) else: sim_state.low_risk_ne.append(agent) #infecting a single person of classs 'infect' ent = sim_state.S[infect].pop(0) ent.state = 'I_a' sim_state.I[infect].append(ent) sim_state.infected.append(ent) count = 0 while count < T: #retrieving rate values across classes λ_C1 = calculate_rate(sim_state, 'C1') λ_C2 = calculate_rate(sim_state, 'C2') λ_C3 = calculate_rate(sim_state, 'C3') λ_C4 = calculate_rate(sim_state, 'C4') #total recovery rate β_tot = β * (len(sim_state.I['C1']) + len(sim_state.I_sym) + len(sim_state.I['C2']) + \ len(sim_state.I['C3']) + len(sim_state.I['C4'])) #total vaccination rate if len(sim_state.V_tot) > 0: λv_tot = (λ_v / n_v) else: λv_tot = 0 λ_tot = λ_C1 + λ_C2 + λ_C3 + λ_C4 + β_tot + λv_tot #case if virus is erradicated if λ_tot == 0: break λ_rand = np.random.exponential(1 / λ_tot) count += λ_rand U = np.random.uniform() ###CASE 1: A susceptible person from C1 is infected if U < λ_C1 / λ_tot: infect_entity(sim_state, 'C1') ###CASE 2: A susceptible person from C2 is infected elif U < (λ_C1 + λ_C2) / λ_tot: infect_entity(sim_state, 'C2') ###CASE 3: A susceptible person from C3 is infected elif U < (λ_C1 + λ_C2 + λ_C3) / λ_tot: infect_entity(sim_state, 'C3') ###CASE 4: A susceptible person from C4 is infected elif U < (λ_C1 + λ_C2 + λ_C3 + λ_C4) / λ_tot: infect_entity(sim_state, 'C4') ###Case 5: An entity (or batch) is vaccinated elif U < (λ_C1 + λ_C2 + λ_C3 + λ_C4 + λv_tot) / λ_tot: #all people available for vaccination n = min(n_v, len(sim_state.V_tot)) for i in range(n): i = np.random.randint(0, len(sim_state.V_tot)) ent = sim_state.V_tot.pop(i) sim_state.V[ent.C].remove(ent) if ent in sim_state.S[ent.C]: sim_state.S[ent.C].remove(ent) ent.state = "R_v" else: sim_state.I[ent.C].remove(ent) sim_state.infected.remove(ent) ent.state = 'R' sim_state.R.append(ent) ###Case 6: A person recovers else: i = np.random.randint(0, len(sim_state.infected)) ent = sim_state.infected.pop(i) sim_state.R.append(ent) #if the recovered person is from assymptomatic if ent.state == 'I_a': sim_state.I[ent.C].remove(ent) sim_state.V[ent.C].remove(ent) sim_state.V_tot.remove(ent) #if the recovered person is symptomatic else: sim_state.I_sym.remove(ent) ent.state = 'R' #recording data sim_state.times.append(count) sim_state.num_inf.append(len(sim_state.infected)) #counting infected showing symptoms for all classes C1 = sim_state.n['C1'] - (len(sim_state.S['C1'])) C2 = sim_state.n['C2'] - (len(sim_state.S['C2'])) C3 = sim_state.n['C3'] - (len(sim_state.S['C3'])) C4 = sim_state.n['C4'] - (len(sim_state.S['C4'])) for ent in sim_state.R: if ent.C == 'C1': C1 -= 1 elif ent.C == 'C2': C2 -= 1 elif ent.C == 'C3': C3 -= 1 else: C4 -= 1 sim_state.C1_inf.append(C1) sim_state.C2_inf.append(C2) sim_state.C3_inf.append(C3) sim_state.C4_inf.append(C4) return sim_state
def com(A, B, psi): ABpsi = State() # operate with A on all components in B|psi> for amp, cpt in B(psi): ABpsi += amp * A(cpt) return ABpsi
def I2m(psi): amp = sqrt((psi.I2 + psi.m2) * (psi.I2 - psi.m2 + 1)) ket = UncoupledBasisState(psi.J, psi.mJ, psi.I1, psi.m1, psi.I2, psi.m2 - 1) return State([(amp, ket)])
def I1m(psi): amp = sqrt((psi.I1 + psi.m1) * (psi.I1 - psi.m1 + 1)) ket = UncoupledBasisState(psi.J, psi.mJ, psi.I1, psi.m1 - 1, psi.I2, psi.m2) return State([(amp, ket)])
def getStateChildren(self,state): children = [] node_has_stack = state.stacks.get(state.location) #Handle actions load and unload if node_has_stack != None: #print(node.stack) #copy stack for next branch level stacks_copy = copy.deepcopy(state.stacks) stack_at_current_node = stacks_copy[state.location] #Check for casks in stack, and if load is possible if stack_at_current_node.stackSpaceOccupied() > 0 and not state.loaded: print("casks before cask pop") for c in stacks_copy[state.location].stored_casks: print("cask name", c.c_id) cask = stack_at_current_node.removeCaskFromStack() cost_of_action = cask.weight + 1 new_total_cost = (state.total_cost + cost_of_action) stacks_copy[state.location] = stack_at_current_node print("casks after cask pop") #for c in stacks_copy[state.location].stored_casks: # print("cask name", c.c_id) cask_list = copy.deepcopy(state.cask_list) cask_list.append(cask.c_id) temp_state = State(state.location,True,cost_of_action,new_total_cost,state,"load", cask, stacks_copy,cask_list, {}, state.mission + 1) #temp_state.mission.mission += 1 print("mission number", temp_state.mission, temp_state.casks_handled) if not self.stateExplored(temp_state): temp_state.casks_handled[cask.c_id] = True children.append(temp_state) print("mission number", temp_state.mission, temp_state.casks_handled) #If CTS has cask unload if available space in stack elif state.loaded: if stack_at_current_node.stackSpaceFree() >= state.cask.length: cost_of_action = state.cask.weight + 1 new_total_cost = (state.total_cost + cost_of_action) stack_at_current_node.addCaskToStack(state.cask) stacks_copy[state.location] = stack_at_current_node temp_state = classes.State(state.location,False,cost_of_action,new_total_cost,state,"unload", None, stacks_copy, state.cask_list,{}, state.mission + 1) #temp_state.mission.mission += 1 if not self.stateExplored(temp_state): children.append(temp_state) #Handle action move node_num = self.node_name_to_num.get(state.location) if node_num == None: print("trying to access undefined Node_num aborting program") os.exit(0) for col_num in range(0,self.num_nodes): if self.adj_matrix[node_num][col_num] < inf: location = self.node_num_to_name.get(col_num) if location == None: print("tried to access unexisting node") os.exit(0) loaded = state.loaded #if loaded in previous state, it is still loaded after a move new_total_cost = state.total_cost # add previous action costs to total cost cost_of_action = 0 #calculate cost of move if(loaded): cost_of_action = (state.cask.weight + 1)*self.adj_matrix[node_num][col_num] new_total_cost += cost_of_action else: cost_of_action = self.adj_matrix[node_num][col_num] new_total_cost += cost_of_action #create new state, check if it is not already explored temp_state = State(location,loaded,cost_of_action, new_total_cost, state,"move", state.cask, state.stacks,state.cask_list,state.casks_handled, state.mission) if not self.stateExplored(temp_state): children.append(temp_state) #print("el in children", len(children)) for el in children: print("child ", el.location,el.total_cost,el.action,el.cost_of_action) return children
def I2z(psi): return State([(psi.m2, psi)])
def I1z(psi): return State([(psi.m1, psi)])
def Jz(psi): return State([(psi.mJ, psi)])
def J2(psi): return State([(psi.J * (psi.J + 1), psi)])
from classes import State if __name__ == '__main__': print 'N-Puzzle Solver!' print 10 * '-' state = State(3) print 'The Starting State is:' start = state.start_state(5) state.print_st(start) print 'The Goal State should be:' state.print_st(state.goal) print 'Here it Goes:' state.print_st(start) state.solve_it(start)
def get_initial_state(self): dealer_first_card = self.draw_black_card() player_first_card = self.draw_black_card() return State(dealer_first_card.value, player_first_card.value)
def Jm(psi): amp = sqrt((psi.J + psi.mJ) * (psi.J - psi.mJ + 1)) ket = UncoupledBasisState(psi.J, psi.mJ - 1, psi.I1, psi.m1, psi.I2, psi.m2) return State([(amp, ket)])
while not puzzle_size.isdigit() or not 1 <= int(puzzle_size) <= 100: print("wrong input. please enter a number between 1 and 100") puzzle_size = input( "please enter the n size of an n x n puzzle:\n") shuffles_amount = input( "how many times should the puzzle be shuffled?\n") while not shuffles_amount.isdigit() or int(shuffles_amount) < 1: print("wrong input. please enter a number above 0") shuffles_amount = input( "how many times should the puzzle be shuffled?\n") puzzle = Puzzle(int(puzzle_size)) if puzzle.size == 1: start = goal = State(puzzle.goal_array) print_solution(start, goal, 1, 0) puzzle.get_goal() if args.filepath: start = State(start) else: start = State(puzzle.goal_array) start.zero_tile = start.find_zero() start = puzzle.shuffle(start, int(shuffles_amount)) start.parent = 0 get_heuristics(start, puzzle, args) # we first check if the starting state is solvable if args.filepath and not start.can_be_solved(puzzle): print("can't be solved")
async def serve(): state = State() server = await asyncio.start_unix_server(make_handler(state), path=PATH) async with server: await server.serve_forever()