Esempio n. 1
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
Esempio n. 2
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]

        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
Esempio n. 3
0
 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
Esempio n. 4
0
def compute_and_set_ideal_state(state: State, sample_scooters: list):
    progressbar = Bar("| Computing ideal state",
                      max=len(os.listdir(TEST_DATA_DIRECTORY)))
    number_of_scooters_counter = np.zeros(
        (len(state.clusters), len(os.listdir(TEST_DATA_DIRECTORY))))
    for index, file_path in enumerate(sorted(os.listdir(TEST_DATA_DIRECTORY))):
        progressbar.next()
        current_snapshot = read_bounded_csv_file(
            f"{TEST_DATA_DIRECTORY}/{file_path}")
        current_snapshot = current_snapshot[current_snapshot["id"].isin(
            sample_scooters)]
        current_snapshot["cluster"] = [
            state.get_cluster_by_lat_lon(row["lat"], row["lon"]).id
            for index, row in current_snapshot.iterrows()
        ]
        for cluster in state.clusters:
            number_of_scooters_counter[cluster.id][index] = len(
                current_snapshot[current_snapshot["cluster"] == cluster.id])
    cluster_ideal_states = np.mean(number_of_scooters_counter, axis=1)
    normalized_cluster_ideal_states = normalize_to_integers(
        cluster_ideal_states, sum_to=len(sample_scooters))

    for cluster in state.clusters:
        cluster.ideal_state = normalized_cluster_ideal_states[cluster.id]

    # setting number of scooters to ideal state
    state_rebalanced_ideal_state = idealize_state(state)

    # adjusting ideal state by average cluster in- and outflow
    simulate_state_outcomes(state_rebalanced_ideal_state, state)

    progressbar.finish()
Esempio n. 5
0
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)
Esempio n. 6
0
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
Esempio n. 7
0
 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
Esempio n. 8
0
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
Esempio n. 9
0
 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!')
Esempio n. 10
0
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()
Esempio n. 11
0
def init(connection):

    """
    Sets up the necessary state information to be able to use the
    read/write procedures. Returns the state object the other methods
    expect

    """

    state = State()
    state.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    state.sock.connect((connection.host, connection.port))

    state.sock.settimeout(1)

    if connection.ssl:
        state.sock = wrap_socket(state.sock)

    state.ssl = connection.ssl
    state.buffer = b''

    return state
Esempio n. 12
0
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
Esempio n. 13
0
def step_time(state: State,
              time: int,
              use_srq: bool,
              infinite: bool = False) -> State:
    """Perform a single timestep, generating arrivals and loading a boat.
    
    Returns the new state (modified in place, not a copy).
    use_srq should be the same value as passed to the constructor of state.
    """
    # Generate the groups
    if not infinite:
        arrivals = generate_groups(time, const.GROUP_SIZE_DISTRIBUTION)
    else:
        arrivals = generate_n_groups(
            time, const.GROUP_SIZE_DISTRIBUTION,
            const.BOAT_CAPACITY + const.MAX_LINE_SKIP - len(state.line) - 1)

    # Sort groups into SRQ, if necessary
    if use_srq:
        for group in arrivals:
            if group.size == 1:
                state.srq.append(group)
            else:
                state.line.append(group)
    else:
        state.line.extend(arrivals)

    # Load the boat and keep track of departed groups
    remaining = const.BOAT_CAPACITY
    departed = []
    while len(state.line) > 0:
        options = [
            index
            for index, option in enumerate(state.line[0:const.MAX_LINE_SKIP])
            if option.size <= remaining
        ]
        if len(options) > 0:
            departed.append(state.line.pop(options[0]))
            remaining -= departed[-1].size
        else:
            break

    if use_srq:
        while len(state.srq) > 0 and remaining > 0:
            departed.append(state.srq.pop(0))
            remaining -= 1

    state.departed_groups = departed
    return state
Esempio n. 14
0
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())
Esempio n. 15
0
 def test_to_from_lists(self):
     d = dict(enumerate([[1, 2, 3], [4, 5, 6], [7, 8, 9]]))
     self.assertEqual(d, dict(State.from_dict(d)))
Esempio n. 16
0
 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)
