Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 8 additions & 34 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from flask import Flask, render_template, request, jsonify, json
from flask import Flask, render_template, request, jsonify, json, redirect, url_for
from optlang import Model, Variable, Constraint, Objective
from formulator_service import INGREDIENT_DB
from formulator_service import INGREDIENT_PRICE
Expand Down Expand Up @@ -31,7 +31,7 @@ def result():
# print(ration)

# Computation starts here---

# animal ration(nutrient requirement)
animal_ration = ANIMAL_FEED_REQUIREMENT_DB[animal_type]

Expand Down Expand Up @@ -85,6 +85,8 @@ def result():
except Exception as e:
print(e)
# print(len(constraints))
#{{ ingredient_db[selected_ingredients[i]]["Price"] }}
#{% for i in range( 0, lengthOfIngredients): %}


# for name in ingredient_names:
Expand All @@ -98,7 +100,7 @@ def result():
obj = Objective(
sum(INGREDIENT_PRICE[name] * variables[name][ration] for name in ingredient_names),
direction='min'
)
)
# Objective( 58*x1+150*x2+60*x3+15*x4+50*x5+90*x6+700*x7+1300*x8+550*x9)

# print(obj)
Expand All @@ -113,37 +115,9 @@ def result():
for var_name , var in model.variables.items():
print(var_name, "=", var.primal)
# result = model.objective.value

# Calculate results for template
variable_quantity = model.variables
objValue = round(model.objective.value, 2)

variables_list = []
for a, n in variable_quantity.items():
value = a, round(n.primal, 2)
variables_list.append(value)

price = []
for i in ingredient_names:
value = i, INGREDIENT_PRICE[i]
price.append(value)

collection = dict(zip(variables_list, price))

return render_template("result.html",
animal_type = animal_type,
selected_ingredients = selected_ingredients,
ingredient_db = INGREDIENT_DB,
lengthOfIngredients = len(selected_ingredients),
collection = collection,
objValue = objValue)
else:
return render_template("result.html",
animal_type = None,
selected_ingredients = {},
ingredient_db = INGREDIENT_DB,
lengthOfIngredients = 0,
collection = {},
objValue = 0)
return render_template("result.html", animal_type = animal_type)

return redirect(url_for('form'))

app.run(debug=True)
12 changes: 4 additions & 8 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,20 +110,16 @@ def result():
# print("=",animal_db[animal_selected][selected_animal_stage][nutrient][bound])

if bound == "Min":
contraints_list.append(Constraint(temp_var_sum, lb=
animal_db[animal_selected][selected_animal_stage][nutrient][bound]))
contraints_list.append(Constraint(temp_var_sum, lb=animal_db[animal_selected][selected_animal_stage][nutrient][bound]))
print(temp_var_sum, ">=", animal_db[animal_selected][selected_animal_stage][nutrient][bound])

elif bound == "Max":
contraints_list.append(Constraint(temp_var_sum, ub=
animal_db[animal_selected][selected_animal_stage][nutrient][bound]))
contraints_list.append(Constraint(temp_var_sum, ub=animal_db[animal_selected][selected_animal_stage][nutrient][bound]))
print(temp_var_sum, "<=", animal_db[animal_selected][selected_animal_stage][nutrient][bound])

elif bound == "Equal":
contraints_list.append(Constraint(temp_var_sum, lb=
animal_db[animal_selected][selected_animal_stage][nutrient][bound]))
contraints_list.append(Constraint(temp_var_sum, ub=
animal_db[animal_selected][selected_animal_stage][nutrient][bound]))
contraints_list.append(Constraint(temp_var_sum, lb=animal_db[animal_selected][selected_animal_stage][nutrient][bound]))
contraints_list.append(Constraint(temp_var_sum, ub=animal_db[animal_selected][selected_animal_stage][nutrient][bound]))
print(temp_var_sum, ">=", animal_db[animal_selected][selected_animal_stage][nutrient][bound])
print(temp_var_sum, "<=", animal_db[animal_selected][selected_animal_stage][nutrient][bound])

Expand Down
Loading