def torneio(self):
     candidato_1 = self.populacao[randint(0, self.n_pop)]
     candidato_2 = self.populacao[randint(0, self.n_pop)]
     if candidato_1.fitness <= candidato_2.fitness:
         self.pais.append(deep_copy(candidato_1))
     else:
         self.pais.append(deep_copy(candidato_2))
 def cruzamento(self):
     for i in range(0, self.n_pop, 2):
         if randfloat() <= self.pc:
             filho_1 = deep_copy(self.pais[i])
             filho_2 = deep_copy(self.pais[i+1])
             filho_1.cruzamento(filho_2)
             self.filhos.append(filho_1)
             self.filhos.append(filho_2)
         else:
             self.filhos.append(deep_copy(self.pais[i]))
             self.filhos.append(deep_copy(self.pais[i+1]))
 def remain_pop(self):
     # sort everybody for the rank and crowd distance of individuals
     population = []
     for front in self.frontiers:
         if len(front) + len(population) > self.pop_size:
             population = self.crowd_distance(population=population, front=front)
             break
         for i in front:
             population.append(deep_copy(self.everybody[i]))
         if len(population) == self.pop_size:
             break
     self.population = deep_copy(population)
Example #4
0
 def selection(self):
     self.fathers = []
     for i in range(self.pop_size):
         u = self.population[random.randint(0, self.pop_size - 1)]
         v = self.population[random.randint(0, self.pop_size - 1)]
         if u.dominates(other=v):
             self.fathers.append(deep_copy(u))
         elif v.dominates(other=u):
             self.fathers.append(deep_copy(v))
         else:
             r = random.random()
             if r >= 0.5:
                 self.fathers.append(deep_copy(u))
             else:
                 self.fathers.append(deep_copy(v))
Example #5
0
 def cross_over(self):
     self.sons = []
     for i in range(self.pop_size):  # create empty sons
         ind = Individual(nvar=self.n_var,
                          nbits=self.bits_per_variable,
                          ncal=self.n_cal)
         self.sons.append(deep_copy(ind))
     for i in range(0, self.pop_size,
                    2):  # cross over and fill their binary code
         for j in range(self.n_var):
             r = random.randint(1, self.bits_per_variable)
             gray_code_father_1 = gc.bin_to_gray(
                 self.fathers[i].binary_code[j])
             gray_code_father_2 = gc.bin_to_gray(
                 self.fathers[i + 1].binary_code[j])
             gray_code_son_1 = [
                 gray_code_father_1[0:r], gray_code_father_2[r:]
             ]
             gray_code_son_1 = ''.join(gray_code_son_1)
             gray_code_son_2 = [
                 gray_code_father_2[0:r], gray_code_father_1[r:]
             ]
             gray_code_son_2 = ''.join(gray_code_son_2)
             self.sons[i].binary_code.append(
                 gc.gray_to_bin(gray_code_son_1))
             self.sons[i + 1].binary_code.append(
                 gc.gray_to_bin(gray_code_son_2))
Example #6
0
  def add_relation(self, source, target, kind, label=None, metadata=None):
    """Adds a relation to the context graph."""
    assert utilities.valid_string(source) and utilities.valid_string(target)
    assert utilities.valid_string(kind)
    assert utilities.valid_optional_string(label)
    assert (metadata is None) or isinstance(metadata, types.DictType)

    with self._lock:
      # The timestamp of the relation should be inherited from the previous
      # context graph.
      key = (source, target, kind)
      timestamp = self._previous_relations_to_timestamps.get(key)
      if not utilities.valid_string(timestamp):
        timestamp = utilities.now()

      # Add the relation to the context graph data structure.
      relation = {
          'source': source,
          'target': target,
          'type': kind,
          'timestamp': timestamp
      }
      self._current_relations_to_timestamps[key] = timestamp

      # Add annotations as needed.
      relation['annotations'] = {}
      if metadata is not None:
        relation['annotations']['metadata'] = copy.deep_copy(metadata)

      relation['annotations']['label'] = label if label is not None else kind
      if self._version is not None:
        relation['annotations']['createdBy'] = self._version

      self._context_relations.append(relation)
 def initialize_population(self):
     for i in range(self.pop_size):
         ind = Individual(nvar=self.n_var, nbits=self.bits_per_variable, ncal=self.n_cal)
         self.population.append(deep_copy(ind))  # define it as object
         self.population[i].create_individual()  # create binary string
         self.population[i].binary_to_real(self.population[i].binary_code)  # decript
         self.population[i].calc_fitness()  # set fitness
