oh-my-dear-ai commited on
Commit
dff6166
Β·
1 Parent(s): 5787f24

[fix](app) Replace int with round to improve numerical precision

Browse files
Files changed (1) hide show
  1. app.py +4 -4
app.py CHANGED
@@ -164,7 +164,7 @@ def calculator(currency, budget, strategy, extra_rate, *amount):
164
  model.addCons(obj1 <= budget)
165
 
166
  # first optimize
167
- model.hideOutput()
168
  model.optimize()
169
 
170
  if model.getStatus() == "optimal":
@@ -184,7 +184,7 @@ def calculator(currency, budget, strategy, extra_rate, *amount):
184
 
185
  if model.getStatus() == "optimal":
186
  for i, var in enumerate(x):
187
- if (v := int(model.getVal(var))) > 0 and sold_prices[i] > 0:
188
  solution.append(
189
  f"{plants_names[i]} ({sold_prices[i]} {currency}): {v}\n"
190
  )
@@ -192,9 +192,9 @@ def calculator(currency, budget, strategy, extra_rate, *amount):
192
  total_count += v
193
 
194
  if optimal_total_value == budget:
195
- return f"Great! Found a combination of items with a total value equal to the budget ({budget} {currency}).πŸ˜ƒ\n\n{''.join(solution)}\nTotal value: {int(total_price)} {currency}\nTotal count: {total_count}" # Count: {int(model.getObjVal())}
196
 
197
- return f"Oops! {int(budget - optimal_total_value)} {currency} short of the target value ({budget} {currency}).😣\n\n{''.join(solution)}\nTotal value: {int(total_price)} {currency}\nTotal count: {total_count}" # Count: {int(model.getObjVal())}
198
 
199
  return "No solution found for the second optimization!"
200
 
 
164
  model.addCons(obj1 <= budget)
165
 
166
  # first optimize
167
+ # model.hideOutput()
168
  model.optimize()
169
 
170
  if model.getStatus() == "optimal":
 
184
 
185
  if model.getStatus() == "optimal":
186
  for i, var in enumerate(x):
187
+ if (v := round(model.getVal(var))) > 0 and sold_prices[i] > 0:
188
  solution.append(
189
  f"{plants_names[i]} ({sold_prices[i]} {currency}): {v}\n"
190
  )
 
192
  total_count += v
193
 
194
  if optimal_total_value == budget:
195
+ return f"Great! Found a combination of items with a total value equal to the budget ({budget} {currency}).πŸ˜ƒ\n\n{''.join(solution)}\nTotal value: {total_price} {currency}\nTotal count: {total_count}" # Count: {int(model.getObjVal())}
196
 
197
+ return f"Oops! {round(budget - optimal_total_value)} {currency} short of the target value ({budget} {currency}).😣\n\n{''.join(solution)}\nTotal value: {total_price} {currency}\nTotal count: {total_count}" # Count: {int(model.getObjVal())}
198
 
199
  return "No solution found for the second optimization!"
200