Skip to content
This repository has been archived by the owner on Aug 3, 2024. It is now read-only.

Commit

Permalink
fixed recipe recursion algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
dvalnn committed May 29, 2024
1 parent 37eab9a commit 8d73c88
Showing 1 changed file with 20 additions and 65 deletions.
85 changes: 20 additions & 65 deletions src/scheduler/handlers/order_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,77 +22,32 @@ pub fn gen_items(
Ok(items)
}

// #[async_recursion]
// pub(crate) async fn get_full_recipe(
// piece: PieceKind,
// pool: &sqlx::PgPool,
// ) -> Result<Vec<Recipe>> {
// let mut product = piece;
// let mut full_recipe = Vec::new();
//
// loop {
// let recipes = Recipe::get_by_product(product, pool).await?;
// if recipes.is_empty() {
// tracing::warn!("Full recipe: {:?}", full_recipe);
// return Ok(full_recipe);
// };
//
// tracing::warn!("Recipes for product{:?}: {:?}", product, recipes);
//
// if recipes.len() == 1 {
// product = recipes[0].material_kind;
// tracing::warn!("Adding recipe to full recipe: {:?}", recipes[0]);
// full_recipe.push(recipes[0].clone());
// continue;
// }
//
// let mut subrecipes = Vec::new();
// let n_recursions = recipes.len();
// for (idx, recipe) in recipes.clone().into_iter().enumerate() {
// tracing::warn!("RECURSING {}/{}", idx, n_recursions);
// let subrecipe = get_full_recipe(recipe.material_kind, pool).await?;
// subrecipes.push(subrecipe);
// }
//
// let Some(mut best) = subrecipes
// .into_iter()
// .enumerate()
// .min_by_key(|r| r.1.iter().map(|r| r.operation_time).sum::<i64>())
// else {
// tracing::warn!(
// "Full recipe returning from place 2: {:?}",
// full_recipe
// );
// return Ok(full_recipe);
// };
//
// best.1.extend(vec![recipes[best.0].clone()]);
// product = best.1[0].material_kind;
// full_recipe.extend(best.1);
// }
// }

#[async_recursion]
pub(crate) async fn get_full_recipe(
piece: PieceKind,
pool: &sqlx::PgPool,
) -> Result<Vec<Recipe>> {
let mut product = piece;
let mut full_recipe = Vec::new();
let recipes = Recipe::get_by_product(piece, pool).await?;
if recipes.is_empty() {
return Ok(vec![]);
}

let mut possible_paths = Vec::new();
for recipe in recipes {
let subrecipe = get_full_recipe(recipe.material_kind, pool).await?;
let mut recipe_path = vec![recipe];
recipe_path.extend(subrecipe);
possible_paths.push(recipe_path);
}

loop {
let recipes = Recipe::get_by_product(product, pool).await?;
if recipes.is_empty() {
return Ok(full_recipe);
};
let best = possible_paths
.into_iter()
.min_by_key(|r| r.iter().map(|r| r.operation_time).sum::<i64>());

//TODO: implement a better heuristic
let fastest = match recipes.into_iter().min_by_key(|r| r.operation_time)
{
Some(f) => f,
None => anyhow::bail!("No recipe found for product {:?}", product),
};
tracing::debug!("Best full recipe for piece {:?}: {:?}", piece, best);

product = fastest.material_kind;
full_recipe.push(fastest);
match best {
Some(b) => Ok(b),
None => unreachable!(),
}
}

0 comments on commit 8d73c88

Please sign in to comment.