Compare commits

...

2 Commits

Author SHA1 Message Date
134ba50999 added to json function 2024-05-02 10:11:53 +02:00
ff3f3c12bf corrected sample count 2024-05-02 08:29:40 +02:00
5 changed files with 51 additions and 8 deletions

1
.gitignore vendored
View File

@@ -1,2 +1,3 @@
/target
/Cargo.lock
*/output.json

View File

@@ -7,3 +7,14 @@ edition = "2021"
[dependencies.rand]
version = "0.8.5"
[dependencies.serde]
version = "1.0.200"
features = ["derive"]
[dependencies.axum]
version = "0.7.5"
features = ["json"]
[dependencies.serde_json]
version = "1.0.116"

View File

@@ -42,7 +42,7 @@ impl Function {
threads.push(std::thread::spawn(move || {
let mut hits: u64 = 0;
for _ in 0..samples {
for _ in 0..samples/thread_cnt {
let point: Point = bounds.get_random_point();
if function.includes(&point) {
hits += 1;
@@ -52,10 +52,17 @@ impl Function {
}))
}
for _ in 0..samples % thread_cnt {
let point: Point = bounds.get_random_point();
if function.includes(&point) {
hits += 1;
}
}
for thread in threads {
hits += thread.join().unwrap();
}
return (hits as f64 / (thread_cnt * samples) as f64) * bounds.get_area();
return (hits as f64 / samples as f64) * bounds.get_area();
}
}

View File

@@ -1,3 +1,6 @@
use std::fs::File;
use std::io;
use std::io::Write;
use crate::bounds::Bounds;
use crate::function::Function;
use crate::linear_bounds::LinearBounds;
@@ -15,14 +18,14 @@ fn main() {
let bounds: Bounds = Bounds::new(LinearBounds::new(0_f64, 20_f64), LinearBounds::new(-100_f64, 150000_f64));
let func: Function = Function::new();
let sample_limit: usize = 1_000;
let sample_limit: usize = 1_000_000_000;
let sub_samples: Vec<usize> = vec![1, 2, 5];
let samples_per_iteration: usize = 10;
let samples_per_iteration: usize = 1;
let mut result: Plot = Plot::new();
let mut sample_cnt: usize = 1;
while sample_cnt <= sample_limit {
while sample_cnt < sample_limit {
for sub_sample in sub_samples.clone() {
let samples: usize = sub_sample * sample_cnt;
for _ in 0..samples_per_iteration {
@@ -32,4 +35,11 @@ fn main() {
sample_cnt *= 10;
}
result.print();
let mut file: io::Result<File> = File::create("output.json");
if file.is_err() {
panic!("Error creating output file")
}
if let Err(_) = file.unwrap().write(result.to_json().to_string().as_bytes()) { panic!("Error writing to file") };
}

View File

@@ -1,6 +1,9 @@
use std::iter::zip;
use axum::extract::FromRef;
use serde::Serialize;
use serde_json::{json, Value};
#[derive(Debug)]
#[derive(Debug, Serialize)]
pub struct Plot {
x: Vec<usize>,
y: Vec<Vec<f64>>,
@@ -21,9 +24,20 @@ impl Plot {
}
}
pub fn print(self) -> () {
for (x, y) in zip(self.x, self.y) {
pub fn print(&self) -> () {
let plot: Plot = Plot::from_ref(self);
for (x, y) in zip(plot.x, plot.y) {
println!("{}: {:?}", x, y);
}
}
pub fn to_json(&self) -> Value {
return json!(self);
}
}
impl Clone for Plot {
fn clone(&self) -> Self {
return Plot {x: self.x.clone(), y: self.y.clone()}
}
}