DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
WORKSHEET- 4
Student Name: Anushka Jaiswal UID: 22BCS15089
Branch: CSE Section/Group: 818-B
Semester: 4 Date of Performance: 16/04/24
Subject Name: Numerical Methods and Optimization on using Python
Subject Code: 22CSH-259
1. Aim:
You are a farmer planning to grow two types of crops, Crop A and Crop B, on your farmland.
Each type of crop requires a certain amount of water and fertilizer to grow, and you have a
limited amount of both resources available. The objective is to maximize the total yield of
crops while respecting the resource constraints. Maximize the total yield of crops.
Crop details:
Crop A: Requires 5 gallons of water and 2 bags of fertilizer per acre.
Crop B: Requires 3 gallons of water and 4 bags of fertilizer per acre.
Constraints:
Ø Limited availability of water: You have 50 gallons of water available.
Ø Limited availability of fertilizer: You have 40 bags of fertilizer available.
2. Source Code:
from pulp import *
# Define the problem
prob = LpProblem("Crop_Optimization", LpMaximize)
# Define decision variables
x1 = LpVariable("Crop_A_Acres", lowBound=0, cat='Continuous')
x2 = LpVariable("Crop_B_Acres", lowBound=0, cat='Continuous')
# Define objective function
prob += 40*x1 + 50*x2, "Total_Yield_of_Crops"
# Define constraints
prob += 5*x1 + 3*x2 <= 50, "Water_Constraint"
prob += 2*x1 + 4*x2 <= 40, "Fertilizer_Constraint"
# Solve the problem
[Link]()
# Output the results
print("Optimal Solution:")
for v in [Link]():
print([Link], "=", [Link])
print("Total Yield of Crops:", value([Link]))
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
3. Screenshot of Outputs:
4. Learning Outcomes
1. Understanding how to formulate optimization problems using linear programming.
2. Learning to use PuLP, a Python library for linear programming, to solve optimization
problems.
3. Understanding the importance of defining decision variables, objective function, and
constraints in optimization models.
4. Gaining insights into interpreting and analyzing the results of optimization models.
5. Understanding the significance of optimization techniques in resource allocation and
decision-making processes.