From 93149d9570cd7d7a2ce73e71c144a1ea20c3b771 Mon Sep 17 00:00:00 2001 From: FiveMovesAhead Date: Mon, 1 Dec 2025 15:21:17 +0000 Subject: [PATCH] Submitted knapsack/dynamic --- tig-algorithms/src/knapsack/dynamic/README.md | 23 +++++ tig-algorithms/src/knapsack/dynamic/mod.rs | 93 +++++++++++++++++++ tig-algorithms/src/knapsack/mod.rs | 3 +- 3 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 tig-algorithms/src/knapsack/dynamic/README.md create mode 100644 tig-algorithms/src/knapsack/dynamic/mod.rs diff --git a/tig-algorithms/src/knapsack/dynamic/README.md b/tig-algorithms/src/knapsack/dynamic/README.md new file mode 100644 index 00000000..2962f15b --- /dev/null +++ b/tig-algorithms/src/knapsack/dynamic/README.md @@ -0,0 +1,23 @@ +# TIG Code Submission + +## Submission Details + +* **Challenge Name:** knapsack +* **Algorithm Name:** dynamic +* **Copyright:** 2024 Uncharted Trading Limited +* **Identity of Submitter:** Uncharted Trading Limited +* **Identity of Creator of Algorithmic Method:** null +* **Unique Algorithm Identifier (UAI):** null + +## License + +The files in this folder are under the following licenses: +* TIG Benchmarker Outbound License +* TIG Commercial License +* TIG Inbound Game License +* TIG Innovator Outbound Game License +* TIG Open Data License +* TIG THV Game License + +Copies of the licenses can be obtained at: +https://github.com/tig-foundation/tig-monorepo/tree/main/docs/licenses \ No newline at end of file diff --git a/tig-algorithms/src/knapsack/dynamic/mod.rs b/tig-algorithms/src/knapsack/dynamic/mod.rs new file mode 100644 index 00000000..9d1e4ebc --- /dev/null +++ b/tig-algorithms/src/knapsack/dynamic/mod.rs @@ -0,0 +1,93 @@ +use anyhow::{anyhow, Result}; +use serde_json::{Map, Value}; +use tig_challenges::knapsack::*; + + +pub fn solve_challenge( + challenge: &Challenge, + save_solution: &dyn Fn(&Solution) -> Result<()>, + hyperparameters: &Option>, +) -> Result<()> { + Err(anyhow!("This algorithm is no longer compatible.")) +} + +// Old code that is no longer compatible +#[cfg(none)] +mod dead_code { + use tig_challenges::knapsack::*; + + + pub fn solve_challenge(challenge: &Challenge) -> anyhow::Result> { + let mut solution = Solution { + sub_solutions: Vec::new(), + }; + for sub_instance in &challenge.sub_instances { + match solve_sub_instance(sub_instance)? { + Some(sub_solution) => solution.sub_solutions.push(sub_solution), + None => return Ok(None), + } + } + Ok(Some(solution)) + } + + pub fn solve_sub_instance(challenge: &SubInstance) -> anyhow::Result> { + let max_weight = challenge.max_weight; + let baseline_value = challenge.baseline_value; + let num_items = challenge.num_items; + + // Sort items by value-to-weight ratio in descending order + let mut sorted_items: Vec = (0..num_items).collect(); + sorted_items.sort_by(|&a, &b| { + let ratio_a = challenge.values[a] as f64 / challenge.weights[a] as f64; + let ratio_b = challenge.values[b] as f64 / challenge.weights[b] as f64; + ratio_b.partial_cmp(&ratio_a).unwrap() + }); + + // Initialize combinations with a single empty combo + let mut combinations: Vec<(Vec, u32, u32)> = vec![(vec![false; num_items], 0, 0)]; + + let mut items = Vec::new(); + for &item in &sorted_items { + // Create new combos with the current item + let mut new_combinations: Vec<(Vec, u32, u32)> = combinations + .iter() + .map(|(combo, value, weight)| { + let mut new_combo = combo.clone(); + new_combo[item] = true; + ( + new_combo, + value + challenge.values[item], + weight + challenge.weights[item], + ) + }) + .filter(|&(_, _, weight)| weight <= max_weight) // Keep only combos within weight limit + .collect(); + + // Check if any new combination meets the minimum value requirement + if let Some((combo, _, _)) = new_combinations + .iter() + .find(|&&(_, value, _)| value >= baseline_value) + { + items = combo + .iter() + .enumerate() + .filter_map(|(i, &included)| if included { Some(i) } else { None }) + .collect(); + break; + } + + // Merge new_combinations with existing combinations + combinations.append(&mut new_combinations); + + // Deduplicate combinations by keeping the highest value for each weight + combinations.sort_by(|a, b| a.2.cmp(&b.2).then_with(|| b.1.cmp(&a.1))); // Sort by weight, then by value + combinations.dedup_by(|a, b| a.2 == b.2 && a.1 <= b.1); // Deduplicate by weight, keeping highest value + } + + Ok(Some(SubSolution { items })) + } +} + +pub fn help() { + println!("No help information available."); +} diff --git a/tig-algorithms/src/knapsack/mod.rs b/tig-algorithms/src/knapsack/mod.rs index 33ddf59d..384121e4 100644 --- a/tig-algorithms/src/knapsack/mod.rs +++ b/tig-algorithms/src/knapsack/mod.rs @@ -1,4 +1,5 @@ -// c003_a001 +pub mod dynamic; +pub use dynamic as c003_a001; // c003_a002