Пример #1
0
def test_transition_can_fire():
    t = Transition([], [])
    assert t.can_fire(State([0])) == True
    t1 = Transition([], [0])
    assert t1.can_fire(State([6])) == True
    t2 = Transition([0], [])
    assert t2.can_fire(State([1])) == True
    t3 = Transition([0],[])
    assert t3.can_fire(State([0])) == False
    t4 = Transition([0, 1], [])
    assert t4.can_fire(State([320, 0])) == False
Пример #2
0
def get_data(file_name):
	wrd_voc, wrd_list, pos_voc, pos_list, depen_voc, depen_list = read_data(file_name)
	sentences = getTree(file_name)

	#skip the empty catagories
	# input data:
	#		top 2 words at buffer & stack; leftmost and rightmost child of the top 2 words at stack
	#		wordIndex ===> wordVec, posIndex ==>posVec, arc_labels ==> arcLabels
	cnt_case = 1
	trn_case_name = file_name + '_case'
	#trn_label_name = '../data/trn_label'
	f_case = codecs.open(trn_case_name, 'w', 'utf-8')
	#f_label = codecs.open(trn_label_name, 'w', 'utf-8')


	for i in range(0, len(sentences)):
		sentence = sentences[i]
		words = sentence['words']
		poses = sentence['poses']
		indexs = sentence['indexs']
		arc_labels = sentence['arc_labels']
		trans = Transition(sentence)
		while not trans.finish():
			index_i = trans.stack[-1]
			index_j = trans.buffer[0]
			#print index_i
			if index_i == -1:
				if indexs[index_j] == index_i:
					if legal_right_arc(index_j, indexs, trans.buffer):
						save_case(f_case, words, poses, 'R_' + arc_labels[index_j], trans.buffer, trans.stack)
						trans.right_arc(arc_labels[index_j])
						#if len(trans.stack) != 0: print len(trans.stack)
						if len(trans.buffer) != 1: print len(trans.buffer)
						break# finish parsing
				else:
					save_case(f_case, words, poses, 'SHIFT\n', trans.buffer, trans.stack) 
					trans.shift()
					continue
			# we must confirm there are no any arc belong to the buffer header
			if indexs[index_j] == index_i:
				if legal_right_arc(index_j, indexs, trans.buffer):
					save_case(f_case, words, poses, 'R_' + arc_labels[index_j], trans.buffer, trans.stack)
					trans.right_arc(arc_labels[index_j])
				else:
					save_case(f_case, words, poses, 'SHIFT\n', trans.buffer, trans.stack) 
					trans.shift()
				continue
			# all the left arc is OK~
			if indexs[index_i] == index_j:
				save_case(f_case, words, poses, 'L_' + arc_labels[index_i], trans.buffer, trans.stack)
				trans.left_arc(arc_labels[index_i])
				continue
			save_case(f_case, words, poses, 'SHIFT\n', trans.buffer, trans.stack) 
			trans.shift()

	f_case.close()
Пример #3
0
 def addNewTransition(self, q_from, q_to, symbol, timeguard):
     t = Transition(q_from, q_to, symbol, timeguard)
     self.DELTA.append(t)
Пример #4
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
s0 = State(0, True, False)
s1 = State(1, False, False)
s2 = State(2, False, True)
t1 = Transition(s0, 'a', s0)
t2 = Transition(s0, 'b', s1)
t3 = Transition(s1, 'a', s2)
t4 = Transition(s1, 'b', s2)
t5 = Transition(s2, 'a', s0)
t6 = Transition(s2, 'b', s1)
L = [t1, t2, t3, t4, t5, t6]
#auto=Automate(L)

print(" -auto est l'automate suivant : ")
autoB = Automate.creationAutomate("tripleA.txt")
autoA = Automate.creationAutomate("auto.txt")
#autoA.show('autoA')
#autoB.show('autoB')

