0% found this document useful (0 votes)
18 views

Python Worksheet 4

Uploaded by

sharmayash.6789
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Python Worksheet 4

Uploaded by

sharmayash.6789
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

WORKSHEET 1.4

Student Name: Anshuman Singh UID 22BCS11371


Branch@: CSE Section/Group: 614 A
Semester:4th Date of Performance: 10 APRIL 2024
Subject Name: Numerical Methods and Optimizations using Python
Subject Code: 22CSH-259

1. Aim:
a. Suppose you are planning to invest in two types of stocks: Stock A and
Stock B. You have a total of $10,000 to invest. You expect Stock A to yield a
return of 8% per annum and Stock B to yield a return of 10% per annum.
Maximize the total return on investment.

However, you also have certain parameters:

Ø You want to invest at least $2,000 in Stock A.

Ø You want to invest at least $3,000 in Stock B.

Ø You cannot invest more than $7,000 in Stock A.

Ø You cannot invest more than $6,000 in Stock B.

2. Source Code:
a.)

import numpy as np
from scipy.optimize import linprog

# Objective function coefficients (negative because linprog minimizes)


c = [-0.08, -0.10]
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

# Inequality constraints matrix


A = [[1, 0], # Constraint for Stock A investment (>= 2000)
[0, 1], # Constraint for Stock B investment (>= 3000)
[-1, 0], # Constraint for Stock A investment (<= 7000)
[0, -1]] # Constraint for Stock B investment (<= 6000)

# Inequality constraints bounds


b = [2000, # Minimum investment in Stock A
3000, # Minimum investment in Stock B
7000, # Maximum investment in Stock A
6000] # Maximum investment in Stock B

# Solve the linear programming problem


res = linprog(c, A_ub=A, b_ub=b, method='highs')

# Check if the optimization was successful


if res.success:
# Extract results
stock_A_qty = int(round(res.x[0]))
stock_B_qty = int(round(res.x[1]))

# Calculate total investment and total return


total_investment = stock_A_qty * 1000 + stock_B_qty * 1000
total_return = -res.fun * total_investment

# Print results
print(f"Number of Stock A: {stock_A_qty}")
print(f"Number of Stock B: {stock_B_qty}")
print(f"Total Investment: ${total_investment}")
print(f"Total Return: ${total_return}")
else:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

print("Optimization was not successful.")

b.)

import numpy as np
from scipy.optimize import linprog

# Objective function coefficients (negative because linprog minimizes)


c = [-10, -15] # Profit from Product X and Product Y

# Inequality constraints matrix


A = [[2, 3], # Labor constraint for Product X and Product Y
[1, 2]] # Material constraint for Product X and Product Y

# Inequality constraints bounds


b = [100, # Maximum available labor (100 hours)
80] # Maximum available material (80 units)

# Solve the linear programming problem


res = linprog(c, A_ub=A, b_ub=b, method='highs')

# Check if the optimization was successful


if res.success:
# Extract results
product_X_qty = int(round(res.x[0]))
product_Y_qty = int(round(res.x[1]))

# Calculate total profit


total_profit = -res.fun # Convert negative minimum to positive maximum

# Print results
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

print(f"Number of Product X: {product_X_qty}")


print(f"Number of Product Y: {product_Y_qty}")
print(f"Total Profit: ${total_profit}")
else:
print("Optimization was not successful.")

3. Screenshot of Outputs:

PROBLEM A:

PROBLEM B:

You might also like