haileyhalimj@gmail.com
commited on
Commit
Β·
60e706b
1
Parent(s):
d6dee1d
π Replace get_per_product_speed() with direct extract.read_package_speed_data() calls
Browse files- Remove redundant get_per_product_speed() wrapper function
- Replace all 6 usage locations with direct extract.read_package_speed_data() calls
- Updated files:
- src/config/optimization_config.py: Remove function definition and update calls
- src/models/optimizer_real.py: Remove import and update calls
- src/demand_filtering.py: Direct import and call (2 locations)
- ui/pages/optimization_results.py: Direct import and call
- src/demand_validation_viz.py: Direct import and call
- Eliminate unnecessary abstraction layer
- Improve code clarity by using direct function calls
- Reduce function call overhead
- src/config/optimization_config.py +2 -15
- src/demand_filtering.py +16 -23
- src/demand_validation_viz.py +2 -1
- src/models/optimizer_real.py +4 -3
- ui/pages/config_page.py +0 -1
- ui/pages/optimization_results.py +2 -1
src/config/optimization_config.py
CHANGED
|
@@ -439,19 +439,6 @@ def get_fixed_min_unicef_per_day():
|
|
| 439 |
|
| 440 |
# Fallback to default only if not configured by user
|
| 441 |
return getattr(DefaultConfig, 'FIXED_MIN_UNICEF_PER_DAY', {1: 1, 2: 1, 3: 1, 4: 1, 5: 1})
|
| 442 |
-
def get_per_product_speed():
|
| 443 |
-
try:
|
| 444 |
-
# Try to get from streamlit session state (from config page)
|
| 445 |
-
import streamlit as st
|
| 446 |
-
if hasattr(st, 'session_state') and 'per_product_speed' in st.session_state:
|
| 447 |
-
print(f"Using per product speed from config page: {st.session_state.per_product_speed}")
|
| 448 |
-
return st.session_state.per_product_speed
|
| 449 |
-
except Exception as e:
|
| 450 |
-
print(f"Could not get per product speed from streamlit session: {e}")
|
| 451 |
-
|
| 452 |
-
print(f"Loading default per product speed from data files")
|
| 453 |
-
per_product_speed = extract.read_package_speed_data()
|
| 454 |
-
return per_product_speed
|
| 455 |
|
| 456 |
|
| 457 |
# ============================================================================
|
|
@@ -470,7 +457,7 @@ def _ensure_fresh_config():
|
|
| 470 |
global PAYMENT_MODE_CONFIG
|
| 471 |
|
| 472 |
# Refresh all cached values
|
| 473 |
-
PER_PRODUCT_SPEED =
|
| 474 |
LINE_LIST = get_line_list()
|
| 475 |
EMPLOYEE_TYPE_LIST = get_employee_type_list()
|
| 476 |
SHIFT_LIST = get_active_shift_list()
|
|
@@ -585,7 +572,7 @@ def get_payment_mode_config():
|
|
| 585 |
# which causes the app to reload, which imports this module again, etc.
|
| 586 |
|
| 587 |
# Initialize with default values (will use fallback data when no Streamlit session)
|
| 588 |
-
# PER_PRODUCT_SPEED =
|
| 589 |
# LINE_LIST = get_line_list()
|
| 590 |
# EMPLOYEE_TYPE_LIST = get_employee_type_list()
|
| 591 |
# SHIFT_LIST = get_active_shift_list()
|
|
|
|
| 439 |
|
| 440 |
# Fallback to default only if not configured by user
|
| 441 |
return getattr(DefaultConfig, 'FIXED_MIN_UNICEF_PER_DAY', {1: 1, 2: 1, 3: 1, 4: 1, 5: 1})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 442 |
|
| 443 |
|
| 444 |
# ============================================================================
|
|
|
|
| 457 |
global PAYMENT_MODE_CONFIG
|
| 458 |
|
| 459 |
# Refresh all cached values
|
| 460 |
+
PER_PRODUCT_SPEED = extract.read_package_speed_data()
|
| 461 |
LINE_LIST = get_line_list()
|
| 462 |
EMPLOYEE_TYPE_LIST = get_employee_type_list()
|
| 463 |
SHIFT_LIST = get_active_shift_list()
|
|
|
|
| 572 |
# which causes the app to reload, which imports this module again, etc.
|
| 573 |
|
| 574 |
# Initialize with default values (will use fallback data when no Streamlit session)
|
| 575 |
+
# PER_PRODUCT_SPEED = extract.read_package_speed_data()
|
| 576 |
# LINE_LIST = get_line_list()
|
| 577 |
# EMPLOYEE_TYPE_LIST = get_employee_type_list()
|
| 578 |
# SHIFT_LIST = get_active_shift_list()
|
src/demand_filtering.py
CHANGED
|
@@ -85,6 +85,14 @@ class DemandFilter:
|
|
| 85 |
print(f"Error loading data for filtering: {str(e)}")
|
| 86 |
return False
|
| 87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
def standalone_master_filter(self, product_id: str) -> Tuple[str, bool]:
|
| 89 |
"""
|
| 90 |
Classify product type and check if it's a standalone master.
|
|
@@ -128,9 +136,9 @@ class DemandFilter:
|
|
| 128 |
has_line_assignment = product_id in self.line_assignments
|
| 129 |
|
| 130 |
# For masters: standalone should have line assignment, non-standalone should NOT
|
|
|
|
| 131 |
if product_type == "master":
|
| 132 |
if is_standalone_master:
|
| 133 |
-
# Standalone masters should have "long line" assignment
|
| 134 |
if not has_line_assignment:
|
| 135 |
exclusion_reasons.append("Standalone master missing line assignment")
|
| 136 |
elif self.line_assignments.get(product_id) != 6: # 6 = LONG_LINE
|
|
@@ -157,6 +165,9 @@ class DemandFilter:
|
|
| 157 |
def filter_products(self) -> Tuple[List[str], Dict[str, int], List[str], Dict[str, int]]:
|
| 158 |
"""
|
| 159 |
Filter products into included and excluded lists.
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
Returns:
|
| 162 |
Tuple containing:
|
|
@@ -219,7 +230,8 @@ class DemandFilter:
|
|
| 219 |
speed_data = None
|
| 220 |
try:
|
| 221 |
from src.config import optimization_config
|
| 222 |
-
|
|
|
|
| 223 |
except Exception as e:
|
| 224 |
print(f"Warning: Could not load speed data for validation: {e}")
|
| 225 |
|
|
@@ -255,7 +267,8 @@ class DemandFilter:
|
|
| 255 |
speed_data = None
|
| 256 |
try:
|
| 257 |
from src.config import optimization_config
|
| 258 |
-
|
|
|
|
| 259 |
except Exception as e:
|
| 260 |
print(f"Warning: Could not load speed data for analysis: {e}")
|
| 261 |
|
|
@@ -308,26 +321,6 @@ class DemandFilter:
|
|
| 308 |
'included_missing_speed_count': included_without_speed,
|
| 309 |
'included_missing_hierarchy_count': included_without_hierarchy
|
| 310 |
}
|
| 311 |
-
|
| 312 |
-
def get_exclusion_summary(self) -> Dict:
|
| 313 |
-
"""Get summary of excluded products for reporting"""
|
| 314 |
-
included_products, included_demand, excluded_products, excluded_demand = self.filter_products()
|
| 315 |
-
|
| 316 |
-
excluded_details = {}
|
| 317 |
-
for product_id in excluded_products:
|
| 318 |
-
_, reasons = self.is_product_ready_for_optimization(product_id)
|
| 319 |
-
excluded_details[product_id] = {
|
| 320 |
-
'demand': excluded_demand[product_id],
|
| 321 |
-
'reasons': reasons
|
| 322 |
-
}
|
| 323 |
-
|
| 324 |
-
return {
|
| 325 |
-
'included_count': len(included_products),
|
| 326 |
-
'included_demand': sum(included_demand.values()),
|
| 327 |
-
'excluded_count': len(excluded_products),
|
| 328 |
-
'excluded_demand': sum(excluded_demand.values()),
|
| 329 |
-
'excluded_details': excluded_details
|
| 330 |
-
}
|
| 331 |
|
| 332 |
|
| 333 |
# Test script when run directly
|
|
|
|
| 85 |
print(f"Error loading data for filtering: {str(e)}")
|
| 86 |
return False
|
| 87 |
|
| 88 |
+
def too_high_demand_filter(self, product_id: str) -> bool:
|
| 89 |
+
"""
|
| 90 |
+
Check if the demand for a product is too high.
|
| 91 |
+
If too high, the product will be excluded from optimization.
|
| 92 |
+
"""
|
| 93 |
+
return True
|
| 94 |
+
|
| 95 |
+
|
| 96 |
def standalone_master_filter(self, product_id: str) -> Tuple[str, bool]:
|
| 97 |
"""
|
| 98 |
Classify product type and check if it's a standalone master.
|
|
|
|
| 136 |
has_line_assignment = product_id in self.line_assignments
|
| 137 |
|
| 138 |
# For masters: standalone should have line assignment, non-standalone should NOT
|
| 139 |
+
|
| 140 |
if product_type == "master":
|
| 141 |
if is_standalone_master:
|
|
|
|
| 142 |
if not has_line_assignment:
|
| 143 |
exclusion_reasons.append("Standalone master missing line assignment")
|
| 144 |
elif self.line_assignments.get(product_id) != 6: # 6 = LONG_LINE
|
|
|
|
| 165 |
def filter_products(self) -> Tuple[List[str], Dict[str, int], List[str], Dict[str, int]]:
|
| 166 |
"""
|
| 167 |
Filter products into included and excluded lists.
|
| 168 |
+
1) Should have demand higher than 0
|
| 169 |
+
2) Should pass through is_product_ready_for_optimization()
|
| 170 |
+
|
| 171 |
|
| 172 |
Returns:
|
| 173 |
Tuple containing:
|
|
|
|
| 230 |
speed_data = None
|
| 231 |
try:
|
| 232 |
from src.config import optimization_config
|
| 233 |
+
from src.preprocess import extract
|
| 234 |
+
speed_data = extract.read_package_speed_data()
|
| 235 |
except Exception as e:
|
| 236 |
print(f"Warning: Could not load speed data for validation: {e}")
|
| 237 |
|
|
|
|
| 267 |
speed_data = None
|
| 268 |
try:
|
| 269 |
from src.config import optimization_config
|
| 270 |
+
from src.preprocess import extract
|
| 271 |
+
speed_data = extract.read_package_speed_data()
|
| 272 |
except Exception as e:
|
| 273 |
print(f"Warning: Could not load speed data for analysis: {e}")
|
| 274 |
|
|
|
|
| 321 |
'included_missing_speed_count': included_without_speed,
|
| 322 |
'included_missing_hierarchy_count': included_without_hierarchy
|
| 323 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 324 |
|
| 325 |
|
| 326 |
# Test script when run directly
|
src/demand_validation_viz.py
CHANGED
|
@@ -39,7 +39,8 @@ class DemandValidationViz:
|
|
| 39 |
"""Load all data needed for visualization"""
|
| 40 |
try:
|
| 41 |
from src.config import optimization_config
|
| 42 |
-
|
|
|
|
| 43 |
return self.filter_instance.load_data()
|
| 44 |
except Exception as e:
|
| 45 |
error_msg = f"Error loading data: {str(e)}"
|
|
|
|
| 39 |
"""Load all data needed for visualization"""
|
| 40 |
try:
|
| 41 |
from src.config import optimization_config
|
| 42 |
+
from src.preprocess import extract
|
| 43 |
+
self.speed_data = extract.read_package_speed_data()
|
| 44 |
return self.filter_instance.load_data()
|
| 45 |
except Exception as e:
|
| 46 |
error_msg = f"Error loading data: {str(e)}"
|
src/models/optimizer_real.py
CHANGED
|
@@ -24,7 +24,6 @@ from src.config.optimization_config import (
|
|
| 24 |
get_max_employee_per_type_on_day, # DYNAMIC: {emp_type: {t: headcount}}
|
| 25 |
MAX_HOUR_PER_PERSON_PER_DAY, # e.g., 14
|
| 26 |
get_max_hour_per_shift_per_person, # DYNAMIC: {1: hours, 2: hours, 3: hours}
|
| 27 |
-
get_per_product_speed, # DYNAMIC: {6: cap_units_per_hour, 7: cap_units_per_hour}
|
| 28 |
get_max_parallel_workers, # DYNAMIC: {6: max_workers, 7: max_workers}
|
| 29 |
FIXED_STAFF_CONSTRAINT_MODE, # not used in fixed-team model (λμ ν¬μ
μ΄λΌ 무μλ―Έ)
|
| 30 |
get_team_requirements, # DYNAMIC: {emp_type: {product: team_size}} from Kits_Calculation.csv
|
|
@@ -69,7 +68,7 @@ def build_lines():
|
|
| 69 |
# These variables are now created dynamically when needed
|
| 70 |
# line_tuples=build_lines()
|
| 71 |
# print("line_tuples",line_tuples)
|
| 72 |
-
# PER_PRODUCT_SPEED =
|
| 73 |
# print("PER_PRODUCT_SPEED",PER_PRODUCT_SPEED)
|
| 74 |
|
| 75 |
def sort_products_by_hierarchy(product_list):
|
|
@@ -206,7 +205,9 @@ def run_optimization_for_week():
|
|
| 206 |
print("Lines",line_tuples)
|
| 207 |
|
| 208 |
# Load product speed data dynamically
|
| 209 |
-
|
|
|
|
|
|
|
| 210 |
print("PER_PRODUCT_SPEED",PER_PRODUCT_SPEED)
|
| 211 |
|
| 212 |
# --- Short aliases for parameters ---
|
|
|
|
| 24 |
get_max_employee_per_type_on_day, # DYNAMIC: {emp_type: {t: headcount}}
|
| 25 |
MAX_HOUR_PER_PERSON_PER_DAY, # e.g., 14
|
| 26 |
get_max_hour_per_shift_per_person, # DYNAMIC: {1: hours, 2: hours, 3: hours}
|
|
|
|
| 27 |
get_max_parallel_workers, # DYNAMIC: {6: max_workers, 7: max_workers}
|
| 28 |
FIXED_STAFF_CONSTRAINT_MODE, # not used in fixed-team model (λμ ν¬μ
μ΄λΌ 무μλ―Έ)
|
| 29 |
get_team_requirements, # DYNAMIC: {emp_type: {product: team_size}} from Kits_Calculation.csv
|
|
|
|
| 68 |
# These variables are now created dynamically when needed
|
| 69 |
# line_tuples=build_lines()
|
| 70 |
# print("line_tuples",line_tuples)
|
| 71 |
+
# PER_PRODUCT_SPEED = extract.read_package_speed_data() # Dynamic call
|
| 72 |
# print("PER_PRODUCT_SPEED",PER_PRODUCT_SPEED)
|
| 73 |
|
| 74 |
def sort_products_by_hierarchy(product_list):
|
|
|
|
| 205 |
print("Lines",line_tuples)
|
| 206 |
|
| 207 |
# Load product speed data dynamically
|
| 208 |
+
# Import extract module for direct access to data functions
|
| 209 |
+
from src.preprocess import extract
|
| 210 |
+
PER_PRODUCT_SPEED = extract.read_package_speed_data()
|
| 211 |
print("PER_PRODUCT_SPEED",PER_PRODUCT_SPEED)
|
| 212 |
|
| 213 |
# --- Short aliases for parameters ---
|
ui/pages/config_page.py
CHANGED
|
@@ -1022,7 +1022,6 @@ def clear_all_cache_and_results():
|
|
| 1022 |
keys_to_clear = [
|
| 1023 |
'optimization_results',
|
| 1024 |
'demand_dictionary',
|
| 1025 |
-
'per_product_speed',
|
| 1026 |
'kit_hierarchy_data',
|
| 1027 |
'team_requirements'
|
| 1028 |
]
|
|
|
|
| 1022 |
keys_to_clear = [
|
| 1023 |
'optimization_results',
|
| 1024 |
'demand_dictionary',
|
|
|
|
| 1025 |
'kit_hierarchy_data',
|
| 1026 |
'team_requirements'
|
| 1027 |
]
|
ui/pages/optimization_results.py
CHANGED
|
@@ -775,7 +775,8 @@ def display_input_data_inspection():
|
|
| 775 |
st.write(f" ... and {len(product_list) - 10} more")
|
| 776 |
|
| 777 |
st.write("**Production Speed (units/hour):**")
|
| 778 |
-
|
|
|
|
| 779 |
st.write("*Sample speeds:*")
|
| 780 |
sample_speeds = list(speed_data.items())[:5]
|
| 781 |
for product, speed in sample_speeds:
|
|
|
|
| 775 |
st.write(f" ... and {len(product_list) - 10} more")
|
| 776 |
|
| 777 |
st.write("**Production Speed (units/hour):**")
|
| 778 |
+
from src.preprocess import extract
|
| 779 |
+
speed_data = extract.read_package_speed_data()
|
| 780 |
st.write("*Sample speeds:*")
|
| 781 |
sample_speeds = list(speed_data.items())[:5]
|
| 782 |
for product, speed in sample_speeds:
|