Example #8
0
def _authenticate(cloud):
    """Authenticate to a cloud

    Ideally we would use shade.openstack_cloud or
    os_client_config.make_shade (equivalent), but they don't seem to
    support passing in an arbitrary dict of configuration parameters
    (the docs are not very clear ATM).

    To work around this, we write a temporary file with the
    appropriate configuration and tell os-client-config to load it.
    """
    CLOUD_NAME = 'default'

    tmp = tempfile.NamedTemporaryFile()
    try:
        cfg = {'clouds': {CLOUD_NAME: cloud}}
        y = yaml.safe_dump(cfg)
        tmp.write(y)
        tmp.seek(0)

        old_env = copy.deep_copy(os.environ)

        try:
            os.environ['OS_CLIENT_CONFIG_FILE'] = tmp.name
            auth = os_client_config.make_shade()
            return auth
        finally:
            os.environ = old_env

    finally:
        tmp.close()
Example #9
0
  def add_relation(self, source, target, kind, label=None, metadata=None):
    """Adds a relation to the context graph."""
    assert utilities.valid_string(source) and utilities.valid_string(target)
    assert utilities.valid_string(kind)
    assert utilities.valid_optional_string(label)
    assert (metadata is None) or isinstance(metadata, dict)

    with self._lock:
      # The timestamp of the relation should be inherited from the previous
      # context graph.
      key = (source, target, kind)
      timestamp = self._previous_relations_to_timestamps.get(key)
      if not utilities.valid_string(timestamp):
        timestamp = utilities.now()

      # Add the relation to the context graph data structure.
      relation = {
          'source': source,
          'target': target,
          'type': kind,
          'timestamp': timestamp
      }
      self._current_relations_to_timestamps[key] = timestamp

      # Add annotations as needed.
      relation['annotations'] = {}
      if metadata is not None:
        relation['annotations']['metadata'] = copy.deep_copy(metadata)
      relation['annotations']['label'] = label if label is not None else kind

      self._context_relations.append(relation)
Example #10
0
    def deposits(self) -> Iterable[Deposit]:
        """Create a dict representation of this deposit suitable for being converted to JSON."""
        if self.template is None:
            raise TypeError(
                "cannot generate JSON for a gradient without a template deposit"
            )

        for step_index in range(0, self.step_count + 1):
            percent = step_index / self.step_count
            deposit = deep_copy(self.template)
            deposit.deposit_name += f"-{self.name}_step{step_index}"

            for sweep in self.sweeps:
                sweep.percent = percent
                if sweep.name == "center_height":
                    sweep.assign(deposit.distribution)
                elif sweep.name == "cluster_size":
                    sweep.assign(deposit)
                elif sweep.name == "max_height":
                    sweep.assign(deposit.distribution)
                elif sweep.name == "min_height":
                    sweep.assign(deposit.distribution)
                elif sweep.name == "purity":
                    sweep.assign(deposit.vein, do_round=False)
                else:
                    raise NotImplementedError(
                        f"no known sweep of {sweep.name}")

            yield deposit
Example #11
0
 def _lookup_efs(self, ms_items):
     for ms_item in ms_items:
         for s in ms_item.sents:
             s.pred_entity_fillers.clear()
             for one_ef in s.entity_fillers:
                 copied_ef = deep_copy(one_ef)
                 copied_ef.links.clear()  # clean links
                 s.pred_entity_fillers.append(copied_ef)
 def roleta(self):
     giro_da_roleta = randfloat(0, self.soma_roleta)
     aux = 0
     i = -1
     while aux < giro_da_roleta:
         aux += 1/self.populacao[i].fitness
         i += 1
     self.pais.append(deep_copy(self.populacao[i]))
