Пример #1
0
def three_state_loop_dfa():
    """
    alphabet: {p, b}
    s1 -> p -> s2
    s2 -> b -> s3
    s3 -> p -> s1
    initial: s1
    accepting: s1
    """
    d = dfa.DFA()
    d.states = set()
    d.initial_state = None
    s1 = state.State('s1', True, True)
    s2 = state.State('s2', False, False)
    s3 = state.State('s3', False, False)
    d.add_state(s1)
    d.add_state(s2)
    d.add_state(s3)
    d.initial_state = s1
    d.alphabet = {'p', 'b'}
    d.add_edge(s1, s2, 'p')
    d.add_edge(s2, s3, 'b')
    d.add_edge(s3, s1, 'p')

    return d
Пример #2
0
def test_add_state_exc():
    d = dfa.DFA()
    s = state.State('s1', True, False)
    s_ninit = state.State('si', True, True)
    with pytest.raises(error.StateError):
        d.add_state(s)
    with pytest.raises(error.StateError):
        d.add_state(s_ninit)
Пример #3
0
def test_add_edge():
    d = dfa.DFA()
    s = state.State('s2', True, False)
    d.add_state(s)
    d.set_alphabet({'a'})
    d.add_edge(d.initial_state, s, 'a')
    assert (len(d.initial_state.edges) == 1)
Пример #4
0
def test_add_state():
    d = dfa.DFA()
    print([s.label for s in d.states])
    assert (len(d.states) == 1)
    s = state.State('s2', True, False)
    d.add_state(s)
    assert (len(d.states) == 2)
    assert (s in d.states)
Пример #5
0
def test_add_edge_exc():
    d = dfa.DFA()
    s = state.State('s2', True, False)
    d.set_alphabet({'a'})
    with pytest.raises(error.StateError):
        d.add_edge(d.initial_state, s, 'a')

    d.add_state(s)
    with pytest.raises(error.AlphabetError):
        d.add_edge(d.initial_state, s, 'b')
Пример #6
0
def two_state_dfa():
    """
    alphabet: {a}
    s1 -> a -> s2 
    s2 -> a -> s2
    initial: s1
    accepting: s2
    """
    d = dfa.DFA()
    d.states = set()
    d.initial_state = None
    s1 = state.State("s1", False, True)
    s2 = state.State("s2", True, False)
    d.add_state(s1)
    d.add_state(s2)
    d.initial_state = s1
    d.set_alphabet({'a'})
    d.add_edge(s1, s2, 'a')
    d.add_edge(s2, s2, 'a')

    return d
Пример #7
0
    def test_craft(self) -> None:
        """
        Checks if crafting a item (axe) works correctly:
         * items used fully should be removed
         * not used items should not be touched
         * inventory should be updated
        """

        NUM_ROCK, NUM_GOLD, NUM_LOGS = 2, 2, 1

        elevation_function = geometry.ElevationFunction(1000)
        rock = entities.Rocks(1, None)
        gold = entities.Gold(2, None)
        logs = entities.Log(3, None)
        assert rock.features.stackable is not None
        assert gold.features.stackable is not None
        rock.features.stackable.set_size(NUM_ROCK)
        gold.features.stackable.set_size(NUM_GOLD)
        entity_list: List[essentials.Entity] = [rock, gold, logs]

        inv = inventory.Inventory()
        inv.insert_entry(1, rock.as_info())
        inv.insert_entry(2, gold.as_info())
        inv.insert_entry(3, logs.as_info())

        st = state.State(elevation_function, entity_list)

        assembly = craft.Assembly(
            recipe_codename='axe',
            sources=[[rock.as_craft_item()], [logs.as_craft_item()]],
        )

        result = st.craft_entity(assembly, inv)

        self.assertEqual(len(result.created), 1)
        self.assertEqual(set(result.deleted), {logs.id, rock.id})
        ents = list(st.get_entities())
        self.assertEqual(len(ents), 2)
        self.assertEqual(ents[0], gold)
        self.assertEqual(ents[1].get_name(), 'axe')

        axe = ents[1]

        result_items = inv.to_items()
        expected_items = {
            craft.Item(actor_id=gold.id,
                       essence=gold.ESSENCE,
                       quantity=NUM_GOLD),
            craft.Item(actor_id=axe.id, essence=axe.ESSENCE, quantity=1),
        }
        self.assertEqual(result_items, expected_items)
Пример #8
0
def five_state_dfa():
    """
    alphabet: {a, b, c}
    s1 -> a -> s2
    s2 -> b -> s2
    s2 -> a -> s3
    s3 -> c -> s5
    s5 -> b -> s5
    s5 -> a -> s4
    s4 -> a -> s5
    initial: s1
    accepting: s2, s5
    """
    d = dfa.DFA()
    d.states = set()
    d.initial_state = None
    s1 = state.State("s1", False, True)
    s2 = state.State("s2", True, False)
    s3 = state.State("s3", False, False)
    s4 = state.State("s4", False, False)
    s5 = state.State("s5", True, False)
    d.add_state(s1)
    d.add_state(s2)
    d.add_state(s3)
    d.add_state(s4)
    d.add_state(s5)
    d.initial_state = s1
    d.set_alphabet({'a', 'b', 'c'})
    d.add_edge(s1, s2, 'a')
    d.add_edge(s2, s2, 'b')
    d.add_edge(s2, s3, 'a')
    d.add_edge(s3, s5, 'c')
    d.add_edge(s5, s5, 'b')
    d.add_edge(s5, s4, 'a')
    d.add_edge(s4, s5, 'a')

    return d
