oh-my-dear-ai commited on
Commit
4b0a34a
·
1 Parent(s): f10d8cf

Update app.py plants.csv requirements.txt

Browse files
Files changed (3) hide show
  1. app.py +132 -52
  2. plants.csv +16 -16
  3. requirements.txt +0 -0
app.py CHANGED
@@ -3,30 +3,59 @@ import pandas as pd
3
  import pulp
4
  import numpy as np
5
 
6
- LANGUAGE = "en"
7
-
8
  # Define the quality tiers and names for the plants
9
  PLANTS_TIERS = {
10
- "radiant": "金品质",
11
- "flourishing": "紫品质",
12
- "hardy": "蓝品质",
13
- "feeble": "白品质",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  }
15
 
16
- PLANTS_I18N = {
17
- "fanged_geranium": "毒牙天竺葵",
18
- "gillyweed": "鳃囊草",
19
- "rose": "玫瑰",
20
- "puffapod": "泡泡豆荚",
21
- "nifflers_fancy": "嗅幻草",
22
- "ladys_mantle": "羽衣草",
23
- "mandrake": "曼德拉草",
24
- "chinese_chomping_cabbage": "中国咬人甘蓝",
25
- "peony": "牡丹",
26
- "begonia": "秋海棠",
27
- "mayflower": "五月花",
28
- "hydrangea": "绣球",
29
- "daffodil": "水仙",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  }
31
 
32
  # Import and process plant data
@@ -50,7 +79,7 @@ def calculator(*args):
50
  and inventory constraints.
51
 
52
  Args:
53
- *args (tuple): A tuple where the first element is Gabby's gold budget,
54
  and the subsequent elements represent the stock levels of
55
  each plant type.
56
 
@@ -59,15 +88,25 @@ def calculator(*args):
59
  the total gold earned, and the remaining inventory.
