Ot Lab - 1-12
Ot Lab - 1-12
Source Code:
import numpy as np
mx1 = np.array([[5, 10], [15, 20]])
mx2 = np.array([[25, 30], [35, 40]])
print("Matrix1 =\n",mx1) print("\
nMatrix2 =\n",mx2)
print ("\nAddition of two matrices: ")
print (np.add(mx1,mx2))
print ("\nSubtraction of two matrices:
") print (np.subtract(mx1,mx2))
print ("\nMatrix Division: ")
print (np.divide(mx1,mx2))
print ("\nMultiplication of two matrices: ")
print (np.multiply(mx1,mx2))
Output:
Question No.2: Write a python program for Min Cost Path.
Source Code:
R=3
C=3
def minCost(cost, m, n):
# initialization
tc = [[0 for x in range(C)] for x in
range(R)] # base case
tc[0][0] = cost[0][0]
# total cost(tc) array
for i in range(1, m + 1):
tc[i][0] = tc[i-1][0] + cost[i]
[0] for j in range(1, n + 1):
tc[0][j] = tc[0][j-1] + cost[0]
[j] for i in range(1, m + 1):
for j in range(1, n + 1):
tc[i][j] = min(tc[i-1][j-1], tc[i-1][j], tc[i][j-1]) + cost[i][j]
return tc[m][n]
cost = [[1, 5, 3],
[7, 7, 4],
[8, 5, 3]]
print("Total Cost:",minCost(cost, 2, 1))
Output:
Question No.3: Write a python program for Finding Maximum Number in An
Array.
Source Code:
def myMax(list1):
max = list1[0]
for x in list1:
if x > max:
max = x
return max
list1 = [10, 20, 4, 45, 99]
print("Largest element is:", myMax(list1))
Output:
#Solution
print("x1 =", p.value(x1))
print("x2 =", p.value(x2))
print("x3 =", p.value(x3))
print("Objective = ", p.value(Lp_prob.objective))
Question No.6: Write a python program for Queuing Problem (QP) on Simulating
Bank Queues.
Source Code:
import numpy as np
import matplotlib.pyplot as plt
# Function to simulate the bank queue
def simulate_bank_queue(num_tellers, arrival_rate, service_rate, sim_time):
# Initialize variables
time = 0
queue_length = []
wait_times = []
# Driver's Code
if name == ' main ':
arr = [['a', 2, 100], # Job Array
['b', 1, 19],
['c', 2, 27],
['d', 1, 25],
['e', 3, 15]]
print("Following is maximum profit sequence of jobs")
# Function Call
printJobScheduling(arr, 3)
Output:
Question No.8: Write a python program for Game Theory.
Source Code:
import numpy as np
from scipy.optimize import linprog
# Define the payoff matrix for player 1
payoff_matrix_p1 = np.array([[3, 0],
[5, 1]])
# Define the payoff matrix for player 2 (transpose of player 1 matrix)
payoff_matrix_p2 = np.array([[3, 5],
[0, 1]])
# Function to calculate Nash equilibrium using linear programming
def nash_equilibrium(payoff_matrix):
num_strategies =
payoff_matrix.shape[0]
# Objective function: maximize the minimum value (minimax strategy)
c = [-1] * num_strategies # Minimize the negative value
# Constraints: probabilities must sum to 1, and each probability should
be non-negative
A_eq = np.ones((1, num_strategies))
b_eq = [1]
# Bounds for probabilities: each should be between 0 and 1
bounds = [(0, 1) for _ in range(num_strategies)]
# Solve the linear programming problem
res = linprog(c, A_eq=A_eq, b_eq=b_eq, bounds=bounds)
if res.success:
return res.x
else:
return None
# Get Nash equilibrium for player 1 and player 2
nash_p1 = nash_equilibrium(payoff_matrix_p1)
nash_p2 = nash_equilibrium(payoff_matrix_p2.T)
# Constraints
# Each worker is assigned to at most 1 task.
for i in range(num_workers):
solver.Add(solver.Sum([x[i, j] for j in range(num_tasks)]) <= 1)
# Each task is assigned to exactly one worker.
for j in range(num_tasks):
solver.Add(solver.Sum([x[i, j] for i in range(num_workers)]) == 1)
# Objective
objective_terms = []
for i in range(num_workers):
for j in range(num_tasks):
objective_terms.append(costs[i][j] * x[i, j])
solver.Minimize(solver.Sum(objective_terms))
# Solve
print(f"Solving with {solver.SolverVersion()}")
status = solver.Solve()
# Print solution
if status == pywraplp.Solver.OPTIMAL or status == pywraplp.Solver.FEASIBLE:
print(f"Total cost = {solver.Objective().Value()}\n")
for i in range(num_workers):
for j in range(num_tasks):
# Test if x[i,j] is 1 (with tolerance for floating point arithmetic).
if x[i, j].solution_value() > 0.5:
print(f"Worker {i} assigned to task {j}." + f" Cost: {costs[i][j]}")
else:
print("No solution found.")
if name == " main ":
main()
Output:
key = (n, W)
if key not in lookup:
include = v[n] + knapsack(v, w, n - 1, W - w[n],
lookup) exclude = knapsack(v, w, n - 1, W, lookup)
lookup[key] = max(include, exclude)
return lookup[key]
cost = matrixChainMultiplication(dims, i, k)
cost += matrixChainMultiplication(dims, k, j)