oh-my-dear-ai commited on
Commit
b64d569
Β·
1 Parent(s): a19fb1a

[opt] hide feeble plants in HVA mode

Browse files
Files changed (1) hide show
  1. app.py +41 -30
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import gradio as gr
2
  import pandas as pd
3
- import pulp
 
4
  import numpy as np
5
  from numpy.typing import NDArray
6
  from pyscipopt import Model, quicksum
@@ -170,9 +171,9 @@ def calculator(*args):
170
  # total_count += v
171
 
172
  if optimal_total_value == budget:
173
- return f"\nGreat! Found a combination of items with a total value equal to the budget ({budget} {currency}).πŸ˜ƒ\n\n{''.join(solution)}\nTotal Price: {int(optimal_total_value)} {currency}\n" # Count: {int(model.getObjVal())}
174
  else:
175
- return f"Oops! {int(budget - optimal_total_value)} {currency} short of the target value ({budget} {currency}).πŸ˜‚\n\n{''.join(solution)}\nTotal value: {int(optimal_total_value)} {currency}\n" # Count: {int(model.getObjVal())}
176
  else:
177
  return "No solution found for the second optimization!"
178
  else:
@@ -195,8 +196,6 @@ with gr.Blocks(css=css) as demo:
195
  """
196
  )
197
 
198
- gold_or_gems = gr.State("gold")
199
-
200
  # Create a Gradio interface with a column layout
201
  with gr.Column():
202
  # Add a row for the currency selection
@@ -247,7 +246,7 @@ with gr.Blocks(css=css) as demo:
247
  info="Select a strategy:",
248
  )
249
 
250
- def show_plant_boxes(currency):
251
  # Update the state variable
252
 
253
  inventory = {}
@@ -256,24 +255,25 @@ with gr.Blocks(css=css) as demo:
256
  for _, row in df.iterrows():
257
  # Check if the plant should be shown based on the selected currency
258
  if row[currency] != 0:
259
- species_set.add(row["species"])
260
- # Create the Number component for the plant inventory
261
- inventory[f"{row['species']}_{row['tier']}"] = gr.Number(
262
- label=PLANTS_LABLES[row["species"]],
263
- info=f"{PLANTS_TIERS[row['tier']]} ${row[currency]}",
264
- value=0,
265
- precision=0,
266
- minimum=0,
267
- maximum=500,
268
- step=10,
269
- visible=True,
270
- elem_classes=(
271
- f"first-{currency}-box"
272
- if len(species_set) > species_count
273
- else None
274
- ),
275
- )
276
- species_count = len(species_set)
 
277
  else:
278
  # If not shown, create a dummy invisible component
279
  inventory[f"{row['species']}_{row['tier']}"] = gr.Number(visible=False)
@@ -283,7 +283,7 @@ with gr.Blocks(css=css) as demo:
283
 
284
  # Create the dynamic plant inventory inputs
285
  with gr.Row() as inventory_row:
286
- inventory = show_plant_boxes(gold_or_gems.value)
287
 
288
  # Add a row for the Clear and Calculate buttons
289
  with gr.Row():
@@ -299,17 +299,28 @@ with gr.Blocks(css=css) as demo:
299
  # Set up the button click event to call the calculator function
300
  submit_btn.click(
301
  calculator,
302
- inputs=[gold_or_gems, budget, selected_strategy, acquisition_rate] + inventory,
 
303
  outputs=[result],
304
  api_name=False,
305
  )
306
 
307
  # Update the inventory when the currency changes
308
  currency_radio.change(
309
- fn=lambda selected_currency: show_plant_boxes(selected_currency)
310
- + [selected_currency], # Adjusted function to return only the components
311
- inputs=currency_radio,
312
- outputs=inventory + [gold_or_gems], # Update each child in the inventory_row
 
 
 
 
 
 
 
 
 
 
313
  )
314
 
315
  # Launch the Gradio application
 
1
  import gradio as gr
2
  import pandas as pd
3
+
4
+ # import pulp
5
  import numpy as np
6
  from numpy.typing import NDArray
7
  from pyscipopt import Model, quicksum
 
171
  # total_count += v
172
 
173
  if optimal_total_value == budget:
174
+ 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(optimal_total_value)} {currency}\n" # Count: {int(model.getObjVal())}
175
  else:
176
+ return f"Oops! {int(budget - optimal_total_value)} {currency} short of the target value ({budget} {currency}).😣\n\n{''.join(solution)}\nTotal value: {int(optimal_total_value)} {currency}\n" # Count: {int(model.getObjVal())}
177
  else:
178
  return "No solution found for the second optimization!"
179
  else:
 
196
  """
197
  )
198
 
 
 
199
  # Create a Gradio interface with a column layout
200
  with gr.Column():
201
  # Add a row for the currency selection
 
246
  info="Select a strategy:",
247
  )
248
 
249
+ def show_plant_boxes(currency, acquisition_rate):
250
  # Update the state variable
251
 
252
  inventory = {}
 
255
  for _, row in df.iterrows():
256
  # Check if the plant should be shown based on the selected currency
257
  if row[currency] != 0:
258
+ if acquisition_rate == 0 or row["tier"] != "feeble":
259
+ species_set.add(row["species"])
260
+ # Create the Number component for the plant inventory
261
+ inventory[f"{row['species']}_{row['tier']}"] = gr.Number(
262
+ label=PLANTS_LABLES[row["species"]],
263
+ info=f"{PLANTS_TIERS[row['tier']]} ${row[currency]}",
264
+ value=0,
265
+ precision=0,
266
+ minimum=0,
267
+ maximum=500,
268
+ step=10,
269
+ visible=True,
270
+ elem_classes=(
271
+ f"first-{currency}-box"
272
+ if len(species_set) > species_count
273
+ else None
274
+ ),
275
+ )
276
+ species_count = len(species_set)
277
  else:
278
  # If not shown, create a dummy invisible component
279
  inventory[f"{row['species']}_{row['tier']}"] = gr.Number(visible=False)
 
283
 
284
  # Create the dynamic plant inventory inputs
285
  with gr.Row() as inventory_row:
286
+ inventory = show_plant_boxes(currency_radio.value, acquisition_rate.value)
287
 
288
  # Add a row for the Clear and Calculate buttons
289
  with gr.Row():
 
299
  # Set up the button click event to call the calculator function
300
  submit_btn.click(
301
  calculator,
302
+ inputs=[currency_radio, budget, selected_strategy, acquisition_rate]
303
+ + inventory,
304
  outputs=[result],
305
  api_name=False,
306
  )
307
 
308
  # Update the inventory when the currency changes
309
  currency_radio.change(
310
+ fn=lambda selected_currency, hva_rate: show_plant_boxes(
311
+ selected_currency, hva_rate
312
+ ), # Adjusted function to return only the components
313
+ inputs=[currency_radio, acquisition_rate],
314
+ outputs=inventory, # Update each child in the inventory_row
315
+ )
316
+
317
+ # Update the inventory when the acquisition rate changes
318
+ acquisition_rate.change(
319
+ fn=lambda selected_currency, hva_rate: show_plant_boxes(
320
+ selected_currency, hva_rate
321
+ ), # Adjusted function to return only the components
322
+ inputs=[currency_radio, acquisition_rate],
323
+ outputs=inventory, # Update each child in the inventory_row
324
  )
325
 
326
  # Launch the Gradio application