60
  """
61
  budget = args[0] # 葭碧预算
62
- stocks = np.array(args[1:]) # 植物库存
 
63
 
64
  # 植物名称列表
65
- plants_en = [f"{row['tier']}_{row['species']}" for index, row in df.iterrows()]
 
 
 
66
 
67
  gold = np.array(df["gold"]) # 植物单价
68
 
69
- # 创建问题实例,目标是最大化存货数量
70
- prob = pulp.LpProblem("Maximize_Sale", pulp.LpMaximize)
 
 
 
 
 
 
71
 
72
  # 决策变量,售出每种植物的件数,必须是整数
73
  x = pulp.LpVariable.dicts("x", range(len(stocks)), lowBound=0, cat="Integer")
@@ -76,8 +115,8 @@ def calculator(*args):
76
  for i in range(len(stocks)):
77
  x[i].upBound = stocks[i]
78
 
79
- # 目标函数:最大化总的存货数量
80
- prob += pulp.lpSum([stocks[i] - x[i] for i in range(len(stocks))])
81
 
82
  # 约束条件:每类产品售出数量乘以单价之和等于总价格
83
  prob += pulp.lpSum([gold[i] * x[i] for i in range(len(stocks))]) == budget
@@ -94,50 +133,91 @@ def calculator(*args):
94
  for i, v in x.items():
95
  if v.varValue:
96
  if v.varValue > 0:
97
- solution.append(f"{plants_en[i]}: {int(v.varValue)}\n")
98
  sold += int(v.varValue) * gold[i]
99
 
100
- return f"Optimal solution found:\n\n{''.join(solution)}\nTotal price: {sold}\nStock: {pulp.value(prob.objective)}"
 
101
  else:
102
- return "No solution found."
103
 
104
 
105
- # Create a Gradio interface with a column layout
106
  with gr.Blocks() as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
  with gr.Column():
108
  # Add a row for the budget input
109
- with gr.Row():
110
- budget = gr.Number(
111
- label="Gold Budget", value=0, minimum=0, maximum=15000, step=100
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
  )
113
 
114
- # Add a row for the plant inventory inputs
115
- with gr.Row():
116
- inventory = {}
117
- for index, row in df.iterrows():
118
- inventory[f"{row['tier']}_{row['species']}"] = gr.Number(
119
- label=f"{row['tier']}_{row['species']}",
120
- value=0,
121
- minimum=0,
122
- maximum=500,
123
- step=10,
124
- )
125
 
126
  # Add a button to trigger the calculation
127
- submit_btn = gr.Button(value="Calculate")
128
 
129
- # Add a row for the result textbox
130
- with gr.Row():
131
- result = gr.Textbox()
132
 
133
  # Set up the button click event to call the calculator function
134
  submit_btn.click(
135
  calculator,
136
- inputs=[budget] + list(inventory.values()),
137
  outputs=[result],
138
  api_name=False,
139
  )
140
 
 
141
  # Launch the Gradio application
142
- if __name__ == "__main__":
143
- demo.launch(show_api=False)
 
3
  import pulp
4
  import numpy as np
5
 
 
 
6
  # Define the quality tiers and names for the plants
7
  PLANTS_TIERS = {
8
+ "radiant": "RADIANT",
9
+ "flourishing": "FLOURISHING",
10
+ "hardy": "HARDY",
11
+ "feeble": "FEEBLE",
12
+ }
13
+
14
+ PLANTS_LABLES = {
15
+ "fanged_geranium": "Fanged Geranium",
16
+ "gillyweed": "Gillyweed",
17
+ "rose": "Rose",
18
+ "puffapod": "Puffapod",
19
+ "nifflers_fancy": "Niffler's Fancy",
20
+ "ladys_mantle": "Lady's Mantle",
21
+ "mandrake": "Mandrake",
22
+ "chinese_chomping_cabbage": "Chinese Chomping Cabbage",
23
+ "peony": "Peony",
24
+ "begonia": "Begonia",
25
+ "mayflower": "Mayflower",
26
+ "hydrangea": "Hydrangea",
27
+ "daffodil": "Daffodil",
28
  }
29
 
30
+ INTERFACE_TEXTS = {
31
+ "cn": {
32
+ "gold_label": "葭碧の金币预算:",
33
+ "strategies_label": "请选择凑单策略:",
34
+ "clear_btn_label": "❌清除",
35
+ "calculate_btn_label": "🛠计算",
36
+ "output_label": "计算结果:",
37
+ "strategy_options": [
38
+ ("最小化售出株数(优先出售高价植物)", "MaximizeStock"),
39
+ ("最大化售出株数(优先出售低价植物)", "MinimizeStock"),
40
+ ],
41
+ },
42
+ "en": {
43
+ "gold_label": "Gabby's Gold Budget:",
44
+ "strategies_label": "Select a strategy:",
45
+ "clear_btn_label": "❌Clear",
46
+ "calculate_btn_label": "🛠Calculate",
47
+ "output_label": "Output:",
48
+ "strategy_options": [
49
+ (
50
+ "Minimize the number of plants sold (prioritize high-priced plants)",
51
+ "MaximizeStock",
52
+ ),
53
+ (
54
+ "Maximize the number of plants sold (prioritize low-priced plants)",
55
+ "MinimizeStock",
56
+ ),
57
+ ],
58
+ },
59
  }
60
 
61
  # Import and process plant data
 
79
  and inventory constraints.
80
 
81
  Args:
82
+ *args (tuple): A tuple where the first element is Gabby's gold budget, the selected strategy for selling plants,
83
  and the subsequent elements represent the stock levels of
84
  each plant type.
85
 
 
88
  the total gold earned, and the remaining inventory.
89
  """
90
  budget = args[0] # 葭碧预算
91
+ strategy = args[1] # 出售策略
92
+ stocks = np.array([x if x else 0 for x in args[2:]]) # 植物库存
93
 
94
  # 植物名称列表
95
+ plants_names = [
96
+ f"{PLANTS_TIERS[row['tier']]} {PLANTS_LABLES[row['species']]}"
97
+ for index, row in df.iterrows()
98
+ ]
99
 
100
  gold = np.array(df["gold"]) # 植物单价
101
 
102
+ if sum([gold[i] * stocks[i] for i in range(len(stocks))]) < budget:
103
+ return "No solution found.\nThe total value of the plants is lower than the budget."
104
+
105
+ # 创建问题实例,根据出售策略设定优化目标
106
+ if strategy == "MaximizeStock":
107
+ prob = pulp.LpProblem("Maximize_Stock", pulp.LpMinimize)
108
+ else:
109
+ prob = pulp.LpProblem("Maximize_Stock", pulp.LpMaximize)
110
 
