Skip to content

Commit

Permalink
Factor out genetic algorithm harness
Browse files Browse the repository at this point in the history
  • Loading branch information
t4ccer committed Dec 19, 2023
1 parent 2a84839 commit 94a9c0a
Show file tree
Hide file tree
Showing 9 changed files with 378 additions and 277 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ serde_repr = { version = "0.1.12", optional = true}
cgt-derive = { path = "cgt-derive"}
rayon = {version = "1.7.0", optional = true}
dashmap = { version = "5.5.3", features = ["inline"] }
rand = "0.8.5"

[dev-dependencies]
quickcheck = { version = "1.0", default-features = false }
Expand Down
69 changes: 69 additions & 0 deletions cgt-cli/src/io.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use std::{
fs::File,
io::{self, stderr, Stderr},
};

#[derive(Debug, Clone)]
pub enum FileOrStderr {
FilePath(String),
Stderr,
}

impl From<String> for FileOrStderr {
fn from(value: String) -> Self {
if &value == "-" {
Self::Stderr
} else {
Self::FilePath(value)
}
}
}

impl FileOrStderr {
fn create_with<'a, F>(&'a self, f: F) -> io::Result<FileOrStderrWriter>
where
F: FnOnce(&'a str) -> io::Result<File>,
{
match self {
FileOrStderr::FilePath(ref fp) => Ok(FileOrStderrWriter::File(f(fp)?)),
FileOrStderr::Stderr => Ok(FileOrStderrWriter::Stderr(stderr())),
}
}

#[allow(dead_code)]
pub fn open(&self) -> io::Result<FileOrStderrWriter> {
self.create_with(File::open)
}

#[allow(dead_code)]
pub fn create(&self) -> io::Result<FileOrStderrWriter> {
self.create_with(File::create)
}
}

pub enum FileOrStderrWriter {
File(File),
Stderr(Stderr),
}

impl io::Write for FileOrStderrWriter {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
match self {
FileOrStderrWriter::File(f) => f.write(buf),
FileOrStderrWriter::Stderr(stderr) => {
let mut lock = stderr.lock();
lock.write(buf)
}
}
}

fn flush(&mut self) -> io::Result<()> {
match self {
FileOrStderrWriter::File(f) => f.flush(),
FileOrStderrWriter::Stderr(stderr) => {
let mut lock = stderr.lock();
lock.flush()
}
}
}
}
1 change: 1 addition & 0 deletions cgt-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod amazons;
mod anyhow_utils;
mod canonical_form;
mod domineering;
mod io;
mod quicksort;
mod snort;
mod wind_up;
Expand Down
23 changes: 6 additions & 17 deletions cgt-cli/src/snort/common.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anyhow::{bail, Context, Result};
use cgt::{
genetic_algorithm::Scored,
graph::undirected::Graph,
numeric::{dyadic_rational_number::DyadicRationalNumber, rational::Rational},
short::partizan::{
Expand All @@ -22,28 +23,13 @@ pub enum Log {
temperature: DyadicRationalNumber,
},
HighFitness {
position: Scored,
position: Scored<Snort, Rational>,
canonical_form: String,
temperature: DyadicRationalNumber,
degree: usize,
},
}

#[derive(Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub struct Scored {
pub position: Snort,
pub score: Rational,
}

impl Scored {
pub fn without_score(position: Snort) -> Self {
Scored {
position,
score: Rational::NegativeInfinity,
}
}
}

#[derive(Debug, Clone)]
pub struct Edge {
pub from: u32,
Expand Down Expand Up @@ -129,7 +115,10 @@ pub fn analyze_position(position: Snort) -> Result<()> {
let score = temperature.to_rational() - Rational::from(degree as i32);

let log = Log::HighFitness {
position: Scored { position, score },
position: Scored {
object: position,
score,
},
canonical_form: canonical_form.to_string(),
temperature,
degree,
Expand Down
Loading

0 comments on commit 94a9c0a

Please sign in to comment.