Пример #9
0
    def test_merge_entities_not_fit(self) -> None:
        NUM1, NUM2 = 12, 12
        NUMS = NUM1 + NUM2
        HAND, POCKET = defs.Hand.LEFT, 2

        elevation_function = geometry.ElevationFunction(1000)
        rock1 = entities.Rocks(1, None)
        rock2 = entities.Rocks(2, None)
        assert rock1.features.stackable is not None
        assert rock2.features.stackable is not None
        rock1.features.stackable.set_size(NUM1)
        rock2.features.stackable.set_size(NUM2)
        entity_list: List[essentials.Entity] = [rock1, rock2]

        inv = inventory.Inventory()
        inv.store_entry(HAND, rock1.as_info())
        inv.insert_entry(POCKET, rock2.as_info())

        st = state.State(elevation_function, entity_list)

        st.merge_entities(inv, HAND, POCKET)

        source_entity = st.get_entity(rock1.id)
        source_entry = inv.get_hand_entry(HAND)
        self.assertIsNotNone(source_entry)
        self.assertIsNotNone(source_entity)
        self.assertIsNotNone(
            source_entity.features.stackable)  #type:ignore[union-attr]
        self.assertEqual(source_entry.id, rock1.id)  #type:ignore[union-attr]
        self.assertEqual(source_entity.features.stackable.get_size(),
                         4)  #type:ignore[union-attr]

        target_entity = st.get_entity(rock2.id)
        target_entry = inv.get_pocket_entry(POCKET)
        self.assertIsNotNone(target_entry)
        self.assertIsNotNone(target_entity)
        self.assertIsNotNone(
            target_entity.features.stackable)  #type:ignore[union-attr]
        self.assertEqual(target_entry.id, rock2.id)  #type:ignore[union-attr]
        self.assertEqual(target_entity.features.stackable.get_size(),
                         20)  #type:ignore[union-attr]
Пример #10
0
    elif len(sys.argv) == 1:
        os.chdir('examples')
        exp_config_file = 'config_Example2_sim.py'
    elif len(sys.argv) > 2:
        sys.exit('Wrong number of argument')

    # Experiment config file
    print("* Experimental configuration file")
    print(exp_config_file)
    config = exp.exp(exp_config_file)
    cmd = 'cp ' + exp_config_file + ' ' + config.tmp_DA_path + '/config.py'
    os.system(cmd)

    # Init
    print("* State initialization")
    State = state.State(config, first=True)

    # Model
    print('* Model Initialization')
    Model = mod.Model(config, State)

    # Observations
    print('* Observations')
    dict_obs = obs.obs(config, State)

    # Analysis
    print('* Analysis')
    ana.ana(config, State, Model, dict_obs=dict_obs)

    end = time.time()
    print('computation time:', end - start, 'seconds')
Пример #11
0
def test_init():
    s = state.State('s1', True, False)
    assert (s.label == 's1')
    assert (len(s.edges) == 0)
    assert (s.accepting)
    assert (not s.initial)
Пример #12
0
def test_edge():
    s1 = state.State('s1', False, True)
    s2 = state.State('s2', False, False)
    s1.add_edge('a', s2)
    assert (len(s1.edges) == 1)
    assert (len(s2.edges) == 0)
Пример #13
0
def test_change():
    s = state.State('s', False, False)
    s.set_acc()
    assert (s.accepting)
Пример #14
0
from src import material, state

copper_params = {
    'mag': [0, 0, 1],
    'sa_equil': [0.0],
    'diff': [0.3],
    'polar_con': [0.5],
    'polar_diff': [0.9],
    'len_pre': [4.0e-9],
    'len_dph': [4.0e-9],
    'len_sf': [2.0e-9],
}

copper_1 = material.Material(copper_params)
copper_1.params['thickness'] = [5.0e-9]
copper_1.params['n_dx'] = [100]

copper_2 = material.Material(copper_params)
copper_2.params['thickness'] = [10.0e-9]
copper_2.params['n_dx'] = [100]

sys = state.State([copper_1, copper_2])
#print(sys.space)
Пример #15
0
 def __init__(self):
     self.state = state.State()
     self.cycles = 7
     self.apu_cycles = 0
     self.ppu_cycles = 0
Пример #16
0
     params2 = params2.split(' ')
 
 
 print('\n\n\
 *****************************************************************\n\
 *****************************************************************\n\
               1st Experiment (iteration ' + str(iteration) +')\n\
 *****************************************************************\n\
 *****************************************************************\n')
 time0 = datetime.now()
 # Updtade configuration file
 print('* Updtade configuration file')
 update_config(config1,iteration,params=params1)
 # State
 print('* State Initialization')
 State1 = state.State(config1)
 # Model
 print('* Model Initialization')
 Model1 = mod.Model(config1,State1)
 # Observations
 print('* Observations')
 dict_obs1 = get_dict_obs(config1,State1)
 # Compute new observations taking into account previous estimation
 print('* Compute new observations')
 if iteration>0:
     update_config(config2,iteration-1)
     State2 = state.State(config2)
     compute_new_obs(iteration,dict_obs1,config2,State2)
 # Analysis
 print('* Analysis')
 ana.ana(config1,State1,Model1,dict_obs=dict_obs1)