You are on page 1of 3

KALINDI COLLEGE

UNIVERSITY OF DELHI

NUMERICAL OPTIMIZATION
PRACTICALS
SUBMITTED BY: K.meghna
22570020
B.SC(HONS.) COMPUTER SCIENCE
SUBMITTED TO: MS. REENA JAIN MA’AM
PRACTICAL 6
WAP TO SOLVE CONSTRAINT OPTIMIZATION
PROBLEM.
#in this the objective function is x^2+y^2 there is a constraint x+y−1≥0.
import numpy as np
from scipy.optimize import minimize

# Define the objective function to minimize


def objective(x):
return x[0]**2 + x[1]**2 # Example objective function (minimize x^2 + y^2)

# Define the inequality constraint


def constraint(x):
return x[0] + x[1] - 1 # Example constraint (x + y - 1 >= 0)

# Initial guess
initial_guess = np.array([0, 0])

# Define the bounds for variables


bounds = [(None, None), (None, None)] # No specific bounds in this example
# Define the constraint as a dictionary
constraint_dict = {'type': 'ineq', 'fun': constraint}
# Solve the constrained optimization problem using minimize
result = minimize(objective, initial_guess, bounds=bounds,
constraints=constraint_dict)
# Display the results
print("Optimal Point:", result.x)
print("Optimal Value:", result.fun)
#The minimize function is used with the bounds parameter to define the variable
bounds and the constraints parameter to specify the inequality constraint

OUTPUT:

You might also like