Ejemplo n.º 1
0
def main():
    numbers = read_numbers()
    three = numbers[3][0]

    place = [[0, 0, 0, 0, 0],
             [0, 0, 0, 0, 0],
             [0, 0, 0, 0, 0],
             [0, 0, 1, 0, 0],
             [0, 0, 0, 0, 0]]
    for i in three:
        print(i)
    print()

    BUBBLE_SIZE = 7

    q = hello(three, place)
    print("SIZE: ", len(q), len(q[0]))
    for i in q:
        print(i)
    bubbled_place = make_bubble(place, BUBBLE_SIZE)
    out_matrix(bubbled_place)
    out_matrix(zip_bubbled_data(bubbled_place, BUBBLE_SIZE))

    # важно чтобы id клеток разных регионов не пересекались!!!
    r = Region(len(merge_input([make_bubble(place, BUBBLE_SIZE), hello(three, place)])), 5, 0)

    for step in range(200):
        s = 0
        x = -1
        y = -1
        for i, I in enumerate(place):
            for j, J in enumerate(I):
                if place[i][j] == 1:
                    x, y = i, j
                    break

        print("-----" * 10)
        for i in place:
            print(i)
        print("-----" * 10)
        for i, I in enumerate(place):
            for j, J in enumerate(I):
                place[i][j] = 0

        if (x == -1 and y == -1) or step < 10:
            while 1:
                x = randrange(len(place))
                y = randrange(len(place))
                place[x][y] = 1
                if get_sum(hello(three, place)):
                    break
                place[x][y] = 0
        else:
            place[x][y] = 1
        r.step_forward(merge_input([make_bubble(place, BUBBLE_SIZE), hello(three, place)]))

        r.out_prediction()
        new_place = zip_bubbled_data(do_cut(r.get_binary_prediction(), len(make_bubble(place, BUBBLE_SIZE))),
                                     BUBBLE_SIZE)
        place = new_place
Ejemplo n.º 2
0
env.configure(tasks_dir=os.path.abspath("data/sample/imported/"), monitor_scale=10, map_shape=(10, 10))
env.monitor.start("data/sample/results/basic_dqn", force=True, seed=0)

size = 10
temporal_settings = TemporalSettings(
    region_size=size,
    column_size=1,
    initial_permanence=0.5,
    dendrite_activate_threshold=1,
    dendrite_permanence_inc_delta=0.02,
    dendrite_permanence_dec_delta=-0.1,
    passive_time_to_active_threshold=1000,
    synapse_threshold=0.45,
)

temporal_pooler_region = TemporalPoolerRegion(temporal_settings=temporal_settings)
agent = RandomAgent(env.action_space.n)


def transform_reward_to_weight_inc(reward_):
    if reward_ == -1:
        return -0.05
    if reward_ == -0.1:
        return -0.01
    if reward_ == 0:
        return 0.008
    if reward_ == 1:
        return 0.4  # 0.02
    if reward_ == 10:
        return 0.9  # 0.03
    raise ValueError
