Пример #1
0
    def __get_attractor_landscape_bn(self):
        def A(*args):
            return args[0] or args[1]

        def B(*args):
            return args[0] or args[1]

        def C(*args):
            return args[0] and args[1]

        def D(*args):
            return not (args[0]) and args[1]

        return SelectiveBooleanNetwork(
            [
                BooleanNode('0',
                            predecessors=['2', '3'],
                            bf=BooleanFunction(2, result_generator=A)),  # A
                BooleanNode('1',
                            predecessors=['0', '1'],
                            bf=BooleanFunction(2, result_generator=B)),  # B
                BooleanNode('2',
                            predecessors=['0', '1'],
                            bf=BooleanFunction(2, result_generator=C)),  # C
                BooleanNode('3',
                            predecessors=['2', '3'],
                            bf=BooleanFunction(2, result_generator=D))  # D
            ],
            input_nodes=['0'])
Пример #2
0
    def new_selector(self) -> SelectiveBooleanNetwork:

        nodes = self.__r_connect_nodes(self.__build_nodes())

        return SelectiveBooleanNetwork(nodes,
                                       input_nodes=self.input_nodes,
                                       output_nodes=self.output_nodes)
Пример #3
0
    def test_selector_constraint_4(self):
        bn = SelectiveBooleanNetwork.from_json(
            read_json('./test/bn_for_test.json'))

        c = constraints.test_attraction_basins(bn, 10)

        self.assertTrue(c)
Пример #4
0
def step2_evaluation(bn: SelectiveBooleanNetwork, phi: int, executor=None):

    if bn is not None:
        c4 = constraints.test_attraction_basins(bn, phi=phi, executor=executor)
        if c4:
            bn.attractors_input_map = c4
            return True

    return False
Пример #5
0
 def test_selector_constraint_2(self):
     bn = SelectiveBooleanNetwork.from_json(
         read_json('./test/bn_for_test.json'))
     self.assertTrue(
         constraints.test_attractors_transitions(bn, {
             'a0': {
                 'a0': 0.3,
                 'a1': 0.3
             },
             'a1': {
                 'a0': 0.3,
                 'a1': 0.3
             },
         }))
Пример #6
0
def __task__test_state_attraction(params: tuple):

    bnjson, s, i, phi = params

    if 'bn' not in globals():
        global bn, virgin
        virgin = None
        # bn = SelectiveBooleanNetwork.from_json(bnjson)

    if bnjson != virgin:
        virgin = bnjson
        bn = SelectiveBooleanNetwork.from_json(bnjson)

    return test_state_attraction(bn, s, i, phi)
Пример #7
0
def test_attraction_basins(bn: SelectiveBooleanNetwork,
                           phi: int,
                           executor=None) -> dict or bool:
    '''
    Test whether the given Boolean Network fixate itself
    on a specific attractor once a input value is settled
    '''

    inputs = map(lambda x: dict(zip(bn.input_nodes, x)),
                 itertools.product(TRUTH_VALUES, repeat=len(bn.input_nodes)))

    states = itertools.product(TRUTH_VALUES, repeat=len(bn))

    params = itertools.product(states, inputs)

    attrs = udict()

    if executor is None or 2**len(bn) / NP < 2**5:  #

        for s, i in params:

            bs, a = test_state_attraction(bn, s, i, phi)

            if not attrs.update(bs, a):
                return False

    else:

        params = map(lambda x: (bn.to_json(), x[0], x[1], phi), params)

        for bs, a in set(executor(__task__test_state_attraction, params)):
            if not attrs.update(bs, a):
                return False

    # An attractor should appear for at least 1 set of inputs
    # # that is, even if there are repeated keys, all key shall appears at least once
    return attrs if len(set(attrs.values())) == len(
        bn.atm.attractors) else False


##############################################################################
Пример #8
0
                          fpattern=r'rtest_data_(?:bn_subopt_)?' +
                          f'{fu.FNAME_PATTERN}.json',
                          recursively=args.recursively,
                          ds_merge_level=args.merge_level,
                          data_getter=get_data))

    bninfos = OrderedDict()

    for path in cpaths(args.config.bn_model_path, recursive=3):
        if path.is_file() and 'json' in path.suffix and 'bn' in path.name:

            jsonrepr = read_json(path)

            try:

                bn = SelectiveBooleanNetwork.from_json(jsonrepr)

                info = jsonrepr['gen_params']
                info['atm'] = bn.atm.tableau
                info['ai_map'] = bn.attractors_input_map
                info['a_len'] = dict(
                    map(lambda a: (a[0], len(a[1])),
                        bn.atm.dattractors.items()))

                name = pu.get_simple_fname(path.name,
                                           fu.FNAME_PATTERN,
                                           parts=['%s', '%s', '%s'],
                                           uniqueness=2)

                name = set(
                    filter(lambda x: '_'.join(x.split('_')[:2]) == name,
Пример #9
0
from bncontroller.sim.robot.core import BNController, SBNController
from bncontroller.sim.logging.logger import FileLogger
from bncontroller.sim.logging.datadumper import SimulationDataDumper

#-------------------------------------------

load_global_config()

# print(os.getcwd())
# print(GLOBALS.slct_behaviours_map)

logger = lambda *items: None

#-------------------------------------------

bnselector = SelectiveBooleanNetwork.from_json(read_json(
    GLOBALS.bn_model_path))

bn_pt = OpenBooleanNetwork.from_json(
    read_json(GLOBALS.slct_behaviours_map['0']), )

bn_apt = OpenBooleanNetwork.from_json(
    read_json(GLOBALS.slct_behaviours_map['1']), )

phototaxist = BNController(
    model=bn_pt,
    sensing_interval=int(GLOBALS.sim_sensing_interval_ms),
    bin_thresholds=dict(**GLOBALS.sim_sensors_thresholds),
    bin_strategies={
        DeviceName.LIGHT: bin_strategies.phototaxis,
    },
    led_color=0x00ff00)
Пример #10
0
    load_global_config()

    NP = cpu_count()

    pool = Pool(processes=NP)

    mapper = lambda f, p: pool.imap_unordered(f, p, chunksize=2 * NP)

    for path in cpaths(GLOBALS.bn_model_path, recursive=3):

        print(path)

        bnjson = read_json(path)

        bn = SelectiveBooleanNetwork.from_json(bnjson)
        # print(2**len(bn))
        # print(bnjson['gen_params'])

        i = max(map(len, bn.atm.attractors)) * len(bn) * 20

        t = time.perf_counter()

        if isinstance(GLOBALS.slct_target_transition_tau, list):
            tTau_map = {
                'a0': {
                    'a1': max(GLOBALS.slct_target_transition_tau)
                },
                'a1': {
                    'a0': max(GLOBALS.slct_target_transition_tau)
                }
Пример #11
0
 def test_selector_constraint_3(self):
     bn = SelectiveBooleanNetwork.from_json(
         read_json('./test/bn_for_test.json'))
     i = max(map(len, bn.atm.attractors)) * len(bn) * 20
     self.assertTrue(constraints.test_bn_state_space_homogeneity(
         bn, i, 0.1))
Пример #12
0
 def test_selector_constraint_1(self):
     bn = SelectiveBooleanNetwork.from_json(
         read_json('./test/bn_for_test.json'))
     self.assertTrue(constraints.test_attractors_number(bn, 2))
     self.assertFalse(constraints.test_attractors_number(bn, 4))