def save_as_json(self, filename): """ Save the dictionary as a json file and also include the cid of the data itself in the json file :param filename: The filename of the json file """ self.save() data = self.get() data['cid'] = self._cid save_to_json_file(filename=filename, data=data) return self._cid
def initialize_api_keys_file(): """ Initialize the api_keys.json file with a new random api key and secret for the admin """ # Check to make sure the directory exists if not os.path.isdir('json/private/'): os.makedirs('json/private') # Create a random string of characters (uppercase letters and digits) for a api_key and api_secret pair api_key = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(16)) api_secret = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(16)) data = {api_key: {'secret': api_secret, 'permissions': 'all'}} save_to_json_file(API_KEYS_FILE, data)
def save_config(self, filename): config = { 'title': self.title, 'description': self.description, 'dir': self.dir, 'save_dir': self.save_dir, 'load_last_save': self.load_last_save, 'champions_dir': self.champions_dir, 'model_script': self.model_script, 'model_class': self.model_class, 'rosetta_stone_script': self.rosetta_stone_script, 'rosetta_stone_class': self.rosetta_stone_class, 'fitness_function_script': self.fitness_function_script, 'fitness_function_class': self.fitness_function_class, 'periodic_save': self.periodic_save, 'population_size': self.population_size, 'target_fitness': self.target_fitness, 'truncation': self.truncation, 'truncation_normalization': self.truncation_normalization, 'elitism': self.elitism, 'n_parents': self.n_parents, 'recombination_type': self.recombination_type, 'tournament_size': self.tournament_size, 'max_generations': self.max_generations, 'max_time_total': self.max_time_total, 'max_time_generation': self.max_time_generation, 'stagnation': self.stagnation, 'mutations': { 'boolean': self.boolean_mutation_chance.__dict__, 'integer': self.integer_mutation_chance.__dict__, 'float': self.float_mutation_chance.__dict__, 'string': self.string_mutation_chance.__dict__, 'chromosome': self.chromosome_mutation_chance.__dict__ } } save_to_json_file(filename=filename, data=config)
def save(self): """ Save the action as a json file """ save_to_json_file(os.path.join(ACTIONS_DIR, '%s.json' % self.id), self.json_encodable())
def start(self): population = Population() # Load the Model script model = self.load_script(script=self.model_script, script_class_name=self.model_class) if not isinstance(model, Model): raise Exception( 'Script %s is not a valid Model Script, instead it is a %s' % (model, type(model))) model.darwin_init_actions() model.info() # Load the RosettaStone script rosetta_stone = self.load_script( script=self.rosetta_stone_script, script_class_name=self.rosetta_stone_class) if not isinstance(rosetta_stone, RosettaStone): raise Exception( 'Script %s is not a valid RosettaStone Script, instead it is a %s' % (rosetta_stone, type(rosetta_stone))) # Load the FitnessFunction script fitness_function = self.load_script( script=self.fitness_function_script, script_class_name=self.fitness_function_class) fitness_function.darwin_init_actions() if not isinstance(fitness_function, FitnessFunction): raise Exception( 'Script %s is not a valid FitnessFunction Script, instead it is a %s' % (fitness_function, type(fitness_function))) if self.load_last_save is True: # Load saved generation population.load_directory(directory=self.job_dir) elif self.load_champions is True: # Load previous champions population.load_directory(directory=self.champions_dir) if len(population.genomes) < self.population_size: # Initialize all genomes in the population for i in range(self.population_size - len(population.genomes)): genome = rosetta_stone.genome_template() # Fill the genome with random data genome.init_with_random_data() population.add_genome(genome) champion = population.genomes[0] evolver_start_time = time.time() # Remove the progress file from earlier runs to start with a clean slate if os.path.isfile(self.progress_file): os.remove(self.progress_file) while not self.termination(): # Run any actions that need to happen before each generation model.pre_generation_actions() self.current_generation += 1 generation_start_time = time.time() print('\n\n=========================================') print('Calculating generation %s in %s' % (self.current_generation, self.title)) next_generation = Population() for j, genome in enumerate(population.genomes): # Translate the genome to the model model_template = rosetta_stone.genome_to_model(genome=genome) model.configure(config=model_template) genome.fitness = fitness_function.fitness(model=model).value print('Genome %s (%s): %s' % (j, genome.id(), genome.fitness)) if 0 < self.max_time_generation <= generation_start_time + self.max_time_generation: print( 'Generation is taking too long to calculate, skipping to next generation' ) break # Sort the population by highest fitness population.genomes = sorted(population.genomes, key=lambda x: -x.fitness) print('Best in generation: %s : %s' % (population.genomes[0].id(), population.genomes[0].fitness)) # Periodically save the current population as json files if self.periodic_save > 0 and self.current_generation % self.periodic_save == 0: population.save(directory=self.job_dir) # Truncate the population by removing the worst performing genomes so they don't have a chance to be selected for recombination population.genomes = population.genomes[:int( (len(population.genomes) * self.truncation) / 100)] if self.highest_fitness is None or population.genomes[ 0].fitness > self.highest_fitness or population.genomes[ 0].id() != champion.id(): self.highest_fitness = population.genomes[0].fitness champion = population.genomes[0] self.generations_since_new_champion = 0 else: self.generations_since_new_champion += 1 print('Highest fitness: %s' % self.highest_fitness) print('Generations since improvement: %s' % self.generations_since_new_champion) with open(self.progress_file, 'a') as output_file: output_file.write('%s;%s;%s;%s\n' % (int(time.time()), self.current_generation, self.highest_fitness, (time.time() - generation_start_time))) # Set a new random seed because some models might use a specific seed which could influence the evolution process random.seed(time.time()) # Calculate how many different genomes are left after truncation diversity = len(set([genome.id() for genome in population.genomes])) print('Diversity: %s different genomes' % diversity) # If there is not enough diversity, then evolution will slow down, so set a multiplier for the mutation chances that gets bigger if diversity is low # or when there hasn't been a new champion for many generations mutation_multiplier = len(population.genomes) / float( diversity) + self.generations_since_new_champion * 0.01 print('Mutation multiplier: %s' % mutation_multiplier) # Recombine the best genomes into the next generation for i in range(self.population_size - self.elitism): if self.recombination_type == 1: parent_a, parent_b = roulette_wheel_selection( genomes=population.genomes, n_parents=self.n_parents) elif self.recombination_type == 2: parent_a, parent_b = rank_selection( genomes=population.genomes, n_parents=self.n_parents) elif self.recombination_type == 3: parent_a, parent_b = stochastic_universal_sampling( genomes=population.genomes, n_parents=self.n_parents) elif self.recombination_type == 4: parent_a, parent_b = tournament_selection( genomes=population.genomes, n_parents=self.n_parents, tournament_size=self.tournament_size) else: raise NotImplementedError( 'Unknown recombination type: %s' % self.recombination_type) offspring = recombine(parent_a=parent_a, parent_b=parent_b) next_generation.add_genome(offspring) # Apply mutations to the next generation for genome in next_generation.genomes: for chromosome_id, chromosome in genome.chromosomes.items(): # Apply chromosome mutations chromosome.apply_mutations( mutation_chance=self.chromosome_mutation_chance, multiplier=mutation_multiplier) for gene in chromosome.genes: # Apply gene mutations if chromosome.encoding_type == EncodingType.BOOLEAN: gene.apply_mutations( mutation_chance=self.boolean_mutation_chance, multiplier=mutation_multiplier) elif chromosome.encoding_type == EncodingType.INTEGER: gene.apply_mutations( mutation_chance=self.integer_mutation_chance, multiplier=mutation_multiplier) elif chromosome.encoding_type == EncodingType.FLOAT: gene.apply_mutations( mutation_chance=self.float_mutation_chance, multiplier=mutation_multiplier) elif chromosome.encoding_type == EncodingType.STRING: gene.apply_mutations( mutation_chance=self.string_mutation_chance, multiplier=mutation_multiplier) else: raise NotImplementedError( 'Unknown encoding type: %s' % chromosome.encoding_type) # Copy the n genomes with highest fitness to the next generation if elitism is greater than 0, # these genomes will not be modified by mutations for i in range(self.elitism): if len(population.genomes) > i: next_generation.add_genome(genome=population.genomes[i]) # Copy the next generation to the current population for the next iteration of the loop population.genomes = next_generation.genomes self.elapsed_time = time.time() - evolver_start_time # Run any actions that need to happen after each generation model.post_generation_actions( champion=rosetta_stone.genome_to_model(champion)) # Print out the best solution print('\n\nBest solution:') print('--------------') print(champion.info()) model_template = rosetta_stone.genome_to_model(champion) pprint(model_template) print('\nFitness: %s' % champion.fitness) model.configure(config=model_template) model.champion_actions() champion_file = os.path.join(self.champions_dir, '%s.json' % champion.id()) save_to_json_file(filename=champion_file, data=champion.to_dict()) return champion.fitness
def save(self): save_to_json_file(os.path.join(TRIGGERS_DIR, '%s.json' % self.id), self.json_encodable())
def save(self): save_to_json_file(filename=os.path.join(PAYMENT_PROCESSOR_DIR, '%s.json' % self.payment_request_id), data=self.json_encodable())
get_address_from_wallet(account=bip44_account, index=bip44_index + 4): 35 } # Note: If you want to send a specific amount to each address, each value must be the amount in Satoshis and # the SendTransaction amount must be exactly the sum of all values, if there is any change leftover, the change will go back to the splitter-address # or to the change-address if one is specified # total_amount = 100000000 # distribute exactly 1 bitcoin across all recipients # change_address = get_address_from_wallet(account=bip44_account, index=bip44_index + 5) # Check to make sure the distribution is valid assert valid_distribution(distribution=distribution) # must save the distribution as a json file distribution_filename = os.path.join('splitter-distribution.json') save_to_json_file(filename=distribution_filename, data=distribution) ########################################################################################################## print('Setting up Splitter') print('----------------------------------------------\n') print('The address for the splitter is %s' % splitter_address) # -------------------------------------------------------------------------------------------------------- # Clean up old triggers and actions first # -------------------------------------------------------------------------------------------------------- clean_up_triggers(trigger_ids=['Splitter']) clean_up_actions(action_ids=['Splitter']) # --------------------------------------------------------------------------------------------------------
def save(self, directory): shutil.rmtree(directory) time.sleep(1) for genome in self.genomes: save_to_json_file(os.path.join(directory, '%s.json' % genome.id()), data=genome.to_dict())