Esempio n. 17
0
class Bot:
    requests = [] #Keeping track of all requests in requests
    ntm, ntt, trash = get_theatres() # should not be changed after instantiation
    '''
    bot is tied to a unique user conversation
    resource is unique conversation identifier. for a customer, it should be their number
    we kill the bot when the conversation closes

    '''
    def __init__(self, resource='test'):
        self.state = State(resource)

    '''
    handle potential crashing
    '''
    def sleek_get_response(self, message):
        try:
            response = self.get_response(message)
        except:
            response = "Chappie can't quite understand you!"
        return response
            
    '''
    the bot thinks of what to say...

    handles movie-related String input from the user
    function takes in incoming lines from user then sends it to the relevant submodules.
    current state of the Bot is kept track of in State object
    returns new state of bot and the bot response

     Submodules:
    - function tokeniser returns spellchecked lists of tokens
    - multiple tagging functions tag tokens for times, numbers, movie titles, addresses
    - tagged tokens are sent to logic, to be checked for mutual compatibility and then inserted
    into the MovieRequest object
    - logic output is given to function narrow, to decide which questions to ask

    Int question keeps track of which question we are on. -1 for no question, index of
    MovieRequest.done for relevant question
    '''
    def get_response(self, message):
        req, question, options = self.state.req, self.state.question, self.state.options

        #closing
        if message.lower() == "bye":
            return "Goodbye!"

        # send input to tokenizer
        tokens = tokeniser(message)[1]

        if len(tokens) < 1: return "..?"

        #  track of current conversation-create and update
        # Conversation object
        self.state.add_line(tokens[0])

        # understand the prepositions to better find where the info is
        # todo submodule, for now check everything, which works pretty well tbh
        # at [theatre], watch|see [movie], at [time]

        tags = get_tags(tokens, Bot.ntm, Bot.ntt, question)

        # logic for what to do if there is more than one of the above,
        # must narrow it down
        # input items into the MovieRequest object based on the current
        # state of the tags
        # returns the new question that it needs to know to finish the request
        # returns statement, the question itself
        question, statement = narrow(self.state, tags, Bot.ntm, Bot.ntt)

        # if we are still on the same question, add to the counter
        # works because question is an immutable Int
        if self.state.question == question:
            self.state.timeout += 1
        else: self.state.timeout = 0
        self.state.question = question

        self.state.starting = False
        return statement

    '''
    run() function

    used for debugging, can interact on console. user should always be able to keep typing,
    and Bot will keep re-updating the submodules.

    two threads:
    1. for constantly updating raw input from the user and writing to a buffer
    2. for popping items from the buffer and processing in order
    thread 1 signals to thread 2 with a new_text Event every time a new text is sent

    keeps track of and constantly updates State.
    '''

    def run(self):
        chat_buffer = deque()

        # accept input at all times
        # open separate thread which writes it to a buffer
        new_text = threading.Event()
        end_buffer = threading.Event()

        def add_to_buffer():
            while True:
                inp = raw_input()
                chat_buffer.appendleft(inp)
                new_text.set()
                if inp.__eq__('bye'):
                    return
                if end_buffer.is_set():
                    return

        buffer_thread = threading.Thread(name='buffer_thread', target=add_to_buffer)
        buffer_thread.start()

        def close():
            print(self.state.req.readout())
            Bot.requests.append(self.state.req)
            end_buffer.set()
            return

        # main thread
        # while buffer_thread has items in it, pop off items and process
        while True:
            new_text.wait(timeout=20) # timeout wait doesn't seem to do anything todo
            try:
                inp = chat_buffer.pop()
                new_text.clear()
            except IndexError:
                continue

            if inp.lower() == ('bye'):
                print("Goodbye!")
                close()
                return

            response = self.get_response(inp)

            if self.state.question == -1:
                print("Got it, thanks. Bye!")
                close()
                return
            else:
                # ask a question to find out later information
                print(response)
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 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 I2z(psi):
    return State([(psi.m2, psi)])
def Jz(psi):
    return State([(psi.mJ, psi)])
Esempio n. 22
0
 def test_to_from_empty(self):
     d = {}
     self.assertEqual(d, dict(State.from_dict(d)))
Esempio n. 23
0
 def test_access(self):
     s = State()
     s.field = 3
     self.assertEqual(s.field, 3)
Esempio n. 24
0
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
Esempio n. 25
0
def load_settings():
    with open('settings.json', 'r') as conf:
        return State.from_dict(json.load(conf))
Esempio n. 26
0
 def test_to_from_singleton(self):
     d = {'a': 1}
     self.assertEqual(d, dict(State.from_dict(d)))
Esempio n. 27
0
 def test_to_from_nested(self):
     d = {'a': {'b': -1784}, 'c': {'d': 51397, 'e': {'f': 42}}}
     self.assertEqual(d, dict(State.from_dict(d)))
Esempio n. 28
0
 def test_nested_access(self):
     s = State()
     s.field = State()
     s.field.subfield = 3
     self.assertEqual(s.field.subfield, 3)
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 J2(psi):
    return State([(psi.J * (psi.J + 1), psi)])
Esempio n. 31
0
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 I1z(psi):
    return State([(psi.m1, psi)])
Esempio n. 33
0
	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 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)])
Esempio n. 35
0
async def serve():
    state = State()
    server = await asyncio.start_unix_server(make_handler(state), path=PATH)

    async with server:
        await server.serve_forever()
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)])
Esempio n. 37
0
 def test_contains(self):
     s = State()
     s.field = 3
     self.assertTrue('field' in s)
Esempio n. 38
0
 def __init__(self, resource='test'):
     self.state = State(resource)
Esempio n. 39
0
 def test_nonzero(self):
     s = State()
     s.field = 3
     self.assertTrue(s)
Esempio n. 40
0
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
Esempio n. 41
0
        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")
Esempio n. 42
0
 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
Esempio n. 43
0
 def test_to_from_many(self):
     d = dict(enumerate('hello world what a beautiful day'.split()))
     self.assertEqual(d, dict(State.from_dict(d)))