speed control

This commit is contained in:
sparshg 2023-01-09 02:10:34 +05:30
parent 31be57ab37
commit 7110d063b4
8 changed files with 25 additions and 22 deletions

View File

Before

Width:  |  Height:  |  Size: 256 B

After

Width:  |  Height:  |  Size: 256 B

View File

Before

Width:  |  Height:  |  Size: 113 B

After

Width:  |  Height:  |  Size: 113 B

View File

Before

Width:  |  Height:  |  Size: 245 B

After

Width:  |  Height:  |  Size: 245 B

View File

Before

Width:  |  Height:  |  Size: 398 B

After

Width:  |  Height:  |  Size: 398 B

BIN
assets/slow.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 262 B

View File

@ -29,10 +29,11 @@ fn window_conf() -> Conf {
#[macroquad::main(window_conf)] #[macroquad::main(window_conf)]
async fn main() { async fn main() {
rand::srand(macroquad::miniquad::date::now() as _); rand::srand(macroquad::miniquad::date::now() as _);
let pause = load_texture("pause.png").await.unwrap(); let pause = load_texture("assets/pause.png").await.unwrap();
let play = load_texture("play.png").await.unwrap(); let play = load_texture("assets/play.png").await.unwrap();
let fast = load_texture("fast.png").await.unwrap(); let fast = load_texture("assets/fast.png").await.unwrap();
let restart = load_texture("restart.png").await.unwrap(); let slow = load_texture("assets/slow.png").await.unwrap();
let restart = load_texture("assets/restart.png").await.unwrap();
let th = (screen_height() - HEIGHT) * 0.5; let th = (screen_height() - HEIGHT) * 0.5;
let gamecam = Camera2D { let gamecam = Camera2D {
@ -57,10 +58,9 @@ async fn main() {
..Default::default() ..Default::default()
}; };
let mut speedup = false; let mut speedup = 1;
let mut paused = false; let mut paused = false;
let mut bias = false; let mut bias = false;
let mut showall = false;
let mut size = 100; let mut size = 100;
let mut pop = Population::new(size as usize); let mut pop = Population::new(size as usize);
@ -127,20 +127,15 @@ async fn main() {
clear_background(BLACK); clear_background(BLACK);
set_camera(&gamecam); set_camera(&gamecam);
if is_key_pressed(KeyCode::S) { if is_key_pressed(KeyCode::S) {
speedup = !speedup; speedup = (speedup * 10) % 9999;
println!("Speedup: {}", speedup);
} }
if speedup { if !paused {
if !paused { for _ in 0..speedup {
for _ in 0..1000 {
pop.update();
}
}
} else {
if !paused {
pop.update(); pop.update();
} }
pop.draw();
} }
pop.draw();
draw_rectangle_lines(-WIDTH * 0.5, -HEIGHT * 0.5, WIDTH, HEIGHT, 2., WHITE); draw_rectangle_lines(-WIDTH * 0.5, -HEIGHT * 0.5, WIDTH, HEIGHT, 2., WHITE);
draw_rectangle_lines( draw_rectangle_lines(
WIDTH * 0.5 + th, WIDTH * 0.5 + th,
@ -182,9 +177,9 @@ async fn main() {
.position(Vec2::new(0., 0.)) .position(Vec2::new(0., 0.))
.ui(ui, |ui| { .ui(ui, |ui| {
ui.label(None, &format!("Generation: {}", pop.gen)); ui.label(None, &format!("Generation: {}", pop.gen));
ui.same_line(314.); ui.same_line(242.);
if widgets::Button::new("Load Model").ui(ui) { if widgets::Button::new("Load Model").ui(ui) {
if let Some(path) = open_file_dialog("Load Model", "brain.json", None) { if let Some(path) = open_file_dialog("Load Model", "model.json", None) {
let brain = NN::import(&path); let brain = NN::import(&path);
size = 1; size = 1;
pop = Population::new(1); pop = Population::new(1);
@ -193,13 +188,21 @@ async fn main() {
} }
ui.same_line(0.); ui.same_line(0.);
if widgets::Button::new("Save Model").ui(ui) { if widgets::Button::new("Save Model").ui(ui) {
if let Some(path) = save_file_dialog("Save Model", "brain.json") { if let Some(path) = save_file_dialog("Save Model", "model.json") {
pop.worlds[0].export_brain(&path); pop.worlds[0].export_brain(&path);
} }
} }
ui.same_line(0.); ui.same_line(0.);
if widgets::Button::new(slow).ui(ui) {
speedup = std::cmp::max(speedup / 10, 1);
};
ui.same_line(0.);
if widgets::Button::new("1x").ui(ui) {
speedup = 1;
};
ui.same_line(0.);
if widgets::Button::new(fast).ui(ui) { if widgets::Button::new(fast).ui(ui) {
speedup = !speedup; speedup = std::cmp::min(speedup * 10, 1000);
}; };
ui.same_line(0.); ui.same_line(0.);
if widgets::Button::new(if paused { play } else { pause }).ui(ui) { if widgets::Button::new(if paused { play } else { pause }).ui(ui) {

View File

@ -99,7 +99,7 @@ impl NN {
let width = width * 0.8; let width = width * 0.8;
let height = height * 0.8; let height = height * 0.8;
let vspace = height / (self.config.iter().max().unwrap() - 1) as f32; let vspace = height / (self.config.iter().max().unwrap() - 1) as f32;
let mut p1s: Vec<(f32, f32)> = Vec::new(); let mut p1s: Vec<(f32, f32)>;
let mut p2s: Vec<(f32, f32)> = Vec::new(); let mut p2s: Vec<(f32, f32)> = Vec::new();
for (i, layer) in self for (i, layer) in self
.config .config

View File

@ -1,4 +1,4 @@
use std::{f32::consts::PI, f64::consts::TAU, iter}; use std::{f32::consts::PI, f64::consts::TAU};
use macroquad::{prelude::*, rand::gen_range}; use macroquad::{prelude::*, rand::gen_range};