Skip to content

Commit

Permalink
Working on database basics
Browse files Browse the repository at this point in the history
  • Loading branch information
maxfierrog committed Mar 6, 2024
1 parent 519d6e1 commit 0b6f048
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 28 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ debug = true

[dependencies]
clap = { version = "^4", features = ["derive"] }
exitcode = "^1"
serde_json = "^1"
regex = "^1"
exitcode = "^1"
nalgebra = "^0"
colored = "^2"
anyhow = "^1"
regex = "^1"
50 changes: 49 additions & 1 deletion src/database/engine/volatile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,52 @@
//! #### Authorship
//!
//! - Max Fierro, 2/24/2024 (maxfierro@berkeley.edu)
pub struct Database;

use anyhow::Result;

use std::collections::HashMap;

use crate::{
database::{object::schema::Schema, KVStore, Tabular},
model::State,
};

pub struct Database<'a> {
memory: HashMap<State, &'a [u8]>,
}

impl Database<'_> {
pub fn initialize() -> Self {
Self {
memory: HashMap::new(),
}
}
}

impl KVStore for Database<'_> {
fn put(&mut self, key: State, value: &[u8]) {
todo!()
}

fn get(&self, key: State) -> Option<&[u8]> {
todo!()
}

fn del(&self, key: State) {
todo!()
}
}

impl Tabular for Database<'_> {
fn create_table(&self, id: &str, schema: Schema) -> Result<()> {
todo!()
}

fn select_table(&self, id: &str) -> Result<()> {
todo!()
}

fn delete_table(&self, id: &str) -> Result<()> {
todo!()
}
}
6 changes: 4 additions & 2 deletions src/game/zero_by/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,12 @@ impl Game for Session {
fn solve(&self, mode: IOMode, method: SolutionMode) -> Result<()> {
match (self.players, method) {
(2, SolutionMode::Strong) => {
<Self as acyclic::DynamicSolver<2, State>>::solve(&self, mode)
acyclic::dynamic_solver::<2, Self>(self, mode)
.context("Failed solver run.")?
},
(10, SolutionMode::Strong) => {
<Self as acyclic::DynamicSolver<10, State>>::solve(&self, mode)
acyclic::dynamic_solver::<10, Self>(self, mode)
.context("Failed solver run.")?
},
_ => {
return Err(GameError::SolverNotFound {
Expand Down
2 changes: 0 additions & 2 deletions src/solver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,3 @@ pub mod weak {

pub mod error;
pub mod util;

/* SCHEMAS */
71 changes: 51 additions & 20 deletions src/solver/strong/acyclic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,50 @@
//!
//! - Max Fierro, 12/3/2023 (maxfierro@berkeley.edu)

use anyhow::{Context, Result};

use crate::database::engine::volatile;
use crate::game::{Acyclic, Bounded, DTransition, STransition};
use crate::database::object::schema::{Attribute, Datatype, SchemaBuilder};
use crate::database::{KVStore, Tabular};
use crate::game::{Acyclic, Bounded, DTransition, STransition, Solvable};
use crate::interface::IOMode;
use crate::model::{PlayerCount, State};
use crate::schema;
use crate::solver::MAX_TRANSITIONS;

/* SOLVERS */

pub fn dynamic<const N: usize, G>(game: &G, mode: IOMode)
pub fn dynamic_solver<const N: usize, G>(game: &G, mode: IOMode) -> Result<()>
where
G: Acyclic<N> + DTransition<State> + Bounded<State>
G: Acyclic<N> + DTransition<State> + Bounded<State> + Solvable<N>,
{
let mut db = volatile::Database::initialize();
dynamic_backward_induction(db, game);
let mut db = volatile_database(&game.id())
.context("Failed to initialize database.")?;

dynamic_backward_induction(&mut db, game);
Ok(())
}

pub fn static<const N: usize, G>(game: &G, mode: IOMode)
pub fn static_solver<const N: usize, G>(game: &G, mode: IOMode) -> Result<()>
where
G: Acyclic<N> + STransition<State, MAX_TRANSITIONS> + Bounded<State>
G: Acyclic<N>
+ STransition<State, MAX_TRANSITIONS>
+ Bounded<State>
+ Solvable<N>,
{
let mut db = volatile::Database::initialize();
static_backward_induction(db, game);
let mut db = volatile_database(&game.id())
.context("Failed to initialize database.")?;

static_backward_induction(&mut db, game);
Ok(())
}

/* SOLVING ALGORITHMS */

fn dynamic_backward_induction<const N: PlayerCount, G>(
db: &mut BPDatabase<N>,
game: &G,
) where
G: Acyclic<N> + Bounded<State> + DTransition<State>,
fn dynamic_backward_induction<const N: PlayerCount, G, D>(db: &mut D, game: &G)
where
G: Acyclic<N> + Bounded<State> + DTransition<State> + Solvable<N>,
D: KVStore,
{
let mut stack = Vec::new();
stack.push(game.start());
Expand Down Expand Up @@ -72,16 +85,18 @@ fn dynamic_backward_induction<const N: PlayerCount, G>(
}
}

fn static_backward_induction<const N: PlayerCount, G>(
db: &mut BPDatabase<N>,
game: &G,
) where
G: Acyclic<N> + STransition<State, MAX_TRANSITIONS> + Bounded<State>,
fn static_backward_induction<const N: PlayerCount, G, D>(db: &mut D, game: &G)
where
G: Acyclic<N>
+ STransition<State, MAX_TRANSITIONS>
+ Bounded<State>
+ Solvable<N>,
D: KVStore,
{
let mut stack = Vec::new();
stack.push(game.start());
while let Some(curr) = stack.pop() {
let children = game.transition(curr);
let children = game.prograde(curr);
if let None = db.get(curr) {
db.put(curr, Record::default());
if children
Expand Down Expand Up @@ -114,3 +129,19 @@ fn static_backward_induction<const N: PlayerCount, G>(
}
}
}

/* HELPERS */

fn volatile_database(table: &str) -> Result<volatile::Database> {
let schema = schema! {
"Player 1 Utility"; Datatype::SINT; 8,
"Player 2 Utility"; Datatype::SINT; 8,
"Total Remoteness"; Datatype::UINT; 8
};

let mut db = volatile::Database::initialize();
db.create_table(table, schema);
db.select_table(table);

Ok(db)
}
2 changes: 1 addition & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ macro_rules! collection {
///
/// let s2 = schema! {
/// "attribute3"; Datatype::UINT; 20,
/// "attribute4"; Datatype::SINT; 60,
/// "attribute4"; Datatype::SINT; 60
/// };
/// ```
///
Expand Down

0 comments on commit 0b6f048

Please sign in to comment.