111
  # 决策变量,售出每种植物的件数,必须是整数
112
  x = pulp.LpVariable.dicts("x", range(len(stocks)), lowBound=0, cat="Integer")
 
115
  for i in range(len(stocks)):
116
  x[i].upBound = stocks[i]
117
 
118
+ # 目标函数:最大化 / 最小化 售出株数
119
+ prob += pulp.lpSum([x[i] for i in range(len(stocks))])
120
 
121
  # 约束条件:每类产品售出数量乘以单价之和等于总价格
122
  prob += pulp.lpSum([gold[i] * x[i] for i in range(len(stocks))]) == budget
 
133
  for i, v in x.items():
134
  if v.varValue:
135
  if v.varValue > 0:
136
+ solution.append(f"{plants_names[i]}: {int(v.varValue)}\n")
137
  sold += int(v.varValue) * gold[i]
138
 
139
+ return f"Optimal solution found:\n\n{''.join(solution)}\nTotal Price: {sold}\nCount: {int(pulp.value(prob.objective))}"
140
+
141
  else:
142
+ return "No solution found.\n"
143
 
144
 
 
145
  with gr.Blocks() as demo:
146
+ gr.Markdown(
147
+ """<center><font size=8>HP-Magic-Awakened Herbologist Toolkit👾</center>"""
148
+ )
149
+
150
+ # Language selection dropdown
151
+ # language = gr.Radio(
152
+ # choices=["cn", "en"],
153
+ # value="cn",
154
+ # label="Language",
155
+ # info="Select the interface language:",
156
+ # interactive=True,
157
+ # )
158
+
159
+ # Create a Gradio interface with a column layout
160
  with gr.Column():
161
  # Add a row for the budget input
162
+ budget = gr.Number(
163
+ label="Gold",
164
+ info="Gabby's Gold Budget:", # "葭碧の金币预算:",
165
+ value=0,
166
+ minimum=0,
167
+ maximum=15000,
168
+ step=100,
169
+ )
170
+
171
+ # Add a radio selection for the strategy
172
+ selected_strategy = gr.Radio(
173
+ [
174
+ (
175
+ "Minimize the number of plants sold (prioritize high-priced plants)",
176
+ "MaximizeStock",
177
+ ),
178
+ (
179
+ "Maximize the number of plants sold (prioritize low-priced plants)",
180
+ "MinimizeStock",
181
+ ),
182
+ ],
183
+ value="MaximizeStock",
184
+ label="Strategies",
185
+ info="Select a strategy:",
186
+ )
187
+
188
+ # Add a row for the plant inventory inputs
189
+ with gr.Row():
190
+ inventory = {}
191
+ for _, row in df.iterrows():
192
+ inventory[f"{row['species']}_{row['tier']}"] = gr.Number(
193
+ label=f"{PLANTS_LABLES[row['species']]} - {PLANTS_TIERS[row['tier']]}",
194
+ value=0,
195
+ minimum=0,
196
+ maximum=500,
197
+ step=10,
198
+ visible=row["gold"] != 0,
199
  )
200
 
201
+ # Add a row for the Clear and Calculate buttons
202
+ with gr.Row():
203
+ clear_btn = gr.ClearButton(list(inventory.values()), size="sm", value="❌Clear")
 
 
 
 
 
 
 
 
204
 
205
  # Add a button to trigger the calculation
206
+ submit_btn = gr.Button(value="🛠Calculate")
207
 
208
+ # Add a row for the result textbox
209
+ with gr.Row():
210
+ result = gr.Textbox(label="Output")
211
 
212
  # Set up the button click event to call the calculator function
213
  submit_btn.click(
214
  calculator,
215
+ inputs=[budget] + [selected_strategy] + list(inventory.values()),
216
  outputs=[result],
217
  api_name=False,
218
  )
219
 
220
+
221
  # Launch the Gradio application
222
+ demo.queue(api_open=False)
223
+ demo.launch(max_threads=5, share=False)
plants.csv CHANGED
@@ -10,11 +10,11 @@ gold,gems,species,tier,amount
10
  42,0,rose,radiant,0
11
  34,0,rose,flourishing,0
12
  30,0,rose,hardy,0
13
- ,0,rose,feeble,0
14
  85,0,puffapod,radiant,0
15
  62,0,puffapod,flourishing,0
16
  55,0,puffapod,hardy,0
17
- ,0,puffapod,feeble,0
18
  0,5,nifflers_fancy,radiant,0
19
  260,0,nifflers_fancy,flourishing,0
20
  234,0,nifflers_fancy,hardy,0
@@ -22,32 +22,32 @@ gold,gems,species,tier,amount
22
  105,0,ladys_mantle,radiant,0
23
  96,0,ladys_mantle,flourishing,0
24
  76,0,ladys_mantle,hardy,0
25
- ,0,ladys_mantle,feeble,0
26
  24,0,mandrake,radiant,0
27
  22,0,mandrake,flourishing,0
28
  15,0,mandrake,hardy,0
29
- ,0,mandrake,feeble,0
30
  57,0,chinese_chomping_cabbage,radiant,0
31
  40,0,chinese_chomping_cabbage,flourishing,0
32
  36,0,chinese_chomping_cabbage,hardy,0
33
- ,0,chinese_chomping_cabbage,feeble,0
34
  0,2,peony,radiant,0
35
  58,0,peony,flourishing,0
36
  45,0,peony,hardy,0
37
  1,0,peony,feeble,0
38
  72,0,begonia,radiant,0
39
  58,0,begonia,flourishing,0
40
- ,0,begonia,hardy,0
41
- ,0,begonia,feeble,0
42
  186,0,mayflower,radiant,0
43
- ,0,mayflower,flourishing,0
44
- ,0,mayflower,hardy,0
45
- ,0,mayflower,feeble,0
46
  215,0,hydrangea,radiant,0
47
- ,0,hydrangea,flourishing,0
48
- ,0,hydrangea,hardy,0
49
- ,0,hydrangea,feeble,0
50
  0,2,daffodil,radiant,0
51
- ,0,daffodil,flourishing,0
52
- ,0,daffodil,hardy,0
53
- ,0,daffodil,feeble,0
 
10
  42,0,rose,radiant,0
11
  34,0,rose,flourishing,0
12
  30,0,rose,hardy,0
13
+ 1,0,rose,feeble,0
14
  85,0,puffapod,radiant,0
15
  62,0,puffapod,flourishing,0
16
  55,0,puffapod,hardy,0
17
+ 1,0,puffapod,feeble,0
18
  0,5,nifflers_fancy,radiant,0
19
  260,0,nifflers_fancy,flourishing,0
20
  234,0,nifflers_fancy,hardy,0
 
22
  105,0,ladys_mantle,radiant,0
23
  96,0,ladys_mantle,flourishing,0
24
  76,0,ladys_mantle,hardy,0
25
+ 1,0,ladys_mantle,feeble,0
26
  24,0,mandrake,radiant,0
27
  22,0,mandrake,flourishing,0
28
  15,0,mandrake,hardy,0
29
+ 1,0,mandrake,feeble,0
30
  57,0,chinese_chomping_cabbage,radiant,0
31
  40,0,chinese_chomping_cabbage,flourishing,0
32
  36,0,chinese_chomping_cabbage,hardy,0
33
+ 1,0,chinese_chomping_cabbage,feeble,0
34
  0,2,peony,radiant,0
35
  58,0,peony,flourishing,0
36
  45,0,peony,hardy,0
37
  1,0,peony,feeble,0
38
  72,0,begonia,radiant,0
39
  58,0,begonia,flourishing,0
40
+ 45,0,begonia,hardy,0
41
+ 1,0,begonia,feeble,0
42
  186,0,mayflower,radiant,0
43
+ 150,0,mayflower,flourishing,0
44
+ 117,0,mayflower,hardy,0
45
+ 1,0,mayflower,feeble,0
46
  215,0,hydrangea,radiant,0
47
+ 150,0,hydrangea,flourishing,0
48
+ 135,0,hydrangea,hardy,0
49
+ 1,0,hydrangea,feeble,0
50
  0,2,daffodil,radiant,0
51
+ 50,0,daffodil,flourishing,0
52
+ 45,0,daffodil,hardy,0
53
+ 1,0,daffodil,feeble,0
requirements.txt CHANGED
Binary files a/requirements.txt and b/requirements.txt differ