Ejemplo n.º 3
0
class Foo:
    class Chain:
        def __init__(self, a, id_):
            self.a = a
            self.id = id_

    class DendritesNode:
        def __init__(self, id_, dendrites):
            self.id = id_
            self.dendrites = dendrites

    @staticmethod
    def make_string(t):
        res = ""
        for i in range(0, 1000):
            if t & (2 ** i):
                res = res + ' ' + str(i)

        return res

    @staticmethod
    def make_string_(a):
        res = ""
        for i in a:
            res += str(i) + "\n"
        return res

    def dfs_chain(self, t, chain):
        if t.id in self.graph_used_vertex:
            return
        self.graph_used_vertex.add(t.id)
        chain.append(t)
        edges_to_go = []
        for i in self.graph_edges:
            x, y = i
            if x.id == t.id:
                edges_to_go.append(i)
        if len(edges_to_go) == 1:
            _, to = edges_to_go[0]
            cnt = 0
            for i in self.graph_edges:
                x, y = i
                if y.id == to.id:
                    cnt += 1
            if cnt == 1:
                self.dfs_chain(to, chain)

    # поиск в глубину
    def dfs(self, state, cnt=0):
        size = self.tp_level_one.temporal_settings.region_size

        t = 0
        for i in state:
            t |= 2 ** i.id
        if t == 0:
            return
        if t in self.used:
            return
        self.used[t] = True
        active_cells = set()
        for current in state:
            active_cells.add(current.id_to)
        ans = []
        for den in self.dendrites:
            q = 0
            for j in den.synapses:
                if j.id_to in active_cells and j.permanence > self.tp_level_one.temporal_settings.synapse_threshold:
                    q += 1
            if q >= self.tp_level_one.temporal_settings.dendrite_activate_threshold:
                ans.append(den)
        to = 0
        for i in ans:
            to |= 2 ** i.id
        a = [[0 for _ in range(size)] for _ in range(size)]
        for i in state:
            for j in i.synapses:
                x, y = self.id_to_Cell[j.id_to].position_x_y
                a[x][y] = 1
        b = [[0 for _ in range(size)] for _ in range(size)]
        for i in state:
            b[i.position_x_y[0]][i.position_x_y[1]] = 1

        self.edges.append(
            [self.make_string(t) + "\n" + self.make_string_(a), self.make_string(to) + "\n" + self.make_string_(b)])
        self.graph_edges.append([self.DendritesNode(t, state), self.DendritesNode(to, ans)])
        print("**", t, to)
        self.dfs(ans, cnt + 1)

    def __init__(self, pre_learning_steps, input_generator, region_size):
        self.generator = input_generator
        tp_level_one_settings = TemporalSettings(region_size=region_size, column_size=2, initial_permanence=0.5,
                                                 dendrite_activate_threshold=1, dendrite_permanence_inc_delta=0.02,
                                                 dendrite_permanence_dec_delta=-0.1,
                                                 passive_time_to_active_threshold=1000,
                                                 synapse_threshold=0.45)
        self.tp_level_one = Region(tp_level_one_settings)
        for i in range(pre_learning_steps):
            self.tp_level_one.step_forward(self.generator.get_data())
            self.tp_level_one.out_prediction()
            self.generator.move()

        # замораживаем состояние региона
        tp_level_one_settings.dendrite_permanence_dec_delta = tp_level_one_settings.dendrite_permanence_inc_delta = 0.0
        tp_level_one_settings.passive_time_to_active_threshold = 10000000000000000000

        dendrite_id_cnt = 0
        self.id_to_dendrite_map = {}
        self.id_to_Cell = {}
        self.dendrites = []

        for i, I in enumerate(self.tp_level_one.columns):
            for j, J in enumerate(I):
                for cell, Cell in enumerate(self.tp_level_one.columns[i][j].cells):
                    self.id_to_Cell[Cell.id] = Cell
                    Cell.position_x_y = [i, j]
                    for dendrite, Dendrite in enumerate(Cell.dendrites):
                        self.dendrites.append(Dendrite)
                        Dendrite.id = dendrite_id_cnt
                        Dendrite.id_to = Cell.id
                        dendrite_id_cnt += 1
                        Dendrite.position_x_y = [i, j]
                        self.id_to_dendrite_map[Dendrite.id] = Dendrite

        self.used = {}
        self.edges = []
        self.graph_edges = []

        for current in self.dendrites:
            print("id" + str(current.id) + ":" + str(current.position_x_y))
            active_cells = set()
            for i in current.synapses:
                if i.permanence > tp_level_one_settings.synapse_threshold:
                    active_cells.add(i.id_to)
            if len(active_cells) < tp_level_one_settings.dendrite_activate_threshold:
                continue
            print("acells:" + str(active_cells))
            ans = []
            for den in self.dendrites:
                q = 0
                for j in den.synapses:
                    if j.id_to in active_cells and j.permanence > tp_level_one_settings.synapse_threshold:
                        q += 1
                if q >= tp_level_one_settings.dendrite_activate_threshold:
                    ans.append(den)
            print("---" * 5)
            for t in ans:
                print(t.position_x_y)
            self.dfs(ans)
        if sys.platform == "linux":
            draw_graph_pg("refactoring", self.edges)
        else:
            draw_graph_vis("out.html", self.edges)

        self.graph_used_vertex = set()
        self.chains = []
        chain_cnt = 0
        while 1:
            edges_with_in_greater_zero = set()
            for i in self.graph_edges:
                x, y = i
                edges_with_in_greater_zero.add(y.id)
            t = None
            for i in self.graph_edges:
                x, y = i
                if x.id not in self.graph_used_vertex and x.id not in edges_with_in_greater_zero:
                    t = x
                    break
            if not t:
                break
            chain = []
            self.dfs_chain(t, chain)
            self.chains.append(self.Chain(chain, chain_cnt))
            chain_cnt += 1

        while 1:
            t = None
            for i in self.graph_edges:
                x, y = i
                if x.id not in self.graph_used_vertex and y.id not in self.graph_used_vertex:
                    t = x
                    break
            if not t:
                break
            chain = []
            self.dfs_chain(t, chain)
            self.chains.append(self.Chain(chain, chain_cnt))
            chain_cnt += 1

        for i in self.chains:
            s = ""
            for j in i.a:
                s += "[" + self.make_string(j.id) + "]"
            print("chain" + str(s))

        print("----" * 9)

        self.tp_level_one.temporal_settings.dendrite_permanence_dec_delta = 0
        self.tp_level_one.temporal_settings.dendrite_permanence_inc_delta = 0
        self.tp_level_one.temporal_settings.initial_permanence = 0
        self.column_dendrite_dependencies = {}

        for i, I in enumerate(self.tp_level_one.columns):
            for j, J in enumerate(I):
                for cell, Cell in enumerate(self.tp_level_one.columns[i][j].cells):
                    for dendrite_left, DendriteLeft in enumerate(Cell.dendrites):
                        if DendriteLeft.id not in self.column_dendrite_dependencies.keys():
                            self.column_dendrite_dependencies[DendriteLeft.id] = set()
                        for cellRight, CellRight in enumerate(self.tp_level_one.columns[i][j].cells):
                            for dendrite_right, DendriteRight in enumerate(CellRight.dendrites):
                                if DendriteLeft.id != DendriteRight.id:
                                    self.column_dendrite_dependencies[DendriteLeft.id].add(DendriteRight.id)

        self.my_states = {}

        # пока задается жестко, предполагается, что данная велечина, потом будет приходить как параметр
        self.output_size = 15

    def move(self):
        self.tp_level_one.step_forward(self.generator.get_data())
        self.generator.move()
        cur = 0
        for i in self.dendrites:
            if i.active:
                cur |= 2 ** i.id
                # print(2 ** i.id)

        # создаем битовый вектор в котором хранится информация о том, какие дендриты не могут быть в данной комбинации
        reverse_bit_vector = 0

        for i in self.dendrites:
            if i.active:
                # print(self.column_dendrite_dependencies.keys())
                # print(i.id)
                for j, J in enumerate(self.column_dendrite_dependencies[i.id]):
                    reverse_bit_vector |= 2 ** J
        f = False
        if cur & reverse_bit_vector:
            raise KeyError("Конфликт текущего дендрита и активируемого", cur, reverse_bit_vector,
                           cur & reverse_bit_vector, self.column_dendrite_dependencies[cur & reverse_bit_vector])
        for i in self.chains:
            for j in i.a:
                state_id = j.id
                state_id |= reverse_bit_vector
                state_id ^= reverse_bit_vector
                if state_id == cur:
                    my_state = i.id
                    if my_state not in self.my_states:
                        self.my_states[my_state] = len(self.my_states)
                    # проверяем умещается ли у нас выход для второго слоя
                    assert (len(self.my_states) < self.output_size ** 2)
                    print("found:", cur)
                    f = True
                    return encode(self.my_states[my_state], self.output_size)

        if not f:
            # print()
            print("not found", cur)
