Spaces:
Sleeping
Sleeping
[FIX] Lower usage epsilon to 1e-14 and raise on unknown cap_type in LP builder
Browse files249 unmodified lines25025125225325425525625725825926026126226332 unmodified lines2962972982993003013023033043053063073085 unmodified lines3143153163173183193209 unmodified lines330331332333334335336337338339149 unmodified lines489490491492493494495496497498499500501249 unmodified linesA_rows.append(row)b_rows.append(cap_value)constraint_labels.append(("method_cap", m))else:cap_ratio = cap_value / 100.0row = np.zeros(num_lp_vars)row[:num_methods] -= cap_ratiorow[j] += 1.0A_rows.append(row)b_rows.append(0.0)constraint_labels.append(("method_cap", m))A_ub = np.array(A_rows) if A_rows else np.empty((0, num_lp_vars))b_ub = np.array(b_rows)32 unmodified linesall_resource_names = standard_resources + list(custom_batch_names)resource_usage = {r: {} for r in all_resource_names}# 1e-9 is a near-zero threshold: LP solvers return tiny floating-point residuals# (e.g. 0.000000001) instead of exact 0. Values below this threshold are treated# as zero so they don't appear as spurious resource usage in the output.for r in resources_no_batches:for j, m in enumerate(active_methods):used = method_costs.get(m, {}).get(r, 0.0) * method_outputs[j]if used > 1e-9:resource_usage[r][m] = float(used)for r in resources_with_batches:5 unmodified linesif (m, batch_name) in custom_pair_to_index)used = c_s_m * (method_outputs[j] - z_sum)if used > 1e-9:resource_usage[r][m] = float(used)for batch_name, specs in custom_batch_specs.items():9 unmodified linesif (m, batch_name) in custom_pair_to_indexelse 0.0)if used > 1e-9:resource_usage[batch_name][m] = float(used)_EPS = 1e-9resource_used_total = {r: round(sum(v.values()), 10) for r, v in resource_usage.items()}149 unmodified linesresources_with_batches = [r for r in standard_resources if r in group_to_custom_batches]resources_no_batches = [r for r in standard_resources if r not in group_to_custom_batches]A_ub, b_ub, constraint_labels = build_constraint_matrix(active_methods, method_to_index, num_lp_vars, num_methods,resource_caps, method_costs, method_constraints,custom_batch_specs, group_to_custom_batches,resources_no_batches, resources_with_batches,custom_usage_pairs, custom_pair_to_index,)# linprog minimises by convention, so we negate the objective to maximise Σ y_m.objective_coefficients = np.zeros(num_lp_vars)249 unmodified lines25025125225325425525625725825926026126226326426526626726832 unmodified lines3013023033043053063073083093103113123133145 unmodified lines3203213223233243253269 unmodified lines336337338339340341342343344345149 unmodified lines495496497498499500501502503504505506507508509510249 unmodified linesA_rows.append(row)b_rows.append(cap_value)constraint_labels.append(("method_cap", m))elif cap_type == "percent":cap_ratio = cap_value / 100row = np.zeros(num_lp_vars)row[:num_methods] -= cap_ratiorow[j] += 1.0A_rows.append(row)b_rows.append(0.0)constraint_labels.append(("method_cap", m))else:raise ValueError(f"Unknown cap_type '{cap_type}' for method '{m}'. ""Expected 'percent' or 'absolute'.")A_ub = np.array(A_rows) if A_rows else np.empty((0, num_lp_vars))b_ub = np.array(b_rows)32 unmodified linesall_resource_names = standard_resources + list(custom_batch_names)resource_usage = {r: {} for r in all_resource_names}# LP solvers (HiGHS) return floating-point residuals on the order of 1e-14 to# 1e-12, so 1e-14 safely filters out numerical noise while preserving# legitimately tiny usage values (e.g. Non-arable land coefficient = 1e-11)._USAGE_EPS = 1e-14for r in resources_no_batches:for j, m in enumerate(active_methods):used = method_costs.get(m, {}).get(r, 0.0) * method_outputs[j]if used > _USAGE_EPS:resource_usage[r][m] = float(used)for r in resources_with_batches:5 unmodified linesif (m, batch_name) in custom_pair_to_index)used = c_s_m * (method_outputs[j] - z_sum)if used > _USAGE_EPS:resource_usage[r][m] = float(used)for batch_name, specs in custom_batch_specs.items():9 unmodified linesif (m, batch_name) in custom_pair_to_indexelse 0.0)if used > _USAGE_EPS:resource_usage[batch_name][m] = float(used)_EPS = 1e-14resource_used_total = {r: round(sum(v.values()), 10) for r, v in resource_usage.items()}149 unmodified linesresources_with_batches = [r for r in standard_resources if r in group_to_custom_batches]resources_no_batches = [r for r in standard_resources if r not in group_to_custom_batches]try:A_ub, b_ub, constraint_labels = build_constraint_matrix(active_methods, method_to_index, num_lp_vars, num_methods,resource_caps, method_costs, method_constraints,custom_batch_specs, group_to_custom_batches,resources_no_batches, resources_with_batches,custom_usage_pairs, custom_pair_to_index,)except ValueError as e:return False, {"message": str(e)}# linprog minimises by convention, so we negate the objective to maximise Σ y_m.objective_coefficients = np.zeros(num_lp_vars)
optimization/optimization.py+25-16