update the solver sandbox elastic constraint impl

This commit is contained in:
Adrian Manteza 2022-03-17 14:01:44 +00:00
parent efbc83ca8d
commit 34418066ca

View File

@ -61,30 +61,39 @@ class SolverSandbox:
print(v.name, "=", v.varValue)
break
def yas_elastic(tif_target = 140.0): # 140 is the optimal
Items = [1,2,3,4,5]
Bundles = [1,2]
# For TIF target
tif = {
1: 10,
2: 20
}
def yas_elastic(tif_targets, tcc_targets): # [50, 55, 46], [60, 40, 50]
Items = [1,2,3,4,5,6,7,8,9,10]
iif = {
1: 10,
2: 20,
3: 30,
4: 40,
5: 50
1: 5,
2: 5,
3: 5,
4: 10,
5: 10,
6: 10,
7: 15,
8: 20,
9: 20,
10: 20
}
# ---
irf = {
1: 5,
2: 5,
3: 5,
4: 10,
5: 10,
6: 10,
7: 15,
8: 20,
9: 20,
10: 20
}
items = LpVariable.dicts('Item', Items, cat='Binary')
bundles = LpVariable.dicts('Bundle', Bundles, cat='Binary')
items = LpVariable.dicts('Item', Items, cat='Binary')
drift = 0
max_drift = 10 # 10% elasticity
max_drift = 25# 25% elasticity
while drift <= max_drift:
drift_percent = drift / 100
@ -94,32 +103,35 @@ class SolverSandbox:
problem += lpSum([items[i] for i in Items])
# Constraint 1
problem += lpSum([items[i] for i in Items] + [bundles[b] for b in Bundles]) == 2, 'TotalItems'
print(f"Calculating TIF target of {tif_target} with drift of {drift}%")
problem += lpSum([items[i] for i in Items]) == 5, 'TotalItems'
# Our own "Elastic Constraints"
problem += lpSum(
[iif[i] * items[i] for i in Items]
) >= tif_target - (tif_target * drift_percent), 'ItemIifMin'
problem += lpSum(
[iif[i] * items[i] for i in Items]
) <= tif_target + (tif_target * drift_percent), 'ItemIifMax'
for tif_target in tif_targets:
print(f"Calculating TIF target of {tif_target} with drift of {drift}%")
problem += lpSum(
[iif[i] * items[i] for i in Items]
) >= tif_target - (tif_target * drift_percent)
problem += lpSum(
[iif[i] * items[i] for i in Items]
) <= tif_target + (tif_target * drift_percent)
problem += lpSum(
[tif[i] * bundles[i] for i in Bundles]
) >= tif_target - (tif_target * drift_percent), 'BundleTifMin'
problem += lpSum(
[tif[i] * bundles[i] for i in Bundles]
) <= tif_target + (tif_target * drift_percent), 'BundleTifMax'
for tcc_target in tcc_targets:
print(f"Calculating TIF target of {tcc_target} with drift of {drift}%")
problem += lpSum(
[irf[i] * items[i] for i in Items]
) >= tcc_target - (tcc_target * drift_percent)
problem += lpSum(
[irf[i] * items[i] for i in Items]
) <= tcc_target + (tcc_target * drift_percent)
problem.solve()
if LpStatus[problem.status] == 'Infeasible':
print(f"attempt infeasible for drift of {drift}")
for v in problem.variables():
print(v.name, "=", v.varValue)
for v in problem.variables(): print(v.name, "=", v.varValue)
# if drift == max_drift: breakpoint();
print(problem.objective.value())
print(problem.constraints)
@ -129,9 +141,9 @@ class SolverSandbox:
else:
print(f"solution found with drift of {drift}!")
for v in problem.variables():
print(v.name, "=", v.varValue)
for v in problem.variables(): print(v.name, "=", v.varValue);
print(problem.objective.value())
print(problem.constraints)
print(problem.objective)