Ejemplo n.º 4
0
    def __init__(self, pre_learning_steps, input_generator, region_size):
        self.generator = input_generator
        tp_level_one_settings = TemporalSettings(region_size=region_size, column_size=2, initial_permanence=0.5,
                                                 dendrite_activate_threshold=1, dendrite_permanence_inc_delta=0.02,
                                                 dendrite_permanence_dec_delta=-0.1,
                                                 passive_time_to_active_threshold=1000,
                                                 synapse_threshold=0.45)
        self.tp_level_one = Region(tp_level_one_settings)
        for i in range(pre_learning_steps):
            self.tp_level_one.step_forward(self.generator.get_data())
            self.tp_level_one.out_prediction()
            self.generator.move()

        # замораживаем состояние региона
        tp_level_one_settings.dendrite_permanence_dec_delta = tp_level_one_settings.dendrite_permanence_inc_delta = 0.0
        tp_level_one_settings.passive_time_to_active_threshold = 10000000000000000000

        dendrite_id_cnt = 0
        self.id_to_dendrite_map = {}
        self.id_to_Cell = {}
        self.dendrites = []

        for i, I in enumerate(self.tp_level_one.columns):
            for j, J in enumerate(I):
                for cell, Cell in enumerate(self.tp_level_one.columns[i][j].cells):
                    self.id_to_Cell[Cell.id] = Cell
                    Cell.position_x_y = [i, j]
                    for dendrite, Dendrite in enumerate(Cell.dendrites):
                        self.dendrites.append(Dendrite)
                        Dendrite.id = dendrite_id_cnt
                        Dendrite.id_to = Cell.id
                        dendrite_id_cnt += 1
                        Dendrite.position_x_y = [i, j]
                        self.id_to_dendrite_map[Dendrite.id] = Dendrite

        self.used = {}
        self.edges = []
        self.graph_edges = []

        for current in self.dendrites:
            print("id" + str(current.id) + ":" + str(current.position_x_y))
            active_cells = set()
            for i in current.synapses:
                if i.permanence > tp_level_one_settings.synapse_threshold:
                    active_cells.add(i.id_to)
            if len(active_cells) < tp_level_one_settings.dendrite_activate_threshold:
                continue
            print("acells:" + str(active_cells))
            ans = []
            for den in self.dendrites:
                q = 0
                for j in den.synapses:
                    if j.id_to in active_cells and j.permanence > tp_level_one_settings.synapse_threshold:
                        q += 1
                if q >= tp_level_one_settings.dendrite_activate_threshold:
                    ans.append(den)
            print("---" * 5)
            for t in ans:
                print(t.position_x_y)
            self.dfs(ans)
        if sys.platform == "linux":
            draw_graph_pg("refactoring", self.edges)
        else:
            draw_graph_vis("out.html", self.edges)

        self.graph_used_vertex = set()
        self.chains = []
        chain_cnt = 0
        while 1:
            edges_with_in_greater_zero = set()
            for i in self.graph_edges:
                x, y = i
                edges_with_in_greater_zero.add(y.id)
            t = None
            for i in self.graph_edges:
                x, y = i
                if x.id not in self.graph_used_vertex and x.id not in edges_with_in_greater_zero:
                    t = x
                    break
            if not t:
                break
            chain = []
            self.dfs_chain(t, chain)
            self.chains.append(self.Chain(chain, chain_cnt))
            chain_cnt += 1

        while 1:
            t = None
            for i in self.graph_edges:
                x, y = i
                if x.id not in self.graph_used_vertex and y.id not in self.graph_used_vertex:
                    t = x
                    break
            if not t:
                break
            chain = []
            self.dfs_chain(t, chain)
            self.chains.append(self.Chain(chain, chain_cnt))
            chain_cnt += 1

        for i in self.chains:
            s = ""
            for j in i.a:
                s += "[" + self.make_string(j.id) + "]"
            print("chain" + str(s))

        print("----" * 9)

        self.tp_level_one.temporal_settings.dendrite_permanence_dec_delta = 0
        self.tp_level_one.temporal_settings.dendrite_permanence_inc_delta = 0
        self.tp_level_one.temporal_settings.initial_permanence = 0
        self.column_dendrite_dependencies = {}

        for i, I in enumerate(self.tp_level_one.columns):
            for j, J in enumerate(I):
                for cell, Cell in enumerate(self.tp_level_one.columns[i][j].cells):
                    for dendrite_left, DendriteLeft in enumerate(Cell.dendrites):
                        if DendriteLeft.id not in self.column_dendrite_dependencies.keys():
                            self.column_dendrite_dependencies[DendriteLeft.id] = set()
                        for cellRight, CellRight in enumerate(self.tp_level_one.columns[i][j].cells):
                            for dendrite_right, DendriteRight in enumerate(CellRight.dendrites):
                                if DendriteLeft.id != DendriteRight.id:
                                    self.column_dendrite_dependencies[DendriteLeft.id].add(DendriteRight.id)

        self.my_states = {}

        # пока задается жестко, предполагается, что данная велечина, потом будет приходить как параметр
        self.output_size = 15
