Exemplo n.º 1
0
def no_con_last_layer(DNA, max=None):
    if DNA:
        g = Fun.DNA2graph(DNA)
        node = g.key2node.get(Fun.num_layers(DNA) - 1)
        if len(node.parents) == 1:
            return DNA
        else:
            return False
 def node2obs(self, space, node):
     creator = self.observation_creator
     weight = Funct.node2num_particles(node)
     path = list(Funct.node2direction(node))
     path[0] = path[0] - self.center
     num_conv_layers = len(
         [0 for layer in space.node2key(node) if layer[0] == 0])
     return creator(num_layer=num_conv_layers, path=path, weight=weight)
Exemplo n.º 3
0
def update_from_select_nm(self):
    phase_space = self.phase_space
    creator = self.Creator
    selector = self.Selector
    version = self.version
    phase_space.time = phase_space.time + 1
    list_particles = [
        phase_space.node2particles(node) for node in phase_space.objects
    ]
    print("particles: ", list_particles)
    node_max = node_max_particles(phase_space)
    p_m = Funct.node2num_particles(node_max)
    node_c = phase_space.key2node(phase_space.DNA_graph.center)
    p_c = Funct.node2num_particles(node_c)
    print(f'The value of p_m is : {p_m} and pc is : {p_c} ')
    if (p_m > p_c * 2):
        phase_space.time = 0
        num_particles = phase_space.num_particles
        old_graph = phase_space.DNA_graph
        old_center = old_graph.center
        condition = old_graph.condition
        typos = old_graph.typos
        #node_max = phase_space.node_max_particles
        node_max = node_max_particles(phase_space)
        print("node_max particles:", Funct.node2num_particles(node_max))
        center = phase_space.node2key(node_max)
        status = phase_space.status
        Alai = status.Alai
        stream = phase_space.stream
        delta = stream.key2len_hist(center)
        Alai.update(delta)
        stream.signals_off()
        stream.key2signal_on(center)
        stream.clear()
        selector.update(center)
        actions = selector.get_predicted_actions()
        x = old_graph.x_dim
        y = old_graph.y_dim
        space = DNA_Graph(center,
                          1, (x, y),
                          condition,
                          actions,
                          version,
                          creator,
                          selector=selector,
                          num_morphisms=5)
        phase_space.DNA_graph = space
        phase_space.objects = space.objects
        phase_space.support = []
        phase_space.create_particles(num_particles + 1)
        phase_space.attach_balls()
        phase_space.max_changed = False
        phase_space.node_max_particles = None
        self.space = space
        self.phase_space = phase_space
        self.objects = phase_space.objects
        self.support = phase_space.support
        self.Graph = phase_space.DNA_graph
Exemplo n.º 4
0
    def update_current_path(self,space, new_center):
        
        if not isinstance(space, tuple) and new_center is not None:
            node_nc = space.key2node(new_center)
            absolute_direction = Funct.node2direction(node_nc)
            relative_direction = Funct.absolute2relative(direction=absolute_direction, last_layer=self.last_mutated_layer,
                                                            max_layers=self.max_layers_condition)
            self.current_path.path.append(relative_direction)
            self.current_path.absolute_path.append(absolute_direction)

            if len(self.current_path.path)>self.max_path_size:
                self.current_path.path.pop(0)
                self.current_path.absolute_path.pop(0)
Exemplo n.º 5
0
    def __run_predict(self):

        if self.observations is not None and len(self.observations) > 0:
            
            input_predict = self.converter.generate_LSTM_predict(observation=self.current_path)
            print("path to predict: ", self.current_path.path)
            print(input_predict.size())
            top_directions = len(self.observations) // 2
            predicted_tensor = self.net.predict(x=input_predict)
            predicted_relative_directions = self.converter.topK_predicted_directions(predicted_tensor=predicted_tensor, k=len(self.observations))

            print("relative predicted: ", predicted_relative_directions)
            predicted_absolute_directions = []

            for relative_direction in predicted_relative_directions:
                absolute_direction = Funct.relative2absolute(direction=relative_direction, last_layer=self.last_mutated_layer,
                                                                max_layers=self.max_layers_condition)
                predicted_absolute_directions.append(absolute_direction)

            for direction in predicted_absolute_directions:
                
                layer = direction[0]
                mutation = direction[1]

                direction_accepted = self.verify_direction(layer=layer, mutation=mutation)

                if direction_accepted == True:  
                    self.predicted_actions.append( (layer,mutation) )
                
                if len(self.predicted_actions) >= top_directions:
                    break
            
            print("predicted by lstm: ", self.predicted_actions)
