Skip to content

Commit

Permalink
feat: config impl
Browse files Browse the repository at this point in the history
  • Loading branch information
Young-TW committed Oct 17, 2024
1 parent ceb581d commit 2427032
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 5 deletions.
102 changes: 102 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ edition = "2021"

[dependencies]
rand = "0.8.4"
serde = { version = "1.0", features = ["derive"] }
toml = "0.8.19"
File renamed without changes.
47 changes: 47 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use serde::Deserialize;
use std::fs;
use toml;

#[derive(Debug, Deserialize)]
pub struct Config {
pub problem: Problem,
pub algorithm: Algorithm,
pub test: Test,
}

#[derive(Debug, Deserialize)]
pub struct Problem {
pub size: i32,
}

#[derive(Debug, Deserialize)]
pub struct Algorithm {
pub max_gen: i32,
pub n_neighbors: i32,
}

#[derive(Debug, Deserialize)]
pub struct Test {
pub count: i32,
}

impl Default for Config {
fn default() -> Self {
Config {
problem: Problem { size: 20 },
algorithm: Algorithm {
max_gen: 1000,
n_neighbors: 10,
},
test: Test { count: 100 },
}
}
}

impl Config {
pub fn get() -> Result<Config, Box<dyn std::error::Error>> {
let config_content = fs::read_to_string("config.toml")?;
let config: Config = toml::from_str(&config_content)?;
Ok(config)
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod case;
pub mod config;
pub mod debug;
pub mod qts;
pub mod quantum;
Expand Down
14 changes: 9 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use qts_rust::case::case_1;
use qts_rust::config::Config;
use qts_rust::qts::qts;
use qts_rust::types::{Items, Problem};
use std::sync::{Arc, Mutex};
use std::thread;

fn main() {
let test_times: i32 = 1000;
let max_gen: i32 = 1000;
let n_neighbors: i32 = 10;
let config = Config::get().unwrap();

let problem = Problem::default();
let items = Arc::new(Mutex::new(Vec::new()));
Expand All @@ -18,11 +17,16 @@ fn main() {

let mut handles = vec![];

for i in 0..test_times {
for i in 0..config.test.count {
let items = Arc::clone(&items);
let handle = thread::spawn(move || {
let items = items.lock().unwrap();
qts(&items, capacity, max_gen, n_neighbors);
qts(
&items,
capacity,
config.algorithm.max_gen,
config.algorithm.n_neighbors,
);
println!("thread[{}] done", i);
});
handles.push(handle);
Expand Down

0 comments on commit 2427032

Please sign in to comment.