Example #13
0
 def update_best(self, g_best_value, g_best_position):
     if self.type_PSO == 'Global':
         for i in range(self.swarm_size):
             if self.fitness[i] < g_best_value:
                 g_best_value = deep_copy(self.fitness[i])
                 g_best_position = deep_copy(self.swarm[i])
     elif self.type_PSO == 'Local':
         aux_g_best_value = deep_copy(g_best_value)
         aux_1 = -1
         aux_2 = 1
         for i in range(self.swarm_size):
             if aux_2 >= self.swarm_size:
                 aux_2 = 0
             vector = [
                 aux_g_best_value[aux_1], self.fitness[i],
                 aux_g_best_value[aux_2]
             ]
             g_best_value[i] = deep_copy(np.min(vector))
             if np.argmin(vector) == 0:
                 g_best_position[i] = deep_copy(g_best_position[aux_1])
             elif np.argmin(vector) == 1:
                 g_best_position[i] = deep_copy(self.swarm[i])
             elif np.argmin(vector) == 2:
                 g_best_position[i] = deep_copy(g_best_position[aux_2])
             aux_1 += 1
             aux_2 += 1
     return g_best_value, g_best_position
Example #14
0
def mutacao(y, fitness_aux, prob, max_v, min_v, bic_score, nodes, nao_dag):
    for val in range(len(y)):
        r = random.random()
        if r <= prob:
            valor_mut = deep_copy(y[val])
            valor_mut_antigo = deep_copy(y[val])
            while (valor_mut == y[val]):
                valor_mut = min_v + random.randint(min_v, max_v)
            y[val] = valor_mut
            if y not in nao_dag:
                G = vetor_Rede(y, nodes)
                if G:
                    fitness_aux = abs(bic_score.score(G))
                else:
                    nao_dag.append(y)
                    y[val] = valor_mut_antigo
            else:
                y[val] = valor_mut_antigo
    return y, fitness_aux
Example #15
0
    def fromPipeline(cls, pipeline: pipelineIR.PipelineIR) -> Pipeline:
        """Create a new pipeline by copying an already existing `Pipeline`.

        Parameters
        ----------
        pipeline: `Pipeline`
            An already created pipeline intermediate representation object

        Returns
        -------
        pipeline: `Pipeline`
        """
        return cls.fromIR(copy.deep_copy(pipeline._pipelineIR))
Example #16
0
def move_rectangle2(rect, dx, dy):
    """A version of move_rectangle that creates and returns a new Rectangle 
    instead of modifying the old one.
    
    """
    # copy both rect and the Point object representing its corner
    new_rect = copy.deep_copy(rect)

    # modify the Point coords
    new_rect.corner.x += dx
    new_rect.corner.y += dy

    return new_rect
def move_rectangle2(rect, dx, dy):
    """A version of move_rectangle that creates and returns a new Rectangle 
    instead of modifying the old one.
    
    """
    # copy both rect and the Point object representing its corner
    new_rect = copy.deep_copy(rect)
    
    # modify the Point coords
    new_rect.corner.x += dx
    new_rect.corner.y += dy
    
    return new_rect
    def init_fisher(self, Fishers, par_prior_variance=None, pars={}):
        self.Npars = {}
        self.pars = {}
        for i in Fishers.keys():
            self.Npars[i] = len(Fishers[i])
            self.pars[i] = list(pars[i])

        if par_prior_variance is None:
            self.Fishers = Fishers
        else:
            self.Fishers_0prior = copy.deep_copy(Fishers)
            self.Fisher_prior = np.diag(1. / np.array(par_prior_variance))
            for i in Fishers.keys():
                self.Fishers[i] = self.Fisher_0prior[i] + self.Fisher_prior