#print(autoA)
Пример #5
0
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)
print "t=t2? " + str(t == t2)
Пример #6
0
    num_episodes = 25_000
    save_every = 50

    rewards = []
    for episode in range(1, num_episodes + 1):
        state = env.reset()
        done = False

        start_time = time.time()
        ep_rewards = []
        while not done:
            action = agent.get_action(state, False)
            new_state, reward, done, _ = env.step(action)
            #env.render()

            agent.update_mem(Transition(state, action, new_state, reward,
                                        done))
            agent.train()

            state = new_state

            ep_rewards.append(reward)

        rewards.append(np.sum(ep_rewards))
        print(
            f'ep: {episode}, time: {round(time.time() - start_time, 3)}s, ep_reward: {round(np.sum(ep_rewards),2)}, avg_reward: {round(np.mean(rewards[-25:]),2)} epsilon: {round(agent.epsilon,2)}'
        )

        if episode % save_every == 0:
            torch.save(
                agent.model.state_dict(),
                f'models/episode={episode} avg_reward={round(np.mean(rewards[episode-save_every:]),2)}'
Пример #7
0
def fire_alarm():
    t = Transition(transition_step_time)
    t.start()

    time.sleep(post_alarm_wait)
Пример #8
0
def test_multi_in_out():
    t = Transition([0, 1, 2, 3], [4, 5, 6])
    state = State([1, 2, 3, 4, 0, 1, 2])
    s2 = t.fire(state)
    assert s2.tokens == [0, 1, 2, 3, 1, 2, 3]
Пример #9
0
import sys
sys.path.append('../')
from replay import Replay
from transition import Transition
import numpy as np


def f(arr):
    return np.array(arr)


replay = Replay(2, 2)

t1 = Transition(f([]), 1, 2, f([]))
t11 = Transition(f([]), 1, 2, f([]))

t2 = Transition(f([1, 2, 3]), 4, 2, f([77, 2, 1]))
t22 = Transition(f([1, 2, 3]), 4, 2, f([77, 2, 1]))

t3 = Transition(f([1, 2, 1]), 4, 2, f([77, 5, 1]))
t33 = Transition(f([1, 2, 1]), 4, 2, f([77, 5, 1]))

replay.add_transition(t1)
replay.add_transition(t2)
replay.add_transition(t3)
replay.add_transition(t11)
replay.add_transition(t22)
replay.add_transition(t33)

print("Replay: " + str(replay))
print("Transition: " + str(t22))
from transition import Transition
from side import Side
from data.vars import *
from typing import Dict

cipher_name = 'misty2'  # type: str
systems = dict()  # type: Dict[int, System]

systems[2] = System(inputs=[a1, a2, a3, a4],
                    outputs=[c1, c2, c3, c4],
                    transitions=[
                        Transition(Side(a1), Side(c3, cp_with_lo(a2, mu)), F),
                        Transition(Side(a3), Side(c1, cp_with_lo(a4, mu)), F),
                        Transition(Side(a2), Side(c4, cp_with_lo(c1, lmbda)),
                                   G),
                        Transition(Side(a4), Side(c2, cp_with_lo(c3, lmbda)),
                                   G)
                    ])

systems[3] = System(inputs=[a1, a2, a3, a4],
                    outputs=[c1, c2, c3, c4],
                    transitions=[
                        Transition(Side(a1), Side(b2, cp_with_lo(a2, mu)), F),
                        Transition(Side(a3), Side(b1, cp_with_lo(a4, mu)), F),
                        Transition(Side(a2), Side(c3, cp_with_lo(b1, lmbda)),
                                   G),
                        Transition(Side(a4), Side(c1, cp_with_lo(b2, lmbda)),
                                   G),
                        Transition(Side(b1), Side(c4, cp_with_lo(c1, mu)), F),
                        Transition(Side(b2), Side(c2, cp_with_lo(c3, mu)), F)
                    ])
Пример #11
0
from automate import Automate
from state import State
from transition import Transition
from parser import *

print("DEBUT PROGRAMME\n")

s1=State(1, True, True)
s2=State(2, False, False)
t1= Transition(s1,"a",s1)
t2=Transition(s1,"a",s2)
t4=Transition(s2,"a", s2)
t5=Transition(s2,"b",s2)
t6= Transition(s1,"a",s1)
t7= Transition(s2,"b",s1)
listeT1 = [t1,t2,t4,t5,t6,t7]
a=Automate(listStates=[], label="a", listTransitions=listeT1)
print(a)


s3=State(1,True,False)
s4=State(2,False,True)

tb1= Transition(s3,"b",s3)
tb2= Transition(s3,"a",s4)
tb3= Transition(s4,"b",s4)
tb4= Transition(s4,"c",s3)
tb5 = Transition(s3,"b",s4)
listeT2 = [tb1,tb2,tb3,tb4,tb5]
b=Automate(listStates=[s3,s4], label= "b", listTransitions = listeT2)
print(b)
Пример #12
0
def petri(elements):
  print(elements)
  between = ''
  list_places = []
  in_statement = False
  checklbrac = '['
  checkrbrac = ']'
  checktime = 'time'
  checkstar = '*'
  checknarrow = '|'

  checkless = '<'
  checkmore = '>'

  listoutinbeforenarrow = []
  listoutinafternarrow = []

  for i in elements:
    if checklbrac in i:
      in_statement = True
    if in_statement:
      between += i
    if checkrbrac in i:
      in_statement = False
      break
  between = between.replace('[','').replace(']','')
  for i in between:
    list_places.append(int(i))
  print(list_places)

  between = ''
  for i in elements:
    if checktime in i:
      in_statement = True
    if in_statement:
      between += i
    if checkstar in i:
      in_statement = False
      break
  between = between.replace('time','').replace('*','').replace('=', '')
  firings = int(between)
  print(firings)

  between = ''
  for i in elements:
    if checkstar in i:
      in_statement = True
    if in_statement:
      between += i
    if checknarrow in i:
      in_statement = False
      break
  between = between.replace('*','').replace('|','')
  for i in between:
    listoutinbeforenarrow.append(i)
  print(listoutinbeforenarrow)

  listoutinafternarrow = elements[13]
  #between = between.replace('|','').replace('*','')
  # for i in between:
  #   listoutinafternarrow.append(i)
  # print(listoutinafternarrow)

  ps = [Place(m) for m in list_places]

  countless = 0
  countmore = 0

  for i in listoutinbeforenarrow:
    if checkless in i:
      countless += 1
    if checkmore in i:
      countmore += 1
  print(countless, countmore)

  listout = []
  listin = []
  for i in range(countless):
    listout = [Out(ps[i])]
  for i in range(countmore):
    listin = [In(ps[i])]

  for i in range(1):
    ts = dict(i=Transition(listout, listin + [In(ps[1])]))
    # a = elements[0]
    # firings = elements[1]
    # coeff = [num for num in elements[2:] if isinstance(num, int)]
    # print('all coeff', coeff)

    # ps = [Place(m) for m in a]
    # ts = dict(
    #      t1=Transition(
    #          [Out(ps[0])], 
    #          [In(ps[0]), In(ps[1])]
    #     )
        # t2=Transition(
        #     [Out(ps[1])],
        #     [In(ps[2]), In(ps[0])])
    #     ,)
    
    firing_sequence = [choice(list(ts.keys())) for _ in range(firings)] # stochastic execution
    print(firing_sequence)
    petri_net = PetriNet(ts)
    petri_net.run(firing_sequence, ps)
Пример #13
0
    def test_hash(self, s1, s2, inp, out, cnt, s12, s22, inp2, out2, cnt2,
                  equal):
        t = Transition(s1, s2, inp, out, cnt)
        t2 = Transition(s12, s22, inp2, out2, cnt2)

        assert (hash(t) == hash(t2)) == equal
Пример #14
0
    def test_ne(self, s1, s2, inp, out, cnt, s12, s22, inp2, out2, cnt2):
        t = Transition(s1, s2, inp, out, cnt)
        t2 = Transition(s12, s22, inp2, out2, cnt2)

        assert t != t2
Пример #15
0
def test_no_output_transition():
    t = Transition([0], [])
    state = State([6])
    s2 = t.fire(state)
    assert s2.tokens == [5]
Пример #16
0
def test_in_and_out_transition():
    t = Transition([0], [1])
    state = State([6, 5])
    s2 = t.fire(state)
    assert s2.tokens == [5, 6]
Пример #17
0
    def tick(self):
        # print("tick: ", self.name)
        self.__current_time = ticks_us()
        elapsed_time = self.elapsed_time

        # Check is the duration is up
        if elapsed_time >= self.duration:
            cur_angle = self.target_angle
            self.angle(angle_value=cur_angle, channel=self.channel)
            return True
        cur_angle = self.__current_angle
        valid_transition = False
        # print("transition type is: ", self.__transition)
        if self.__transition == 'linear_tween':
            cur_angle = Transition().linear_tween(
                current_time=self.elapsed_time,
                start_value=self.start_angle,
                change_in_value=self.change_in_value,
                duration=self.duration)
            valid_transition = True
        if self.__transition == 'ease_in_circ':
            cur_angle = Transition().ease_in_circ(
                current_time=self.elapsed_time,
                start_value=self.start_angle,
                change_in_value=self.change_in_value,
                duration=self.duration)
            valid_transition = True
        if self.__transition == 'ease_in_cubic':
            cur_angle = Transition().ease_in_cubic(
                current_time=self.elapsed_time,
                start_value=self.start_angle,
                change_in_value=self.change_in_value,
                duration=self.duration)
            valid_transition = True
        if self.__transition == 'ease_in_quad':
            cur_angle = Transition().ease_in_quad(
                current_time=self.elapsed_time,
                start_value=self.start_angle,
                change_in_value=self.change_in_value,
                duration=self.duration)
            valid_transition = True
        if self.__transition == 'ease_in_quart':
            cur_angle = Transition().ease_in_quart(
                current_time=self.elapsed_time,
                start_value=self.start_angle,
                change_in_value=self.change_in_value,
                duration=self.duration)
            valid_transition = True
        if self.__transition == 'ease_in_expo':
            cur_angle = Transition().ease_in_expo(
                current_time=self.elapsed_time,
                start_value=self.start_angle,
                change_in_value=self.change_in_value,
                duration=self.duration)
            valid_transition = True
        if self.__transition == 'ease_in_quart':
            cur_angle = Transition().ease_in_quart(
                current_time=self.elapsed_time,
                start_value=self.start_angle,
                change_in_value=self.change_in_value,
                duration=self.duration)
            valid_transition = True
        if self.__transition == 'ease_in_quint':
            cur_angle = Transition().ease_in_quint(
                current_time=self.elapsed_time,
                start_value=self.start_angle,
                change_in_value=self.change_in_value,
                duration=self.duration)
            valid_transition = True
        if self.__transition == 'ease_in_sine':
            cur_angle = Transition().ease_in_sine(
                current_time=self.elapsed_time,
                start_value=self.start_angle,
                change_in_value=self.change_in_value,
                duration=self.duration,
                start_time=self.__tick_start_time,
                target_angle=self.target_angle)
            valid_transition = True
        if not valid_transition:
            print("error - No Valid Transition provided")

        cur_angle = int(cur_angle)
        self.angle(angle_value=cur_angle, channel=self.channel)

        if self.current_angle == self.target_angle:
            return True
        else:
            return False
Пример #18
0
def test_no_input_transition():
    t = Transition([], [0])
    state = State([4])
    s2 = t.fire(state)
    assert s2.tokens == [5]
Пример #19
0
 def create_Transition(self, condition):
     self.transition = Transition(self, condition, self.next)
Пример #20
0
 def test_transition_init_error(self, s1, s2, inp, out, cnt):
     with pytest.raises(ValueError):
         t = Transition(s1, s2, inp, out, cnt)
Пример #21
0
import os
import copy
import sp
from sp import *
from parser import Parser
from itertools import product
from automateBase import AutomateBase
from automate import Automate

#2.1 Creation d'automates
#1.
s0 = State(0, True, False)
s1 = State(1, False, False)
s2 = State(2, False, True)

t1 = Transition(s0, "a", s0)
t2 = Transition(s0, "b", s1)
t3 = Transition(s1, "a", s2)
t4 = Transition(s1, "b", s2)
t5 = Transition(s2, "a", s0)
t6 = Transition(s2, "b", s0)

autom = Automate([t1, t2, t3, t4, t5, t6])
#print(autom)
#autom.show("A_ListeTrans")

#2.
auto1 = Automate([t1, t2, t3, t4, t5, t6], [s0, s1, s2])
#print(auto1)
#auto1.show("A_ListeTrans")
Пример #22
0
 def _write_header(self):
     print(','.join(Transition.csv_headers()), file=self.file)
Пример #23
0

if __name__ == "__main__":
    cnn = CNN()
    stg = Strategy(cnn)
    megaman = Agent(stg)  # player 1
    megaman.get_exploration_factor = constant_function(EXPLORATION)
    megaman.train()

    performance = []
    print("Training ... ")
    for t in itertools.count():
        megaman.train()
        # p1, p2 = play_many([megaman, "negamax"], num_games=GAMES_PER_EPISODE)
        p1, p2 = play_many(["negamax", megaman], num_games=GAMES_PER_EPISODE)
        transitions = Transition(*zip(*p2))
        num_wins = transitions.reward.count(1)
        win_ratio = num_wins / GAMES_PER_EPISODE
        print(f"WR: {win_ratio:>6.2%}")
        performance.append(win_ratio)
        megaman.learn(transitions, player_id=2)
        megaman.get_exploration_factor = constant_function(megaman.get_exploration_factor() * DECAY_FACTOR)
    
        torch.save(megaman.strategy.model.state_dict(), PATH_RESULTS / "megaman2.pt")
        with open(PATH_RESULTS / "megaman2_vs_negamax.json", "w") as f:
            json.dump(performance, f)

        if (win_ratio >= 0.95):
            break
    
# Set up our FSM with the required info:
# Set of states
states = ['Q=0', 'Q=1', 'Error']
# Set of allowed inputs
# These represent possible permutations of S and R
alphabet = ['00', '01', '10', '11']
# Set of accepted states
accepting_states = ['Q=0', 'Q=1']
# The initial state
initial_state = 'Q=0'
fsm = FSM(states, alphabet, accepting_states, initial_state)

# Create the set of transitions
# S = 0: set, R = 0: reset
transition1 = Transition('Q=0', '00', 'Error')
transition2 = Transition('Q=0', '01', 'Q=1')
transition3 = Transition('Q=0', '10', 'Q=0')
transition4 = Transition('Q=0', '11', 'Q=0')
transition5 = Transition('Q=1', '00', 'Error')
transition6 = Transition('Q=1', '01', 'Q=1')
transition7 = Transition('Q=1', '10', 'Q=0')
transition8 = Transition('Q=1', '11', 'Q=1')
transition9 = Transition('Error', '00', 'Error')
transition10 = Transition('Error', '01', 'Error')
transition11 = Transition('Error', '10', 'Error')
transition12 = Transition('Error', '11', 'Error')
transitions = [
    transition1, transition2, transition3, transition4, transition5,
    transition5, transition6, transition7, transition8, transition9,
    transition10, transition11, transition12
Пример #25
0
def play_once(agents):
    env = make("connectx")
    env.reset()
    env.run(agents)
    return Transition.parse_steps(env.steps)
Пример #26
0
    def __evaluate_event_mrai(self, mrai_event_time: float, mrai_id: int,
                              data_frame: pd.DataFrame):
        # Events before the MRAI timeout
        events_before = data_frame[data_frame.time < mrai_event_time]
        previous_mrai = None
        if Events.MRAI in events_before.event.values:
            previous_mrai = events_before[events_before.event ==
                                          Events.MRAI].tail(1).time.values[0]
        if previous_mrai is not None:
            events_before = events_before[(events_before.time > previous_mrai) &\
                                          (events_before.event != Events.MRAI) &\
                                          (events_before.event != Events.TX)]
        # Find events caused by the MRAI
        events_after = pd.DataFrame(columns=NodeAnalyzer.EVALUATION_COLUMNS)
        if mrai_id in data_frame.index:
            events_after = data_frame.loc[[mrai_id], :]

        # Keep a tmp variable with the state that can have been changed
        # thanks to this reception
        new_state = None

        # Keep a list of received and transmitted routes
        received_routes = []
        transmitted_routes = []
        rx_value = [] if self.rx_values_tmp is None else self.rx_values_tmp
        # rx_events = []

        memory_actual_state = self.actual_state.copy()

        if len(events_before.index) == 0:
            return

        # Check each row of the events caused by the reception
        for row_id, row_event, row_value in zip(
                events_before[NodeAnalyzer.EVALUATION_COLUMNS[0]],
                events_before[NodeAnalyzer.EVALUATION_COLUMNS[2]],
                events_before[NodeAnalyzer.EVALUATION_COLUMNS[5]]):
            # If the event is a change in the state, I update the local
            # variable that keeps the state
            if row_event == Events.RIB_CHANGE:
                if len(rx_value) > 0:
                    if new_state is None:
                        new_state = set()
                    else:
                        self.actual_state = new_state
                    new_state = NodeAnalyzer.__evaluate_rib_change(row_value)
                    rx_v = rx_value.pop(0)
                    # rx_events.pop(0)
                    new_state = self.__state_analyzer(new_state, rx_v)
            # If the event is a RX i update the corresponding sets
            if row_event == Events.RX:
                rx_value.append(row_value)
                # rx_events.append(row_id)
                elem = self.__evaluate_rx(row_value)
                if elem not in received_routes:
                    received_routes.append(elem)

        if len(rx_value) > 0:
            self.rx_values_tmp = rx_value
        else:
            self.rx_values_tmp = None

        new_state = set() if new_state is None else new_state
        self.actual_state = memory_actual_state
        self.__state_register(new_state)

        # Check each row of the events caused by the reception
        for row_event, row_value in zip(
                events_after[NodeAnalyzer.EVALUATION_COLUMNS[2]],
                events_after[NodeAnalyzer.EVALUATION_COLUMNS[5]]):
            # If the event is a TX i update the corresponding sets
            if row_event == Events.TX:
                elem = self.__evaluate_tx(row_value)
                if elem not in transmitted_routes:
                    transmitted_routes.append(elem)

        received_routes = None if len(received_routes) == 0 else \
                             sorted(received_routes)
        transmitted_routes = None if len(transmitted_routes) == 0 else \
                             sorted(transmitted_routes)

        # Create the new transition
        input_state = NodeAnalyzer.hash_state_set(self.actual_state)
        output_state = NodeAnalyzer.hash_state_set(new_state)
        transition = Transition(input_state, output_state, received_routes,
                                transmitted_routes)

        # Change the state
        self.actual_state = new_state
        # Check if the transition has already been known
        if hash(transition) not in self.transitions.index:
            self.transitions.loc[hash(transition)] = [
                transition.init_state, transition.output_state,
                transition.input, transition.output, transition.counter
            ]
        else:
            # The transition is already in the dictionary, increase the
            self.transitions.at[hash(transition),
                                NodeAnalyzer.TRANSITIONS_COLUMNS[5]] += 1
Пример #27
0
from transition import Transition, FSM

# Set up our FSM with the required info:
# Set of states
states = ['State 1', 'State 2', 'Error']
# Set of allowed inputs
alphabet = [1, 0]
# Set of accepted states
accepting_states = ['State 1']
# The initial state
initial_state = 'State 1'
fsm = FSM(states, alphabet, accepting_states, initial_state)

# Create the set of transitions
transition1 = Transition('State 1', 1, 'State 2')
transition2 = Transition('State 2', 0, 'State 1')
transition3 = Transition('State 1', 0, 'Error')
transition4 = Transition('State 2', 1, 'Error')
transition5 = Transition('Error', 1, 'Error')
transition6 = Transition('Error', 0, 'Error')
transitions = [
    transition1, transition2, transition3, transition4, transition5,
    transition6
]
# Verify and add them to the FSM
fsm.add_transitions(transitions)

# Now that our FSM is correctly set up, we can give it input to process
# Let's transition the FSM to a green light
should_be_accepted = fsm.accepts([1, 0, 1, 0])
print(should_be_accepted)
 def trigger_update(self, update):
     result = Transition.trigger_update(self, update)
Пример #29
0
def train(env, agent):
    replay_buffer = ReplayBuffer(REPLAY_BUFFER_SIZE)
    for cycle in range(EPOCH):
        epoch_reward = 0.0
        agent.update_target_net()  # target network update
        for episode in range(EPISODES):
            state = env.reset()
            goal, goal_program = env.sample_goal()
            env.set_goal(goal, goal_program)
            episode_reward = 0
            no_of_achieved_goals = 0
            current_instruction_steps = 0
            trajectory = []

            start = time.time()
            for step in range(STEPS):
                action = agent.get_action(state, goal)
                next_state, reward, done, _ = env.step(
                    action, record_achieved_goal=True)
                achieved_goals = env.get_achieved_goals()
                transition = Transition(state, action, goal, reward,
                                        next_state, achieved_goals, done)
                trajectory.append(transition)
                episode_reward += reward

                if reward == 1.0:
                    goal, goal_program = env.sample_goal()
                    env.set_goal(goal, goal_program)
                    no_of_achieved_goals += 1
                    current_instruction_steps = 0

                if done:
                    break

                if current_instruction_steps == 10:
                    break

                current_instruction_steps += 1
                state = next_state
            # end = time.time()
            # print("environment interaction secs: ", end - start)
            # start = end

            for step in range(len(trajectory)):
                replay_buffer.add(trajectory[step])
                for goal_prime in trajectory[step].satisfied_goals_t:
                    transition = Transition(trajectory[step].current_state,
                                            trajectory[step].action,
                                            goal_prime, 1.0,
                                            trajectory[step].next_state,
                                            trajectory[step].satisfied_goals_t,
                                            trajectory[step].done)
                    replay_buffer.add(transition)
                deltas = relabel_future_instructions(trajectory, step, 4, 0.9)
                for delta in deltas:
                    goal_prime, reward_prime = delta
                    transition = Transition(trajectory[step].current_state,
                                            trajectory[step].action,
                                            goal_prime, reward_prime,
                                            trajectory[step].next_state,
                                            trajectory[step].satisfied_goals_t,
                                            trajectory[step].done)
                    replay_buffer.add(transition)

            # end = time.time()
            # print("HIR secs: ", end - start)
            # start = end
            epoch_reward += episode_reward

            logging.error("[Episode] " + str(episode) + ": reward " +
                          str(episode_reward) + " no of achieved goals: " +
                          str(no_of_achieved_goals))

            agent.update(replay_buffer, BATCH_SIZE)

            # end = time.time()
            # print("update model secs: ", end - start)
            # start = end

        logging.error("[Epoch] " + str(cycle) + ": total reward " +
                      str(epoch_reward))
        agent.save_model()

        agent.epsilon *= 0.96
        if agent.epsilon < 0.1:
            agent.epsilon = 0.1
Пример #30
0
"Window 672 x 512"
windowSurface = pygame.display.set_mode((WIDTH,HEIGHT))
x = WIDTH / 2
y = HEIGHT / 2
transitioning = False
title = TitleScreen(windowSurface)
title.start() # Starts the title screen, which just returns with no value to
              # start the game
# new_position = (x,y)

player = Player((560,230), windowSurface)
playerGroup = pygame.sprite.RenderPlain(player)


HUB = HUB()
transition = Transition(windowSurface)

my_font = pygame.font.SysFont('Verdana', 15)
dialogbox = DialogBox((440, 51), (255, 255, 204), 
    (102, 0, 0), my_font)


level_maker = Levels()
"""
Level maker is used below to generate all the array of sprites of enemies, rocks, 
doors, and dialog    
"""
rocks_by_level = level_maker.createLevels_Rock()
doors_by_level = level_maker.createLevels_Door()
enemies_by_level = level_maker.createLevels_enemies(windowSurface)
enemies_by_level.append(pygame.sprite.RenderPlain(Wizderp(windowSurface)))
Пример #31
0
        done = False
        ep_rewards = []

        while not done:

            action = agent.get_action(state, train=(not args.vis))
            new_state, reward, done, _ = env.step(action)

            if args.vis:
                env.render()

            ep_rewards.append(reward)

            if not args.vis:
                agent.update_mem(
                    Transition(state, action, new_state, reward, done))
                agent.train()

            state = new_state

        sum_rewards.append(np.sum(ep_rewards))

        if ep % config_obj['lr_decay_interval'] == 0:
            lr *= config_obj['lr_decay_rate']
            agent.optimizer = torch.optim.Adam(agent.model.parameters(), lr=lr)

        if ep % chkpt_every == 0 and not args.vis:

            # save model checkpoint and log training progress

            # evaluate model performance (no random actions)
Пример #32
0
def test_nothing_transition():
    t = Transition([], [])
    state = State([6])
    s2 = t.fire(state)
    assert s2.tokens == [6]
Пример #33
0
def main():
    TimerUtility.start_timer('total_time')
    valid_modes = ["predict", "restart", "test", "calibrate"]

    Globals.mype = 0
    Globals.npes = 1
    packing = False
    restart_run = 0

    # Parse command line

    if len(sys.argv) != 3:
        __print_usage(sys.argv[0])
        sys.exit(1)

    if len(sys.argv) != 3 or sys.argv[1] not in valid_modes:
        __print_usage(sys.argv[0])
        sys.exit(1)

    Processing.set_processing_type(Globals.mode_enum[sys.argv[1]])

    if Processing.get_processing_type() == Globals.mode_enum['restart']:
        Processing.set_restart_flag(True)

    Scenario.init(sys.argv[2], Processing.get_restart_flag())

    try:

        log_it = Scenario.get_scen_value("logging")
        random_seed = Scenario.get_scen_value("random_seed")
        Random.set_seed(random_seed)

        landuse_class_info = Scenario.get_scen_value("landuse_class_info")
        LandClass.num_landclasses = len(landuse_class_info)
        # filling in the class array in Land_Class
        for i, landuse_class in enumerate(landuse_class_info):
            # num, class_id, name, idx, hexColor
            landuse_class_meta = LanduseMeta(landuse_class.grayscale,
                                             landuse_class.type,
                                             landuse_class.name, i,
                                             landuse_class.color[2:])
            LandClass.landuse_classes.append(landuse_class_meta)

        # Set up Coefficients
        if sys.argv[1] == 'restart':
            if log_it:
                print("Implement log here")

            diffusion, breed, spread, slope_resistance, road_gravity, random_seed, restart_run = \
                Input.read_restart_file(Scenario.get_scen_value("output_dir"))
            Processing.set_current_run(restart_run)

        else:
            Processing.set_current_run(0)

        Coeff.set_start_coeff(
            Scenario.get_scen_value("calibration_diffusion_start"),
            Scenario.get_scen_value("calibration_spread_start"),
            Scenario.get_scen_value("calibration_breed_start"),
            Scenario.get_scen_value("calibration_slope_start"),
            Scenario.get_scen_value("calibration_road_start"))
        Coeff.set_stop_coeff(
            Scenario.get_scen_value("calibration_diffusion_stop"),
            Scenario.get_scen_value("calibration_spread_stop"),
            Scenario.get_scen_value("calibration_breed_stop"),
            Scenario.get_scen_value("calibration_slope_stop"),
            Scenario.get_scen_value("calibration_road_stop"))
        Coeff.set_step_coeff(
            Scenario.get_scen_value("calibration_diffusion_step"),
            Scenario.get_scen_value("calibration_spread_step"),
            Scenario.get_scen_value("calibration_breed_step"),
            Scenario.get_scen_value("calibration_slope_step"),
            Scenario.get_scen_value("calibration_road_step"))
        Coeff.set_best_fit_coeff(
            Scenario.get_scen_value("prediction_diffusion_best_fit"),
            Scenario.get_scen_value("prediction_spread_best_fit"),
            Scenario.get_scen_value("prediction_breed_best_fit"),
            Scenario.get_scen_value("prediction_slope_best_fit"),
            Scenario.get_scen_value("prediction_road_best_fit"))

        # Initial IGrid
        IGrid.init(packing, Processing.get_processing_type())
        '''
        Skipped memory and logging stuff for now, don't know if I'll need it
        If there is a problem, I can go back and implement
        '''

        # Initialize Landuse
        if len(Scenario.get_scen_value("landuse_data_file")) > 0:
            LandClass.init()
            if Scenario.get_scen_value("log_landclass_summary"):
                if log_it:
                    # this is where we would log
                    Logger.log("Test log")

        # Initialize Colortables
        Color.init(IGrid.ncols)

        # Read and validate input
        IGrid.read_input_files(packing,
                               Scenario.get_scen_value("echo_image_files"),
                               Scenario.get_scen_value("output_dir"))
        IGrid.validate_grids(log_it)

        # Normalize Roads
        IGrid.normalize_roads()

        landuse_flag = len(Scenario.get_scen_value("landuse_data_file")) != 0
        IGrid.verify_inputs(log_it, landuse_flag)

        # Initialize PGRID Grids
        PGrid.init(IGrid.get_total_pixels())

        if log_it and Scenario.get_scen_value("log_colortables"):
            Color.log_colors()

        # Count the Number of Runs
        Processing.set_total_runs()
        Processing.set_last_monte(
            int(Scenario.get_scen_value("monte_carlo_iterations")) - 1)
        if log_it:
            if Processing.get_processing_type(
            ) == Globals.mode_enum["calibrate"]:
                Logger.log(
                    f"Total Number of Runs = {Processing.get_total_runs()}")

        # Compute Transition Matrix
        if len(Scenario.get_scen_value("landuse_data_file")) > 0:
            Transition.create_matrix()
            if log_it and Scenario.get_scen_value("log_transition_matrix"):
                Transition.log_transition()

        # Compute the Base Statistics against which the calibration will take place
        Stats.set_base_stats()
        if log_it and Scenario.get_scen_value("log_base_statistics"):
            Stats.log_base_stats()

        if log_it and Scenario.get_scen_value("log_debug"):
            IGrid.debug("main.py")

        Processing.set_num_runs_exec_this_cpu(0)
        if Processing.get_current_run() == 0 and Globals.mype == 0:
            output_dir = Scenario.get_scen_value("output_dir")
            if Processing.get_processing_type(
            ) != Globals.mode_enum["predict"]:
                filename = f"{output_dir}control_stats.log"
                Stats.create_control_file(filename)

            if Scenario.get_scen_value("write_std_dev_file"):
                filename = f"{output_dir}std_dev.log"
                Stats.create_stats_val_file(filename)

            if Scenario.get_scen_value("write_avg_file"):
                filename = f"{output_dir}avg.log"
                Stats.create_stats_val_file(filename)

        if Scenario.get_scen_value("write_coeff_file"):
            output_dir = Scenario.get_scen_value("output_dir")
            filename = f"{output_dir}coeff.log"
            Coeff.create_coeff_file(filename, True)

        if Processing.get_processing_type() == Globals.mode_enum["predict"]:
            # Prediction Runs
            Processing.set_stop_year(
                Scenario.get_scen_value("prediction_stop_date"))
            Coeff.set_current_coeff(Coeff.get_best_diffusion(),
                                    Coeff.get_best_spread(),
                                    Coeff.get_best_breed(),
                                    Coeff.get_best_slope_resistance(),
                                    Coeff.get_best_road_gravity())
            if Globals.mype == 0:
                Driver.driver()
                Processing.increment_num_runs_exec_this_cpu()

            # Timing stuff
            if log_it and int(Scenario.get_scen_value('log_timings')) > 1:
                TimerUtility.log_timers()

        else:
            # Calibration and Test Runs
            Processing.set_stop_year(
                IGrid.igrid.get_urban_year(IGrid.igrid.get_num_urban() - 1))

            output_dir = Scenario.get_scen_value('output_dir')
            d_start, d_step, d_stop = Coeff.get_start_step_stop_diffusion()
            for diffusion_coeff in range(d_start, d_stop + 1, d_step):
                b_start, b_step, b_stop = Coeff.get_start_step_stop_breed()
                for breed_coeff in range(b_start, b_stop + 1, b_step):
                    s_start, s_step, s_stop = Coeff.get_start_step_stop_spread(
                    )
                    for spread_coeff in range(s_start, s_stop + 1, s_step):
                        sr_start, sr_step, sr_stop = Coeff.get_start_step_stop_slope_resistance(
                        )
                        for slope_resist_coeff in range(
                                sr_start, sr_stop + 1, sr_step):
                            rg_start, rg_step, rg_stop = Coeff.get_start_step_stop_road_gravity(
                            )
                            for road_grav_coeff in range(
                                    rg_start, rg_stop + 1, rg_step):
                                filename = f"{output_dir}{UGMDefines.RESTART_FILE}{Globals.mype}"
                                Output.write_restart_data(
                                    filename, diffusion_coeff, breed_coeff,
                                    spread_coeff, slope_resist_coeff,
                                    road_grav_coeff,
                                    Scenario.get_scen_value('random_seed'),
                                    restart_run)

                                restart_run += 1
                                Coeff.set_current_coeff(
                                    diffusion_coeff, spread_coeff, breed_coeff,
                                    slope_resist_coeff, road_grav_coeff)
                                Driver.driver()
                                Processing.increment_num_runs_exec_this_cpu()
                                # Timing Logs
                                if log_it and int(
                                        Scenario.get_scen_value(
                                            'log_timings')) > 1:
                                    TimerUtility.log_timers()

                                Processing.increment_current_run()

                                if Processing.get_processing_type(
                                ) == Globals.mode_enum['test']:
                                    TimerUtility.stop_timer('total_time')
                                    if log_it and int(
                                            Scenario.get_scen_value(
                                                'log_timings')) > 0:
                                        TimerUtility.log_timers()
                                    Logger.close()
                                    sys.exit(0)

        # Stop timer
        TimerUtility.stop_timer('total_time')
        if log_it and int(Scenario.get_scen_value('log_timings')) > 0:
            TimerUtility.log_timers()
        # Close Logger
        Logger.close()

    except KeyError as err:
        traceback.print_exc()
        print("{0} is not set. Please set it in your scenario file".format(
            str(err).upper()))
        Logger.log("Something went wrong")
        Logger.close()
        sys.exit(1)
    except FileNotFoundError as err:
        traceback.print_exc()
        print(err)
        Logger.log("Something went wrong")
        Logger.close()
        sys.exit(1)
    except Exception:
        traceback.print_exc()
        Logger.log("Something went wrong")
        Logger.close()
        sys.exit(1)
Пример #34
0
def test_loop():
    t = Transition([0], [0])
    state = State([6])
    s2 = t.fire(state)
    assert s2.tokens == [6]
Пример #35
0

def decisionIsIncorrect(state: State, hole):
    if state.stones[state.turn - 1][hole - 1] == 0:
        return True
    if hole <= 0 or hole > numberOfHoles:
        return True
    return False


if __name__ == '__main__':
    currentState = getInitialState()
    while currentState.isFinal() == False:
        displayState(currentState)
        if currentState.turn == turnHuman:
            decision = int(input('Enter your decision: '))
        else:
            decision = getBestAIDecision(currentState)
            print(f'AI took decision {decision}')
        if decisionIsIncorrect(currentState, decision):
            print('Decision is incorrect, hole is empty!')
            continue
        transition = Transition(decision)
        currentState = makeTransition(currentState, transition)
        print('')

    print('Final state:')
    displayState(currentState)
    scores = getFinalScores(currentState)
    print(f'Game over. Scores: {scores}')
Пример #36
0
def main1():
    print(
        "\nTuring machine recognizes whether or not the number of 0s is a power of 2."
    )
    st = '0000000000000000000000'
    tape1 = Tape(st, '0x')
    print("\nInput string:")
    print(st)
    states = [
        State('q1', StateType.Start),
        State('q2', StateType.Empty),
        State('q3', StateType.Empty),
        State('q4', StateType.Empty),
        State('q5', StateType.Empty),
        State('qa', StateType.Accept),
        State('qr', StateType.Reject)
    ]
    transitions = [
        Transition('q1', '$', 'q1', '$', Direction.Right),
        Transition('q1', '0', 'q2', '#', Direction.Right),
        Transition('q1', '#', 'qr', '#', Direction.Right),
        Transition('q1', 'x', 'qr', 'x', Direction.Right),
        Transition('q2', 'x', 'q2', 'x', Direction.Right),
        Transition('q2', '#', 'qa', '#', Direction.Right),
        Transition('q2', '0', 'q3', 'x', Direction.Right),
        Transition('q3', 'x', 'q3', 'x', Direction.Right),
        Transition('q3', '#', 'q5', '#', Direction.Left),
        Transition('q3', '0', 'q4', '0', Direction.Right),
        Transition('q4', '0', 'q3', 'x', Direction.Right),
        Transition('q4', 'x', 'q4', 'x', Direction.Right),
        Transition('q4', '#', 'qr', '#', Direction.Right),
        Transition('q5', '#', 'q2', '#', Direction.Right),
        Transition('q5', '0', 'q5', '0', Direction.Left),
        Transition('q5', 'x', 'q5', 'x', Direction.Left)
    ]
    tm = TuringMachine(states, transitions, tape1)
    tm.process1()
    if (tm.current_state.type == StateType.Accept):
        print('\nAccepting')
    if (tm.current_state.type == StateType.Reject):
        print('\nRejecting')
    pass
Пример #37
0
from emission import Gaussian
from duration import Poisson
from transition import Transition
from initial import Initial
from edhmm import EDHMM

import pylab as pb
import numpy as np
import logging
import sys

logging.basicConfig(filename="experiment_4.log",
                    filemode="w",
                    level=logging.DEBUG)

A = Transition(K=3, A=pb.array([[0, 0.3, 0.7], [0.6, 0, 0.4], [0.3, 0.7, 0]]))
O = Gaussian(nu=1,
             Lambda=np.array([1]),
             mu_0=[0, 0, 0],
             kappa=0.01,
             mu=[-3, 0, 3],
             tau=[np.array([[1]]),
                  np.array([[1]]),
                  np.array([[1]])])
D = Poisson(mu=[5, 15, 20],
            alpha=[1, 1, 1],
            beta=[0.0001, 0.0001, 0.0001],
            support_step=20)
pi = Initial(K=3, beta=0.001)
m = EDHMM(A, O, D, pi)
Пример #38
0
dev_result_name = '../data/dev_result.ec'
sentences = getTree(dev_file_name)
word_vec = get_word_vec()
pos_vec, label_index = get_pos_label_vec()
f_result = codecs.open(dev_result_name, 'w', 'utf-8')

for i in range(0, len(sentences)):
	sentence = sentences[i]
	words = sentence['words']
	poses = sentence['poses']
	indexs = sentence['indexs']
	arc_labels = sentence['arc_labels']
	for i in range(len(indexs)):
		indexs[i] = -2
		arc_labels[i] = 'None\n'
	trans = Transition(sentence)
	while not trans.finish():
		arc_index = 0	# everytime we should find an arc index
		# get test_vec
		input_array = []
		if len(trans.stack) == 0:
			trans.shift()
			continue
		'''
		if len(trans.stack) == 0:
			wrd_v = word_vec['unk']
			pos_v = pos_vec['unk']
			together = np.hstack((wrd_v, pos_v))
			input_array = np.hstack((input_array, together))
			wrd_v = word_vec['unk']
			pos_v = pos_vec['unk']
Пример #39
0
def add_transition(re, states):
    char = re.get_current_token()
    return_states = []
    if char == "\\":
        re.index += 1
        char = re.get_current_token()
        for current in states:
            state = State("inner", None, current)
            transition = Transition(char, state)
            current.add_transition(transition)
            return_states.append(state)
    elif char == "[":
        set_of_chars = []
        re.index += 1
        current_char = re.get_current_token()
        m = True
        if current_char == "^":
            m = False
            re.index += 1
            current_char = re.get_current_token()
        while current_char != "]":
            set_of_chars.append(current_char)
            re.index += 1
            current_char = re.get_current_token()
        for current in states:
            state = State("inner", None, current)
            for new_char in set_of_chars:
                transition = Transition(new_char, state, m)
                current.add_transition(transition)
                return_states.append(state)
    elif char == ".":
        for current in states:
            state = State("inner", None, current)
            transition = Transition("any", state)
            current.add_transition(transition)
            return_states.append(state)
    elif char == "*":
        for current in states:
            last_state = current.last_state
            if last_state is None:
                raise SyntaxError("Invalid Token " + char)
            last_state_transitions = last_state.transitions
            last_state.transitions = []
            zero_transition = Transition(None, current)
            last_state.add_transition(zero_transition)
            for transition in last_state_transitions:
                if transition.char is not None:
                    char_to_match = transition.char
                    same_transition = Transition(char_to_match, last_state)
                    last_state.add_transition(same_transition)

            return_states.append(current)
    elif char == "+":
        for current in states:
            last_state = current.last_state
            if last_state is None:
                raise SyntaxError("Invalid Token " + char)
            new_transitions = []
            for transition in last_state.transitions:
                char_to_match = transition.char
                same_transition = Transition(char_to_match, last_state)
                new_transitions.append(same_transition)
            last_state.add_multiple_transitions(new_transitions)
            return_states.append(current)
    elif char == "?":
        for current in states:
            last_state = current.last_state
            skip_char_transition = Transition(None, current)
            last_state.add_transition(skip_char_transition)
            return_states.append(current)
    elif char == "!":
        re.index += 1
        char = re.get_current_token()
        for current in states:
            state = State("inner", None, current)
            transition = Transition(char, state, False)
            current.add_transition(transition)
            return_states.append(state)
    else:
        for current in states:
            state = State("inner", None, current)
            transition = Transition(char, state)
            current.add_transition(transition)
            return_states.append(state)
    re.index += 1
    return return_states
Пример #40
0
    logger.setLevel(logging.INFO)

    env = gym.make(pms.gameName)

    # You provide the directory to write to (can be an existing
    # directory, including one with existing data -- all monitor files
    # will be namespaced). You can also dump to a tempdir if you'd
    # like: tempfile.mkdtemp().
    outdir = '/tmp/DQN-' + pms.gameName
    env.monitor.start(outdir, force=True, seed=0)

    # This declaration must go *after* the monitor call, since the
    # monitor's seeding creates a new action_space instance with the
    # appropriate pseudorandom number generator.
    agent = dqn.DqnAgent(env.action_space)
    tran = Tran(max_size=pms.bufferSize)
    caffe.set_mode_gpu()

    imageDim = np.array((pms.frameHeight,
                         pms.frameWidth))
    curFrame = np.zeros((pms.frameChannel,
                         pms.frameHeight,
                         pms.frameWidth))
    nextFrame = np.zeros((pms.frameChannel,
                          pms.frameHeight,
                          pms.frameWidth))

    testStep = 0
    update_step = 0

    for i in range(pms.episodeCount):
Пример #41
0
 def test_str(self, s1, s2, inp, out, cnt, expected_str):
     t = Transition(s1, s2, inp, out, counter=cnt)
     assert str(t) == expected_str