Exemplo n.º 6
0
    def __addLayer_convex(self, mutation):
        
        graph_dna = Funct.DNA2graph(self.center_key)

        parent_layers = []

        acum_parents = 0
        acum_history = []

        for layer_index in range(self.current_num_layer):
            
            node = graph_dna.key2node.get(layer_index)
            parents = len(node.parents)
            parent_layers.append(parents)
            acum_parents += parents
            acum_history.append(acum_parents)

        factor = random.randint(0, sum(parent_layers))
        selected_index = -1

        for i in range(len(acum_history)):

            if factor <= acum_history[i]:
                selected_index = i
                break
        
        
        direction_accepted = self.verify_direction(layer=selected_index, mutation=self.mutations[mutation])

        if direction_accepted == True:
            self.predicted_actions.append( (selected_index, self.mutations[mutation]) )
    def update_current_center(self, space=None, new_center=None):

        if not (type(space)) is tuple:
            node_nc = space.key2node(new_center)
            if not (new_center == space.center):
                direction = Funct.node2direction(node_nc)
                if direction[1] == (1, 0, 0, 0):
                    self.center = self.center + 1
                elif direction[1] == (-1, 0, 0, 0):
                    self.center = self.center - 1

        if self.observations:
            node_new_c = space.graph.node2key
            lef_weight = sum([
                observation.weight for observation in self.observations
                if observation.path[0] < 0
            ])
            right_weight = sum([
                observation.weight for observation in self.observations
                if observation.path[0] > 0
            ])
            center_weight = sum([
                observation.weight for observation in self.observations
                if observation.path[0] == 0
            ])
            if center_weight > max(lef_weight, center_weight):
                pass
            elif right_weight > lef_weight:
                self.center = self.center + 1
            elif right_weight < lef_weight:
                self.center = self.center - 1
Exemplo n.º 8
0
def update_from_select_Alai(self):
    phase_space = self.phase_space
    creator = self.Creator
    selector = self.Selector
    version = self.version
    phase_space.time = phase_space.time + 1
    node_max = node_max_particles(phase_space)
    node_c = phase_space.key2node(phase_space.DNA_graph.center)
    p_c = Funct.node2num_particles(node_c)
    p_m = Funct.node2num_particles(node_max)
    print(f'The value of p_m is : {p_m} and p_c is : {p_c} ')
    if (p_m > p_c * 2) or (phase_space.time > 4000):
        phase_space.time = 0
        num_particles = phase_space.num_particles
        old_graph = phase_space.DNA_graph
        old_center = old_graph.center
        condition = old_graph.condition
        typos = old_graph.typos
        node_max = phase_space.node_max_particles
        center = phase_space.node2key(node_max)
        selector.update(old_graph, new_center=center)
        actions = selector.get_predicted_actions()
        x = old_graph.x_dim
        y = old_graph.y_dim
        space = DNA_Graph(center, 1, (x, y), condition, actions, version,
                          creator)
        phase_space.DNA_graph = space
        phase_space.objects = space.objects
        phase_space.support = []
        phase_space.create_particles(num_particles + 1)
        phase_space.stream.signals_off()
        phase_space.attach_balls()
        phase_space.max_changed = False
        phase_space.node_max_particles = None
        self.space = space
        self.phase_space = phase_space
        self.objects = phase_space.objects
        self.support = phase_space.support
        self.Graph = phase_space.DNA_graph
    elif False:
        #elif phase_space.time>self.clear_period:
        phase_space.time = 0
        node2remove = phase_space2node2remove(phase_space)
        node_c = phase_space.key2node(phase_space.DNA_graph.center)
        if node2remove and not (node2remove == node_c):
            remove_node(phase_space, node2remove)
            new_DNA = add_node(phase_space, selector)