Example #19
0
    def convert_definition(self, conversion_fun):
        """
        Convert a trajectory definition line-by-line using a conversion function.

        Trajectory definition of type 'direct' and 'interp' can be converted, but
        waveform trajectory definitions cannot.

        Parameters
        ----------
        conversion_fun : function
            Conversion function taking in one waypoint (list) and returning waypoint (list)

        Raises
        ------
        RuntimeError
            If the trajectory definition is not set
        RuntimeError
            If the trajectory type is incompatible (not direct or interp)
        """
        if self.definition is None:
            raise RuntimeError(
                "Trajectory definition has not been set or loaded.")

        if not (self.traj_type in ["direct", "interp"]):
            raise RuntimeError("Incompatible trajectory type: %s" %
                               (self.traj_type))

        def_new = copy.deepcopy(self.definition)
        setpoints = copy.deep_copy(self.definition['config']['setpoints'])
        setpoints_new = {}
        for traj_segment_key in setpoints:

            traj_segment = setpoints[traj_segment_key]

            # If the trajectory segment is empty, pass that along
            if traj_segment is None:
                setpoints_new[traj_segment_key] = None
                continue

            # If the trajectory has lines, convert them
            setpoints_new[traj_segment_key] = []
            for line in traj_segment:
                setpoints_new[traj_segment_key].append(conversion_fun(line))

        setpoints_new['meta'] = {'converted': True}
        def_new['config']['setpoints'] = setpoints_new

        self.definition = def_new
        self.build_traj()
Example #20
0
 def _copy_sent(sent, cur_event):
     ret = shallow_copy(sent)
     ret.events = []
     ret.pred_events = []
     if cur_event is not None:
         # only one event for center
         ret.events.append(
             cur_event)  # for events, use the original one
         copied_event = deep_copy(
             cur_event)  # for pred, use the copied one
         copied_event.links.clear(
         )  # and clear links(args) for prediction
         ret.pred_events.append(copied_event)
     ret.entity_fillers = shallow_copy(
         ret.entity_fillers)  # for training
     ret.pred_entity_fillers = []  # to predict
     ret.orig_sent = sent  # used in prediction to append back to the original instances
     return ret
 def __init__(self, nvar, ncal):
     # Atributos
     self.n_var = nvar
     self.n_cal = ncal
     self.pc = 0.7
     self.pm = 0.005
     self.n_pop = int(np.sqrt(self.n_cal))
     if self.n_pop > 200:
         self.n_pop = 200
     if self.n_pop % 2 == 1:
         self.n_pop = self.n_pop - 1
     self.n_iter = self.n_cal//self.n_pop
     self.populacao = []
     self.pais = []
     self.filhos = []
     self.problema = Rastringin(self.n_var)
     self.fitness = []
     self.variaveis = []             # gambs para plotar
     # Auxiliares
     self.media_fitness_pop = []
     self.fitness_plot = []
     self.geracoes = []
     # Inicialização a população
     for i in range(self.n_pop):
         ind = self.problema.tipo_individuo()
         ind.inicializar_parametros(n_var=self.n_var, tipo_funcao=self.problema)
         ind.criar_individuo()
         ind.calc_fitness()
         self.variaveis.append(ind.variaveis)
         self.fitness.append(ind.fitness)
         self.populacao.append(ind)
     # Definir Best em geração 0
     self.best = deep_copy(self.populacao[0])
     self.salvar_elite()
     # salvar valores para plotar
     self.media_fitness_pop.append(np.mean(self.fitness))
     self.fitness_plot.append(self.best.fitness)
     self.geracoes.append(0)
     # Evolução
     self.evoluir()
Example #22
0
 def batch_train(self, train, train_targets, test, test_targets, batch_size, partition_sizes = [0.7, 0.2, 0.1], epochs = 150, plot_training_results = True, shuffle=True, verbose = False):
 #Batch training method. Picks a random sized batch. Calculates the average value of n vectors with average targets.
 # Trains the MLP on the batch.
     #We begin to randomize the vectors.
     training_accuracy = np.zeros(epochs)
     test_accuracy = np.zeros(epochs)
     num = len(train)//batch_size
     rest = len(train)%batch_size
     flag = (rest != 0)
     for k in range(epochs):
         if(shuffle):
             p = np.random.permutation(len(train))
             train = train[p]
             train_targets = train_targets[p]
         for i in range(num):
             inp = train[batch_size*i:batch_size*(i+1)]
             target = train_targets[batch_size*i:batch_size*(i+1)]            
             # batch_inputs = np.mean(inp, axis= 0)
             # batch_targets = np.mean(target, axis= 0)
             self.backpropagation_multiple_layers_batch(inp, target, batch_size)           
         #Finally the last batch.    
         if(flag):
             inp = train[batch_size*(i+1):batch_size*(i+2)]
             target = train_targets[batch_size*(i+1):batch_size*(i+2)]       
             # batch_inputs = np.average(inp, axis= 0)
             # batch_targets = np.average(target, axis= 0)    
             self.backpropagation_multiple_layers_batch(inp, target, batch_size)  
         training_accuracy[k] = self.loss_function(train, train_targets)
         test_accuracy[k] = self.loss_function(test, test_targets)
         if(test_accuracy[k] > self.best_accuracy):
             #Keep a copy of the best performing network
             self.best_accuracy = test_accuracy[k]
             self.best_network = copy.deep_copy(self.network)
             self.best_epoch = k
         if(verbose):
             print("Iteration %i, loss = %f" % (k, test_accuracy[k]))    
     if(plot_training_results):
         self.plot_training_progress(training_accuracy, test_accuracy)     
     return(training_accuracy, test_accuracy)