Ejemplo n.º 5
0
from gens.input_generators import Cross
from gens.make_bubble import MakeBubble
from temporalPooler.htm__region import Region as TemporalPoolerRegion
from apps.settings import *

size = 12
temporal_settings = TemporalSettings(region_size=size, column_size=4, initial_permanence=0.5,
                                     dendrite_activate_threshold=8, dendrite_permanence_inc_delta=0.02,
                                     dendrite_permanence_dec_delta=-0.1, passive_time_to_active_threshold=1000,
                                     synapse_threshold=0.45)
generator = MakeBubble(Cross, size // 4, 4)

temporal_pooler_region = TemporalPoolerRegion(temporal_settings=temporal_settings)

for i in range(700):
    generator.out()
    temporal_pooler_region.step_forward(generator.get_data())
    temporal_pooler_region.out_prediction()
    generator.move()
Ejemplo n.º 6
0
from temporalPooler.htm__region import Region
from apps.settings import temporal_settings
from apps.settings import input_settings
from gens.input_generators import MakeBubble

generator = MakeBubble(input_settings.GENERATOR, temporal_settings.REGION_SIZE_N,
                                         input_settings.SCALE)

r = Region(temporal_settings.REGION_SIZE_N * input_settings.SCALE, temporal_settings.COLUMN_SIZE)
print("""
**** ЛЕГЕНДА *****
P1 - Клетка с номером 1, данной колонки находится в состоянии предсказания
A3 - Клетка с номером 3, данной колонки активировалась
O3 - Клетка с номером 3, данной колонки активировалась из-за  простоя (PassiveTime > PASSIVE_TIME_TO_ACTIVE_THRESHOLD)
""")

for i in range(input_settings.STEPS_NUMBER):
    print('---------------------')
    # generator.out()
    r.step_forward(generator.get_data())
    r.out_prediction()
    generator.move()
Ejemplo n.º 7
0
from spatialPooler.sp_region import Region as SpatialPoolerRegion
from apps.settings import *

input_settings = InputSettings(SCALE=1, STEPS_NUMBER=300)
generator = ListImages(12)

spatial_settings = SpatialSettings(debug = False, min_overlap = 1, desired_local_activity = 2, connected_pct = 1,
                           connected_perm = 0.1, xinput = generator.square_size, yinput = generator.square_size,
                           potential_radius = 4, xdimension = 12, ydimension = 12, initial_inhibition_radius = 2,
                           permanence_inc = 0.02, permanence_dec = 0.1, max_boost = 2, min_duty_cycle_fraction = 0.2)

temporal_settings = TemporalSettings(region_size=spatial_settings.xdimension, column_size=4, initial_permanence=0.5,
                                     dendrite_activate_threshold=1, dendrite_permanence_inc_delta=0.02,
                                     dendrite_permanence_dec_delta=-0.1, passive_time_to_active_threshold=1000,
                                     synapse_threshold=0.45)


spatial_pooler_region = SpatialPoolerRegion(spatial_settings=spatial_settings, mapper=SquareMapperAutoRadius())
temporal_pooler_region = TemporalPoolerRegion(temporal_settings=temporal_settings)

for i in range(input_settings.STEPS_NUMBER):
    generator.out()
    outdata=spatial_pooler_region.step_forward(generator.get_data())
    print_matrix(to_binmatrix(outdata))
    temporal_pooler_region.step_forward(outdata)
    temporal_pooler_region.out_prediction()
    cols=temporal_pooler_region.columns
    imagined_input = spatial_pooler_region.out_prediction(cols, temporal_settings.region_size)

    generator.move()