Commit 31076031 authored by SINDT--BARET Yanis's avatar SINDT--BARET Yanis
Browse files

[Update] New name and automatization of ID generator

parent 7c9ffdef
import json
import subprocess
import os
import random
def clone_basic_file(index: int, path: str) -> None:
"""Function to clone a basic file with a new seed.
Args:
index (int): Index of the hike
path (str): Path to a basic file to clone
"""
with open(path, "r") as file:
data_json = json.load(file)
data_json["seed"] = random.randint(1, 1000000)
old_path_splited = path.split(".json")
new_path = f"{old_path_splited[0]}_{index}.json"
with open(new_path, "w") as result_file:
json.dump(data_json, result_file)
def change_resultjson_seed(index: int, result_id: str, path="") -> None:
"""Function to clone a result.json file (so a DNA sequence) with a new seed.
Args:
index (int): Index of the hike
result_id (str): ID chosen
path (str, optional): Path to the result file, if not located in the working directory. Defaults to "".
"""
with open(f"{path}result.json", "r") as file:
data_json = json.load(file)
data_json["seed"] = random.randint(1, 1000000)
with open(f"{path}result_{result_id}_{index}.json", "w") as result_file:
json.dump(data_json, result_file)
if __name__ == "__main__":
"""Program to do multiple experiment.
It takes a basic_file to clone it in X times (corresponding to X different sequence)
And do X hikes with these differents genomes.
It creates 4 differents type of file :
basic_{hike}.json : basic file that give some basic necessary information like the seed to create the gene
result_{ID}_{hike}.json : result file which content the sequence with a new seed to do mutations.
sswm_evol_result_{number of generation}{result file name linked}_{probability of inversion}_{y if it allow neutral mutation else n}_{hikes}.csv :
csv file with 4 columns : | Generation | Fitness selected at the previous generation | New fitness | Type of mutation at this generation |
"""
load_basic: bool = input("Do we need to create all basic files ? (y/n) : ")
nb_hikes = int(
input("How many hikes do you want to do ? : ")
) # Todo: Try exept if wrong input
inversion_rate = input("Which probability of inversion ? : ")
nb_gen = int(input("How many generation ? : "))
is_neutral_mut = input("Do you allow neutral mutation ? (y/n) : ").lower()
temp = str(inversion_rate).split(".")
result_id: str = temp[0] + "00" if len(temp) == 1 else temp[0] + temp[2] + "0"
os.chdir(os.getcwd())
if load_basic == "y":
random.seed(2)
for i in range(1, nb_hikes + 1):
clone_basic_file(i, "basic.json")
random.seed(3)
for i in range(1, nb_hikes + 1):
res_a = subprocess.run(
args=["../../build/bin/module_create", f"basic_{i}.json"],
stdout=subprocess.DEVNULL,
)
change_resultjson_seed(i, result_id)
res_b = subprocess.run(
args=[
"../../build/bin/sswm",
f"result_{result_id}_{i}.json",
str(inversion_rate),
str(nb_gen),
str(is_neutral_mut),
str(i),
]
)
if res_b.returncode == 0:
print("\n --- \n Le programme s'est exécuté sans erreur")
else:
print("\n --- \n Une erreur est survenue")
print(f"\n{i}")
......@@ -5,6 +5,7 @@
#include "json.hpp"
#include "JumpingMT.h"
#include "MutationFactorySswm.h"
#include "Metadata.h"
#include <iomanip>
#include <iostream>
......@@ -14,6 +15,7 @@
using namespace aevol;
using json = nlohmann::json;
#ifdef BASE_4
#define NB_BASE 4
constexpr double ERROR_MARGIN = 1E4;
......@@ -160,7 +162,7 @@ int main(int argc, char* argv[]) { // USAGE: program GENE_JSON_FILE MUTATION_RAT
underscore +
extension;
std::ofstream output_evol_file(result_evol_file);
output_evol_file << " nb_gen,non_mutant,mutant,mutation" << std::endl;
output_evol_file << " nb_gen,non_mutant,mutant,mutation,nb_coding_arn" << std::endl;
// Création de la fabrique de mutation, la seed permet d'initialiser le
......@@ -179,12 +181,20 @@ int main(int argc, char* argv[]) { // USAGE: program GENE_JSON_FILE MUTATION_RAT
new_indiv->evaluate_after_mutation(target, p.w_max,
p.selection_pressure, fuzzyFac);
// print_step(i, individual, new_indiv); Si on veut print les infos
auto prot_list = individual->metadata_->proteins_;
int nb = 0;
for (const auto& seq : prot_list) {
if (seq->is_functional) {
nb++;
}
}
if (individual->fitness < new_indiv->fitness) {
delete individual;
individual = new_indiv;
output_evol_file << i << "," << individual->fitness << ","
<< new_indiv->fitness << "," << mutationEvent.type()
<< "," << nb
<< std::endl;
good_mut++;
}
......@@ -193,12 +203,14 @@ int main(int argc, char* argv[]) { // USAGE: program GENE_JSON_FILE MUTATION_RAT
individual = new_indiv;
output_evol_file << i << "," << individual->fitness << ","
<< new_indiv->fitness << "," << mutationEvent.type()
<< "," << nb
<< std::endl;
neutral_mut++;
}
else {
output_evol_file << i << "," << individual->fitness << ","
<< new_indiv->fitness << "," << mutationEvent.type()
<< "," << nb
<< std::endl;
delete new_indiv;
new_indiv = nullptr;
......@@ -206,10 +218,10 @@ int main(int argc, char* argv[]) { // USAGE: program GENE_JSON_FILE MUTATION_RAT
if (i % 5000 == 0) {
printf("--- Generation %d done ---\n", i);
}
else if (i == nb_gen-1) {
output_evol_file << nb_gen << "," << individual->fitness << ","
<< individual->fitness << "," << mutationEvent.type()
<< std::endl;
if (i == nb_gen) {
output_evol_file << i << "," << individual->fitness << ","
<< new_indiv->fitness << "," << mutationEvent.type()
<< "," << nb << std::endl;
}
}
std::string result(individual->dna_->data());
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment