feat: able to give start_parameters from file

This commit is contained in:
Ahmet Kaan GÜMÜŞ 2024-11-20 00:20:40 +03:00
parent 7d54ed4795
commit 930223c0eb
3 changed files with 77 additions and 4 deletions

View file

@ -0,0 +1,8 @@
[start_parameters]
decision_variable_count = 3
food_source_number = 100
food_source_try_limit = 100
upper_bound = 5
lower_bound = -5
iteration = 1000
run = 5

View file

@ -1,4 +1,8 @@
use std::io;
use std::{
env,
fs::File,
io::{self, BufRead, BufReader},
};
pub mod bee;
pub mod food;
@ -14,6 +18,19 @@ pub struct Input {
}
impl Input {
pub fn get() -> Self {
let args = env::args().collect::<Vec<String>>();
if args.len() > 1 {
if args[1] == "-f" || args[1] == "--file" {
Input::read_config_file_input()
} else {
Input::user_interactive_input()
}
} else {
Input::user_interactive_input()
}
}
fn user_interactive_input() -> Self {
println!("Decision Variable Count");
let decision_variable_count = Self::get_input().parse().unwrap();
@ -44,6 +61,53 @@ impl Input {
run,
}
}
fn read_config_file_input() -> Input {
let mut config_file_input = Self {
decision_variable_count: 0,
food_source_number: 0,
food_source_try_limit: 0,
upper_bound: 0.0,
lower_bound: 0.0,
iteration: 0,
run: 0,
};
let config_file = File::open("abc.toml").unwrap();
let reader = BufReader::new(config_file);
let mut words = vec![];
for line in reader.lines().map(|x| x.unwrap()).collect::<Vec<String>>() {
let mut parsed = line
.split('=')
.map(|x| x.trim().to_string())
.collect::<Vec<String>>();
words.append(&mut parsed);
}
for i in 0..words.len() {
if i % 2 == 0 {
continue;
}
match words[i].as_str() {
"decision_variable_count" => {
config_file_input.decision_variable_count = words[i + 1].parse().unwrap()
}
"food_source_number" => {
config_file_input.food_source_number = words[i + 1].parse().unwrap()
}
"food_source_try_limit" => {
config_file_input.food_source_try_limit = words[i + 1].parse().unwrap()
}
"upper_bound" => config_file_input.upper_bound = words[i + 1].parse().unwrap(),
"lower_bound" => config_file_input.lower_bound = words[i + 1].parse().unwrap(),
"iteration" => config_file_input.iteration = words[i + 1].parse().unwrap(),
"run" => config_file_input.run = words[i + 1].parse().unwrap(),
_ => {}
}
}
config_file_input
}
fn get_input() -> String {
let mut input = String::new();
io::stdin().read_line(&mut input).unwrap();

View file

@ -11,11 +11,11 @@ fn main() {
input.lower_bound,
);
for _ in 0..input.run {
for run_counter in 0..input.run {
let mut best = FoodSource::get(vec![]);
for food_source in &food_sources {
if best.fitness < food_source.fitness {
best.coordinates = food_source.coordinates.clone();
best.coordinates.clone_from(&food_source.coordinates);
best.fitness = food_source.fitness;
}
}
@ -57,7 +57,8 @@ fn main() {
);
}
}
println!("-------------------------------");
println!("\n\t|Run {}|\n", run_counter);
println!("{:#?}", best);
}
}