Exemplo n.º 9
0
def no_con_image(DNA, max=None):
    if DNA:
        g = Fun.DNA2graph(DNA)
        node = g.key2node.get(-1)
        if len(node.kids) == 1:
            return DNA
        else:
            return False
Exemplo n.º 10
0
def con_image(DNA, max=None):
    if DNA:
        g = Fun.DNA2graph(DNA)
        condition = all(
            [node_con_image(g, node) for node in list(g.key2node.values())])
        if condition:
            return DNA
        else:
            return None
Exemplo n.º 11
0
def add_node(phase_space, Selector, particles=0):
    center = phase_space.center()
    DNA_graph = phase_space.DNA_graph
    graph = DNA_graph.graph
    keys = list(graph.node2key.values())
    new_DNA = None
    while (new_DNA in keys) or new_DNA == None:
        new_DNA = Selector.DNA2new_DNA(center)
    actions = Selector.get_predicted_actions()
    direction = tuple(actions[0])
    Funct.add_node(graph, new_DNA)
    graph.add_edges(center, [new_DNA])
    new_node = phase_space.key2node(new_DNA)
    DNA_graph.objects.append(new_node)
    p = Funct.node2plane(new_node)
    p.direction = direction

    return new_DNA
Exemplo n.º 12
0
def max_parents(DNA, max):
    if DNA:
        g = Fun.DNA2graph(DNA)
        if all([
                len(node.parents) < max + 1
                for node in list(g.key2node.values())
        ]):
            return DNA
        else:
            return False
Exemplo n.º 13
0
def update_from_select_stochastic(self):
    phase_space = self.phase_space
    creator = self.Creator
    selector = self.Selector
    version = self.version
    phase_space.time = phase_space.time + 1
    p = randint(0, 300)
    if phase_space.node_max_particles:
        node_max = phase_space.node_max_particles
        node_c = phase_space.key2node(phase_space.DNA_graph.center)
        p_c = Funct.node2num_particles(node_c)
        p_m = Funct.node2num_particles(node_max)
    if (phase_space.max_changed
            and p_m * 0.9 > p_c) or (phase_space.time > 4000):
        phase_space.time = 0
        num_particles = phase_space.num_particles
        old_graph = phase_space.DNA_graph
        old_center = old_graph.center
        condition = old_graph.condition
        typos = old_graph.typos
        node_max = phase_space.node_max_particles
        center = phase_space.node2key(node_max)
        selector.update(old_graph, new_center=center)
        actions = selector.get_predicted_actions()
        x = old_graph.x_dim
        y = old_graph.y_dim
        space = DNA_Graph(center, 1, (x, y), condition, actions, version,
                          creator)
        phase_space.DNA_graph = space
        phase_space.objects = space.objects
        phase_space.support = []
        phase_space.create_particles(num_particles + 1)
        phase_space.stream.signals_off()
        phase_space.attach_balls()
        phase_space.max_changed = False
        phase_space.node_max_particles = None
        self.space = space
        self.phase_space = phase_space
        self.objects = phase_space.objects
        self.support = phase_space.support
        self.Graph = phase_space.DNA_graph
Exemplo n.º 14
0
    def register_observations(self,space,new_center=None):
        
        creator=self.observation_creator
        if type(space) is tuple:
            num_conv_layers = len([0 for layer in space if
                layer[0] == 0])
            self.current_num_layer=num_conv_layers
            if new_center:
                self.center_key=new_center
            else:
                self.center_key=space
        else:
            if new_center:
                self.center_key=new_center
            else:
                self.center_key=space.center
            center=space.center
            self.current_num_layer=len([0 for layer in new_center if
                layer[0] == 0])
            node_c=space.key2node(center)
            
            self.observations = []
            
            weight_array = []
            i = 0
            for node in node_c.kids:

                if Funct.node2energy(node) > 0:
                    weight_array.append(Funct.node2energy(node)*10)

            weight_array = Funct.normalize(dataset=weight_array)
            for node in node_c.kids:

                if Funct.node2energy(node) > 0:

                    observation_path = []
                    for direction in self.current_path.path:
                        observation_path.append(direction)

                    absolute_direction = Funct.node2direction(node)
                    relative_direction = Funct.absolute2relative(direction=absolute_direction, last_layer=self.last_mutated_layer, 
                                                                    max_layers=self.max_layers_condition)
                    observation_path.append(relative_direction)

                    if len(observation_path) > self.max_observation_size:
                        observation_path.pop(0)
                    
                    observation = self.observation_creator(path=observation_path, weight=weight_array[i])
                    self.observations.append(observation)

                    i += 1
 def register_observations(self, space, new_center=None):
     creator = self.observation_creator
     if type(space) is tuple:
         num_conv_layers = len([0 for layer in space if layer[0] == 0])
         self.current_num_layer = num_conv_layers
         if new_center:
             self.center_key = new_center
         else:
             self.center_key = space
     else:
         if new_center:
             self.center_key = new_center
         else:
             self.center_key = space.center
         center = space.center
         self.current_num_layer = len(
             [0 for layer in new_center if layer[0] == 0])
         node_c = space.key2node(center)
         self.observations = (self.observations + [
             self.node2obs(space, node)
             for node in node_c.kids if Funct.node2num_particles(node) > 0
         ])
Exemplo n.º 16
0
def test():

    observation_size = 3
    max_layers_lstm = 5 * 2
    num_actions = 4
    mutations = ((0, 1, 0, 0), (0, -1, 0, 0), (0, 0, 1, 1), (0, 0, -1, -1))
    directions_per_observations = [[(1, (0, 1, 0, 0)), (2, (0, -1, 0, 0)),
                                    (0, (0, 1, 0, 0))],
                                   [(1, (0, 1, 0, 0)), (2, (0, -1, 0, 0)),
                                    (0, (0, 0, 1, 1))],
                                   [(1, (0, 1, 0, 0)), (2, (0, -1, 0, 0)),
                                    (0, (0, -1, 0, 0))],
                                   [(1, (0, 1, 0, 0)), (2, (0, -1, 0, 0)),
                                    (0, (0, 0, -1, -1))]]
    #observations_weight = [getLoss(0.4525), getLoss(0.4520), getLoss(0.4145), getLoss(0.4220)]

    observations_weight = [0.5142 * 10, 0.4821 * 10, 0.4680 * 10, 0.4734 * 10]
    observations_weight = Funct.normalize(dataset=observations_weight)
    print("weight: ", observations_weight)
    lstmConverter = LSTMConverter.LSTMConverter(
        cuda=True,
        max_layers=max_layers_lstm,
        mutation_list=mutations,
        limit_directions=observation_size)
    observations = []

    for i in range(num_actions):
        observation = Observation.Observation(
            path=directions_per_observations[i],
            weight=observations_weight[i],
            time=0)
        print("observation: ", i)
        print("path: ", observation.path)
        print("weight: ", observation.weight)
        observations.append(observation)

    input_lstm = lstmConverter.generate_LSTM_input(observations=observations)
    print("input size: ", input_lstm.size())
    current_path = Observation.Observation(path=[(1, (0, 1, 0, 0)),
                                                 (2, (0, -1, 0, 0))],
                                           weight=1,
                                           time=0)
    network = nw_lstm.NetworkLSTM(observation_size=observation_size,
                                  in_channels=max_layers_lstm,
                                  out_channels=max_layers_lstm *
                                  lstmConverter.mutations,
                                  kernel_size=lstmConverter.mutations,
                                  cuda_flag=True)

    print("empezando entrenamiento")
    network.Training(data=input_lstm,
                     dt=0.001,
                     p=5000,
                     observations=observations)
    print("entrenamiento finalizado")

    predict = lstmConverter.generate_LSTM_predict(observation=current_path)
    predicted = network.predict(predict)
    print("ht: ")
    print(predicted)
    predicted_directions = lstmConverter.topK_predicted_directions(
        predicted_tensor=predicted, k=num_actions // 2)
    print(predicted_directions)

    i += 1
Exemplo n.º 17
0
def is_convex(DNA, index):
    g = Fun.DNA2graph(DNA)
    node = g.key2node.get(index)
    output = node.objects[0][1] == max(
        [node.objects[0][2] for node in node.parents])
    return output