Example #23
0
def map_merge(subject, other, match_fn, *args):
    '''Given two lists of dictionaries, merge the dictionaries.
    Dictionaries are merged if the `match_fn` applied to the single elements,
    returns true.
    Additional arguments are passed to the match function.
    '''

    try:
        match_fn = MATCHERS[match_fn]
    except KeyError:
        raise AnsibleFilterError('Unknown match function')

    subject = list(subject)
    other = list(other)
    matched = []
    result = []

    for subject_item in subject:
        for other_item in other:
            if match_fn(subject_item, other_item, *args):
                # Keep track of matched items.
                matched.append(subject_item)
                matched.append(other_item)
                # Compute the resulting item
                # (merging subject item with other item).
                result_item = deep_copy(subject_item)
                result_item.update(other_item)
                result.append(result_item)

    # Add items that never matched, without modifying them.
    for item in subject + other:
        if len([m for m in matched if item is m]) == 0:
            # This item never matched.
            result.append(item)

    return result
Example #24
0
def map_merge(subject, other, match_fn, *args):
    '''Given two lists of dictionaries, merge the dictionaries.
    Dictionaries are merged if the `match_fn` applied to the single elements,
    returns true.
    Additional arguments are passed to the match function.
    '''

    try:
        match_fn = MATCHERS[match_fn]
    except KeyError:
        raise AnsibleFilterError('Unknown match function')

    subject = list(subject)
    other   = list(other)
    matched = []
    result  = []

    for subject_item in subject:
        for other_item in other:
            if match_fn(subject_item, other_item, *args):
                # Keep track of matched items.
                matched.append(subject_item)
                matched.append(other_item)
                # Compute the resulting item
                # (merging subject item with other item).
                result_item = deep_copy(subject_item)
                result_item.update(other_item)
                result.append(result_item)

    # Add items that never matched, without modifying them.
    for item in subject + other:
        if len([m for m in matched if item is m]) == 0:
            # This item never matched.
            result.append(item)

    return result
Example #25
0
 def initialize_population(self):  # ok!
     '''# ________________________________________________________________________________________________________
     # gambs to add old run as the new population
     old_results = pd.read_csv('newpop.csv', header=None)
     # end gambs =============================================================================================='''
     for i in range(self.pop_size):
         ind = Individual(ncal=self.n_cal)
         self.population.append(deep_copy(ind))  # define it as object
         '''# ____________________________________________________________________________________________________
         # gambs to add old run as the new population
         self.population[i].variables = np.array(old_results.iloc[i])
         self.population[i].calc_fitness()
         # end gambs =========================================================================================='''
         self.population[i].create_individual(
         )  # create string of variables
         self.population[i].calc_fitness()  # set fitness
     #
     # inset the strong individuals
     self.population[-3].variables = np.zeros([500]) + 1
     self.population[-3].calc_fitness()
     self.population[-2].variables = np.zeros([500]) + 2
     self.population[-2].calc_fitness()
     self.population[-1].variables = np.zeros([500])
     self.population[-1].calc_fitness()
Example #26
0
 def create_new_population(self):  # Creates the union of fathers and sons
     self.everybody = []
     for i in range(0, self.pop_size):
         self.everybody.append(deep_copy(self.population[i]))
     for i in range(0, self.pop_size):
         self.everybody.append(deep_copy(self.sons[i]))
 def salvar_elite(self):
     for i in range(self.n_pop):
         if self.populacao[i].fitness < self.best.fitness:
             self.best = deep_copy(self.populacao[i])
