1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// /// Solves a linear programming problem with given demand and supply vectors
// ///
// /// Args:
// /// demand: A vector of demand values by period
// /// supply: A vector of supply capacity values by period
// /// capacity: Optional overall capacity constraint
// ///
// /// Returns:
// /// A vector of optimized production values
// #[pyfunction]
// #[pyo3(signature = (demand, supply, capacity=None))]
// fn solve_linear_program(
// demand: Vec<f64>,
// supply: Vec<f64>,
// capacity: Option<f64>,
// ) -> PyResult<Vec<f64>> {
// // Ensure vectors are of equal length
// if demand.len() != supply.len() {
// return Err(pyo3::exceptions::PyValueError::new_err(
// "Demand and supply vectors must have the same length",
// ));
// }
// // Calculate total demand and available supply
// let total_demand: f64 = demand.iter().sum();
// let total_supply: f64 = supply.iter().sum();
// // If capacity is provided, constrain the total supply
// let effective_supply = if let Some(cap) = capacity {
// total_supply.min(cap)
// } else {
// total_supply
// };
// // Determine if we need to allocate scarce resources or we have excess capacity
// let allocation_factor = if total_demand > effective_supply {
// // Scarce resources - allocate proportionally to demand
// effective_supply / total_demand
// } else {
// // Excess capacity - produce to meet demand
// 1.0
// };
// // Calculate the optimized production plan
// let result: Vec<f64> = demand.iter().map(|&d| d * allocation_factor).collect();
// Ok(result)
// }
// /// Optimizes a production plan for multiple products with resource constraints
// ///
// /// Args:
// /// products: A list of product names
// /// demand_dict: A dict mapping products to their demand
// /// production_rates_dict: A dict mapping products to units produced per hour
// /// available_hours: Total available production hours
// /// current_inventory_dict: A dict mapping products to current inventory levels
// /// min_inventory_dict: A dict mapping products to minimum required inventory
// ///
// /// Returns:
// /// A dict with optimized production quantities and resulting inventory
// #[pyfunction]
// #[pyo3(signature = (products, demand_dict, production_rates_dict, available_hours, current_inventory_dict, min_inventory_dict))]
// fn optimize_production_plan(
// py: Python<'_>,
// products: Vec<String>,
// demand_dict: HashMap<String, f64>,
// production_rates_dict: HashMap<String, f64>,
// available_hours: f64,
// current_inventory_dict: HashMap<String, f64>,
// min_inventory_dict: HashMap<String, f64>,
// ) -> PyResult<PyObject> {
// // Extract values for each product
// let mut product_data: Vec<ProductData> = Vec::new();
// for product in &products {
// // Get demand for this product
// let demand_value = match demand_dict.get(product) {
// Some(&val) => val,
// None => {
// return Err(pyo3::exceptions::PyKeyError::new_err(format!(
// "Product '{}' not found in demand_dict",
// product
// )));
// }
// };
// // Get production rate
// let production_rate = match production_rates_dict.get(product) {
// Some(&val) => val,
// None => {
// return Err(pyo3::exceptions::PyKeyError::new_err(format!(
// "Product '{}' not found in production_rates_dict",
// product
// )));
// }
// };
// // Get current inventory
// let inventory = match current_inventory_dict.get(product) {
// Some(&val) => val,
// None => {
// return Err(pyo3::exceptions::PyKeyError::new_err(format!(
// "Product '{}' not found in current_inventory_dict",
// product
// )));
// }
// };
// // Get minimum inventory requirement
// let min_inv = match min_inventory_dict.get(product) {
// Some(&val) => val,
// None => {
// return Err(pyo3::exceptions::PyKeyError::new_err(format!(
// "Product '{}' not found in min_inventory_dict",
// product
// )));
// }
// };
// // Calculate effective demand (actual demand + inventory shortage)
// let effective_demand = demand_value + (min_inv - inventory).max(0.0);
// // Calculate priority (higher for products with inventory below minimum)
// let priority = if inventory < min_inv {
// (min_inv - inventory) / min_inv.max(1.0)
// } else {
// 0.0
// };
// product_data.push(ProductData {
// name: product.clone(),
// demand: demand_value,
// effective_demand,
// production_rate,
// inventory,
// min_inventory: min_inv,
// priority,
// hours_needed: effective_demand / production_rate,
// });
// }
// // Sort by priority (higher priority first)
// product_data.sort_by(|a, b| {
// b.priority
// .partial_cmp(&a.priority)
// .unwrap_or(Ordering::Equal)
// });
// // Allocate hours based on priority and need
// let mut remaining_hours = available_hours;
// let mut production_plan = Vec::new();
// for product in &product_data {
// let hours_allocated = product.hours_needed.min(remaining_hours);
// let quantity_produced = hours_allocated * product.production_rate;
// production_plan.push((
// product.name.clone(),
// quantity_produced,
// product.inventory + quantity_produced - product.demand,
// ));
// remaining_hours -= hours_allocated;
// if remaining_hours <= 0.0 {
// break;
// }
// }
// // Print debug info
// println!(
// "DEBUG: Production plan created with {} products",
// production_plan.len()
// );
// for (product, qty, inv) in &production_plan {
// println!(
// "DEBUG: Product: {}, Production: {}, Final Inventory: {}",
// product, qty, inv
// );
// }
// // Create return dictionary
// let result_dict = PyDict::new(py);
// // Production quantities
// let production_dict = PyDict::new(py);
// for (product, quantity, _) in &production_plan {
// production_dict.set_item(product, *quantity)?;
// }
// result_dict.set_item("production", production_dict)?;
// // Final inventory
// let inventory_dict = PyDict::new(py);
// for (product, _, final_inventory) in &production_plan {
// inventory_dict.set_item(product, *final_inventory)?;
// }
// result_dict.set_item("projected_inventory", inventory_dict)?;
// // Hours used
// result_dict.set_item("hours_used", available_hours - remaining_hours)?;
// // Convert to Python object
// Ok(result_dict.to_object(py))
// }
// /// Internal structure to hold product data for optimization
// struct ProductData {
// name: String,
// demand: f64,
// effective_demand: f64,
// production_rate: f64,
// inventory: f64,
// min_inventory: f64,
// priority: f64,
// hours_needed: f64,
// }