initial_code

This commit is contained in:
Spectre 2024-10-09 03:51:35 +02:00
parent a57debd087
commit 393b3395a5
14 changed files with 2808 additions and 0 deletions

19
.direnv/bin/nix-direnv-reload Executable file
View file

@ -0,0 +1,19 @@
#!/usr/bin/env bash
set -e
if [[ ! -d "/home/spectre/code/path_of_exile" ]]; then
echo "Cannot find source directory; Did you move it?"
echo "(Looking for "/home/spectre/code/path_of_exile")"
echo 'Cannot force reload with this script - use "direnv reload" manually and then try again'
exit 1
fi
# rebuild the cache forcefully
_nix_direnv_force_reload=1 direnv exec "/home/spectre/code/path_of_exile" true
# Update the mtime for .envrc.
# This will cause direnv to reload again - but without re-building.
touch "/home/spectre/code/path_of_exile/.envrc"
# Also update the timestamp of whatever profile_rc we have.
# This makes sure that we know we are up to date.
touch -r "/home/spectre/code/path_of_exile/.envrc" "/home/spectre/code/path_of_exile/.direnv"/*.rc

View file

@ -0,0 +1 @@
/nix/store/42c5i4afkf4qcq3ssn06s5awm844dv9q-source

View file

@ -0,0 +1 @@
/nix/store/d17n12f96zm6ycv9wkkmql31av8ar928-source

View file

@ -0,0 +1 @@
/nix/store/lryfc8mhk1czqsa421di2y5nzz5c3b8m-source

View file

@ -0,0 +1 @@
/nix/store/mapfkjj4mxiv2ibv3ra8ch4hcjw16wm1-nix-shell-env

File diff suppressed because it is too large Load diff

1
.envrc Normal file
View file

@ -0,0 +1 @@
use flake

1
crop_rot/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

327
crop_rot/Cargo.lock generated Normal file
View file

@ -0,0 +1,327 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "autocfg"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
[[package]]
name = "byteorder"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "crop_rot"
version = "0.1.0"
dependencies = [
"rand",
"rand_distr",
"rayon",
"serde",
"toml",
]
[[package]]
name = "crossbeam-deque"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "613f8cc01fe9cf1a3eb3d7f488fd2fa8388403e97039e2f73692932e291a770d"
dependencies = [
"crossbeam-epoch",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e"
dependencies = [
"crossbeam-utils",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80"
[[package]]
name = "either"
version = "1.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0"
[[package]]
name = "equivalent"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
[[package]]
name = "getrandom"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "hashbrown"
version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e087f84d4f86bf4b218b927129862374b72199ae7d8657835f1e89000eea4fb"
[[package]]
name = "indexmap"
version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "707907fe3c25f5424cce2cb7e1cbcafee6bdbe735ca90ef77c29e84591e5b9da"
dependencies = [
"equivalent",
"hashbrown",
]
[[package]]
name = "libc"
version = "0.2.159"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "561d97a539a36e26a9a5fad1ea11a3039a67714694aaa379433e580854bc3dc5"
[[package]]
name = "libm"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ec2a862134d2a7d32d7983ddcdd1c4923530833c9f2ea1a44fc5fa473989058"
[[package]]
name = "memchr"
version = "2.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
[[package]]
name = "num-traits"
version = "0.2.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
dependencies = [
"autocfg",
"libm",
]
[[package]]
name = "ppv-lite86"
version = "0.2.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04"
dependencies = [
"zerocopy",
]
[[package]]
name = "proc-macro2"
version = "1.0.87"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3e4daa0dcf6feba26f985457cdf104d4b4256fc5a09547140f3631bb076b19a"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af"
dependencies = [
"proc-macro2",
]
[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom",
]
[[package]]
name = "rand_distr"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32cb0b9bc82b0a0876c2dd994a7e7a2683d3e7390ca40e6886785ef0c7e3ee31"
dependencies = [
"num-traits",
"rand",
]
[[package]]
name = "rayon"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa"
dependencies = [
"either",
"rayon-core",
]
[[package]]
name = "rayon-core"
version = "1.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2"
dependencies = [
"crossbeam-deque",
"crossbeam-utils",
]
[[package]]
name = "serde"
version = "1.0.210"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c8e3592472072e6e22e0a54d5904d9febf8508f65fb8552499a1abc7d1078c3a"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.210"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "243902eda00fad750862fc144cea25caca5e20d615af0a81bee94ca738f1df1f"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_spanned"
version = "0.6.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87607cb1398ed59d48732e575a4c28a7a8ebf2454b964fe3f224f2afc07909e1"
dependencies = [
"serde",
]
[[package]]
name = "syn"
version = "2.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89132cd0bf050864e1d38dc3bbc07a0eb8e7530af26344d3d2bbbef83499f590"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "toml"
version = "0.8.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1ed1f98e3fdc28d6d910e6737ae6ab1a93bf1985935a1193e68f93eeb68d24e"
dependencies = [
"serde",
"serde_spanned",
"toml_datetime",
"toml_edit",
]
[[package]]
name = "toml_datetime"
version = "0.6.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0dd7358ecb8fc2f8d014bf86f6f638ce72ba252a2c3a2572f2a795f1d23efb41"
dependencies = [
"serde",
]
[[package]]
name = "toml_edit"
version = "0.22.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ae48d6208a266e853d946088ed816055e556cc6028c5e8e2b84d9fa5dd7c7f5"
dependencies = [
"indexmap",
"serde",
"serde_spanned",
"toml_datetime",
"winnow",
]
[[package]]
name = "unicode-ident"
version = "1.0.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe"
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "winnow"
version = "0.6.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "36c1fec1a2bb5866f07c25f68c26e565c4c200aebb96d7e55710c19d3e8ac49b"
dependencies = [
"memchr",
]
[[package]]
name = "zerocopy"
version = "0.7.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0"
dependencies = [
"byteorder",
"zerocopy-derive",
]
[[package]]
name = "zerocopy-derive"
version = "0.7.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e"
dependencies = [
"proc-macro2",
"quote",
"syn",
]

11
crop_rot/Cargo.toml Normal file
View file

@ -0,0 +1,11 @@
[package]
name = "crop_rot"
version = "0.1.0"
edition = "2021"
[dependencies]
rand = "0.8.5"
rand_distr = "0.4.3"
rayon = "1.10.0"
serde = { version = "1.0.210", features=["derive"] }
toml = "0.8.19"

20
crop_rot/config.toml Normal file
View file

@ -0,0 +1,20 @@
expected_lifeforce = [3.220000000000082, 11.733855400000088, 70.33212248000017, 162.32843617999993, 275.6750786199999, 402.55435975999916, 536.7185175399989, 673.7040835000004, 811.9261996200031, 948.5246767200055]
[lifeforce_value]
yellow = 2.5
blue = 1.0
red = 1.0
[seed_lifeforce]
t1 = 0.14
t2 = 1.62
t3 = 47.0
t4 = 230.0
[map_mods]
pack_size = 1.0
[seed_upgrade_chance]
t1 = 0.25
t2 = 0.2
t3 = 0.03

75
crop_rot/src/main.rs Normal file
View file

@ -0,0 +1,75 @@
// mod harvest;
pub mod model;
pub mod weights;
use model::*;
use weights::{ColorWeights, Config};
use std::fs;
use rayon::prelude::*;
fn main() {
let config = parse_config("config.toml").unwrap();
let mut res: Vec<(ColorWeights, Vec<f64>)> = Vec::new();
for color in gen_possible_color_weights().into_iter() {
let val = run_sim(100, &config, &color);
println!("{:?} {:?}", color, val);
res.push((color, val));
}
println!("{}", res);
// for i in 0..10 {
// let res = lifeforce_after_upgrades(i, 1000000, &config);
// println!("{i}: {res}");
// }
}
fn parse_config(path: &str) -> Result<Config, Box<dyn std::error::Error>> {
let file = fs::read_to_string(path)?;
let config: Config = toml::from_str(&file)?;
Ok(config)
}
fn run_sim(n: usize, config: &Config, color_weight: &ColorWeights) -> Vec<f64> {
(0..n)
.into_par_iter()
.map(|_| Harvest::<Field>::new(color_weight, config).recursive_harvest(config))
.collect()
}
fn gen_possible_color_weights() -> Vec<ColorWeights> {
let allowed = [0.55, 0.65, 0.75, 0.80, 0.90, 1.00];
let mut all: Vec<ColorWeights> = Vec::new();
for &yellow in &allowed {
for &blue in &allowed {
for &red in &allowed {
if yellow == 0.55 || blue == 0.55 || red == 0.55 {
all.push(ColorWeights { yellow, blue, red });
}
}
}
}
all
}
fn lifeforce_after_upgrades(n: usize, tries: usize, config: &Config) -> f64 {
(0..tries)
.into_par_iter()
.map(|_| {
let mut field: Field = Field { color: weights::Color::Yellow, wilted: false, t1_seeds: 23, t2_seeds: 0, t3_seeds: 0, t4_seeds: 0};
for i in 0..n {
field.upgrade(config);
}
field.harvest(config).unwrap()
}).sum::<f64>() / (tries as f64)
}

194
crop_rot/src/model.rs Normal file
View file

@ -0,0 +1,194 @@
use std::fmt::{self, Debug, Display};
use rand::prelude::*;
use crate::weights::{Config, ColorWeights, Color};
#[derive(Debug, Clone)]
pub struct Field {
pub color: Color,
pub wilted: bool,
pub t1_seeds: i8,
pub t2_seeds: i8,
pub t3_seeds: i8,
pub t4_seeds: i8,
}
#[derive(Debug, Clone)]
pub struct FakeField {
color: Color,
wilted: bool,
upgrades: usize,
}
pub trait Harvestable {
fn new(color_weights: &ColorWeights) -> Self;
fn new_ungenerated() -> Self;
fn harvest(&mut self, config: &Config) -> Result<f64, &'static str>;
fn upgrade(&mut self, config: &Config);
fn wilt(&mut self);
fn is_wilted(&self) -> bool;
}
#[derive(Debug, Clone)]
pub struct Harvest<T: Harvestable> {
fields: [T; 10],
value: f64,
}
impl Harvestable for Field {
fn new(color_weights: &ColorWeights) -> Self {
Field { color: color_weights.color(), wilted: false, t1_seeds: 23, t2_seeds: 0, t3_seeds: 0, t4_seeds: 0 }
}
fn new_ungenerated() -> Self {
Field { color: Color::NotGenerated, wilted: true, t1_seeds: 0, t2_seeds: 0, t3_seeds: 0, t4_seeds: 0 }
}
fn harvest(&mut self, config: &Config) -> Result<f64, &'static str> {
match self.wilted {
false => {
self.wilted = true;
Ok(self.t1_seeds as f64 * config.seed_lifeforce.t1 + self.t2_seeds as f64 * config.seed_lifeforce.t2 + self.t3_seeds as f64 * config.seed_lifeforce.t3 + self.t4_seeds as f64 * config.seed_lifeforce.t4)
},
true => Err("cannot harvest wilted crop")
}
}
fn upgrade(&mut self, config: &Config) {
if !self.wilted {
let t2 = config.seed_upgrade_chance.t1_to_t2(self.t1_seeds);
let t3 = config.seed_upgrade_chance.t2_to_t3(self.t2_seeds);
let t4 = config.seed_upgrade_chance.t3_to_t4(self.t3_seeds);
self.t1_seeds += - t2;
self.t2_seeds += t2 - t3;
self.t3_seeds += t3 - t4;
self.t4_seeds += t4;
}
}
fn wilt(&mut self) {
self.wilted = true;
}
fn is_wilted(&self) -> bool {
self.wilted
}
}
impl Harvestable for FakeField {
fn new(color_weights: &ColorWeights) -> FakeField {
FakeField { color: color_weights.color(), wilted: false, upgrades: 0 }
}
fn new_ungenerated() -> Self {
FakeField {color: Color::NotGenerated, wilted: true, upgrades: 0}
}
fn harvest(&mut self, config: &Config) -> Result<f64, &'static str> {
match self.wilted {
false => {
self.wilted = true;
Ok(config.expected_lifeforce[self.upgrades] * config.lifeforce_value.from_color(&self.color))
}
true => Err("cannot harvest wilted crop"),
}
}
fn upgrade(&mut self, config: &Config) {
if !self.wilted { self.upgrades += 1; }
}
fn wilt(&mut self) {
self.wilted = true;
}
fn is_wilted(&self) -> bool {
self.wilted
}
}
impl<T> Harvest<T> where
T: Harvestable + Debug + Clone + Display
{
pub fn new(color_weights: &ColorWeights, config: &Config) -> Harvest<T> {
let rolled_fields: usize = rand::distributions::Uniform::new(3, 6).sample(&mut thread_rng()) * 2;
let mut fields: Vec<T> = Vec::new();
for _ in 0..rolled_fields {
fields.push(T::new(color_weights));
}
for _ in rolled_fields..10 {
fields.push(T::new_ungenerated())
}
Harvest { value: 0.0, fields: fields.try_into().unwrap() }
}
pub fn harvest(&mut self, n: usize, config: &Config) -> Result<f64, &'static str> {
match self.fields[n].harvest(config) {
Err(x) => Err(x),
Ok(gained_val) => {
self.value += gained_val;
let neighbor = n + 1 - n % 2;
if thread_rng().gen_bool(0.4) {self.fields[neighbor].wilt();}
self.fields.iter_mut().for_each(|field| field.upgrade(config));
Ok(gained_val)
}
}
}
pub fn recursive_harvest(self, config: &Config) -> f64 {
if self.fields.iter().all(|field| field.is_wilted()) {
return self.value;
} else {
return self.fields.iter().enumerate().filter(|(_, field)| !field.is_wilted()).map(|(i, field)| {
let mut clone = self.clone();
clone.harvest(i, config);
clone.recursive_harvest(config)
}).reduce(f64::max).unwrap()
}
}
}
impl Display for Field {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.wilted {
false => write!(f, "{}:{}:{}:{}:{}", match self.color {
Color::Yellow => "Y",
Color::Blue => "B",
Color::Red => "R",
Color::NotGenerated => "N/A"
}, self.t1_seeds, self.t2_seeds, self.t3_seeds, self.t4_seeds),
true => write!(f, "Wilted")
}
}
}
impl Display for FakeField {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Up:{}", self.upgrades)
}
}
impl<T: fmt::Display + Harvestable> fmt::Display for Harvest<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for (i, field) in self.fields.iter().enumerate() {
write!(f, "{}", field)?;
if i % 2 == 0 {
write!(f, " <-> ")?;
}
else { write!(f, " ")?; }
}
write!(f, "")
}
}

107
crop_rot/src/weights.rs Normal file
View file

@ -0,0 +1,107 @@
use rand::prelude::Distribution;
use rand_distr::Binomial;
use rand::distributions::WeightedIndex;
use rand::thread_rng;
use serde::Deserialize;
#[derive(Debug, Deserialize)]
pub struct UpgradeProbabilities {
pub t1_t2: f64,
pub t2_t3: f64,
pub t3_t4: f64,
}
#[derive(Debug, Deserialize)]
pub struct LifeforceValue {
pub yellow: f64,
pub blue: f64,
pub red: f64,
}
impl LifeforceValue {
pub fn from_color(&self, color: &Color) -> f64 {
match color {
Color::Yellow => self.yellow,
Color::Blue => self.blue,
Color::Red => self.red,
_ => 0.0
}
}
}
#[derive(Debug, Deserialize)]
pub struct SeedLifeforce {
pub t1: f64,
pub t2: f64,
pub t3: f64,
pub t4: f64,
}
#[derive(Debug, Deserialize)]
pub struct MapMods {
pub pack_size: f64,
}
#[derive(Debug, Deserialize)]
pub struct Weights {
pub upgrade: UpgradeProbabilities,
pub value: LifeforceValue,
pub lifeforce: SeedLifeforce,
pub map_mods: MapMods,
}
#[derive(Debug, Deserialize)]
pub struct SeedUpgradeChances {
pub t1: f64,
pub t2: f64,
pub t3: f64,
}
impl SeedUpgradeChances {
pub fn t1_to_t2(&self, n: i8) -> i8 {
Binomial::new(n as u64, self.t1).unwrap().sample(&mut thread_rng()) as i8
}
pub fn t2_to_t3(&self, n: i8) -> i8 {
Binomial::new(n as u64, self.t2).unwrap().sample(&mut thread_rng()) as i8
}
pub fn t3_to_t4(&self, n: i8) -> i8 {
Binomial::new(n as u64, self.t3).unwrap().sample(&mut thread_rng()) as i8
}
}
#[derive(Debug, Deserialize)]
pub struct Config {
pub lifeforce_value: LifeforceValue,
pub seed_lifeforce: SeedLifeforce,
pub map_mods: MapMods,
pub seed_upgrade_chance: SeedUpgradeChances,
pub expected_lifeforce: [f64; 10],
}
#[derive(Debug, Clone)]
pub enum Color {
Yellow,
Blue,
Red,
NotGenerated
}
#[derive(Debug)]
pub struct ColorWeights {
pub yellow: f64,
pub blue: f64,
pub red: f64,
}
impl ColorWeights {
pub fn color(&self) -> Color {
match WeightedIndex::new([self.yellow, self.blue, self.red]).unwrap().sample(&mut thread_rng()) {
0 => Color::Yellow,
1 => Color::Blue,
2 => Color::Red,
_ => unreachable!(),
}
}
}