Example #28
0
 def return_cards_to_dealer(self):
     old_hand = copy.deep_copy(self.hand) 
     self.hand.clear()
     return old_hand
 def criar_nova_pop(self):
     self.populacao = deep_copy(self.filhos)
     self.populacao[np.argmax(self.fitness)] = self.best
Example #30
0
 def get_parameter_from(self, high_score_worker):  # for exploit
     self.trainer.hyper_parameter = deep_copy(
         high_score_worker.trainer.hyper_parameter)
     self.trainer.net = deep_copy(high_score_worker.trainer.net)
Example #31
0
def annealing(maxsteps=1000, debug=True):
    """ Optimize the black-box function 'cost_function' with the simulated annealing algorithm."""
    #Ler data
    with open('Asia.csv') as csv_file:
        csv_reader = csv.reader(csv_file, delimiter=',')
        aux = 0
        data = []
        data1 = [[] for i in range(8)]
        for row in csv_reader:
            data.append(row)
            for i in range(len(row)):
                data1[i].append(row[i])
            aux = aux + 1
            if aux == 50001:
                break

    data = {}
    for i in range(len(data1)):
        data[data1[i][0]] = [data1[i][j] for j in range(1, len(data1[i]))]
    data = pd.DataFrame(data)
    print("Data: ")
    print(data)  #Dados Retirandos do arquivo
    prob = 0.5
    min_valor = 0
    max_valor = 2
    nao_dag = []
    nodes = ['Pollution', 'Smoker', 'Cancer', 'Xray', 'Dyspnoea']
    nodes = ['asia', 'tub', 'smoke', 'lung', 'bronc', 'either', 'xray', 'dysp']
    ind_size = round((len(nodes) * len(nodes) - len(nodes)) / 2)
    ind = False
    while ind == False:
        aux = [random.randint(min_valor, max_valor) for i in range(ind_size)]
        if aux not in nao_dag:
            G = vetor_Rede(aux, nodes)
            if G:
                state = deep_copy(aux)
                ind = True
            else:
                nao_dag.append(aux)
    print('state')
    print(state)
    bic_score = BicScore(data)
    print(vetor_Rede(state, nodes))
    cost = cost_function(state, bic_score, nodes)
    states, costs = [state], [cost]
    for step in range(maxsteps):
        print(step)
        fraction = step / float(maxsteps)
        T = temperature(fraction)
        #[new_state,new_cost]=pertubacao(deep_copy(state),deep_copy(cost),prob,max_valor,min_valor,bic_score,nodes,nao_dag)
        [new_state,
         new_cost] = mutacao(deep_copy(state), deep_copy(cost), prob,
                             max_valor, min_valor, bic_score, nodes, nao_dag)
        #new_cost = cost_function(new_state,bic_score,nodes)
        #if debug: print("Step #{:>2}/{:>2} : T = {:>4.3g}, state = {:>4.3g}, cost = {:>4.3g}, new_state = {:>4.3g}, new_cost = {:>4.3g} ...".format(step, maxsteps, T, state, cost, new_state, new_cost))

        if acceptance_probability(cost, new_cost, T) > random.random():

            state1 = new_state.copy()
            cost = deep_copy(new_cost)

            states.append(state1)
            costs.append(cost)
            state = deep_copy(state1)
            # print("  ==> Accept it!")
        # else:
        #    print("  ==> Reject it...")
    return state, cost_function(state, bic_score, nodes), states, costs
Example #32
0
 def clone(self) -> object:
     return copy.deep_copy(self)
'passwd'.center(50, '-')
dir(str)
dir()
import utils
help(super)
l = [['peter', 'tim', 'kimberly'], ['la', 'ma', 'ca']]
b_l = l 
del(l[1])
b_l
#shallow copy 
id(l)
id(b_l)
l
b = l
import copy
dl = copy.deep_copy(l)
dl = copy.deepcopy(l)
l
import copy
dl = copy.deepcopy(l)
id(l)
id(dl)
dl
l
l.append('peter')
l
dl
import filecmp
help(filecmp.cmp)
import shutils
import shutil
Example #34
0
 def insertions(self):
     return copy.deep_copy(self.text)