Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define bounds for the variables in the optimization problem.
SciPy
from scipy.optimize import minimize bounds = [[1]] result = minimize(lambda x: x[0]**2 + x[1]**2, [1, 1], bounds=bounds) print(result.x)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a tuple instead of a list for bounds
Not enclosing bounds in a list
Using separate tuples without a list
✗ Incorrect
Bounds in scipy.optimize.minimize must be a list of tuples, each tuple specifying (min, max) for a variable.
2fill in blank
mediumComplete the code to define a linear constraint for the optimization problem.
SciPy
from scipy.optimize import LinearConstraint constraint = LinearConstraint([1], 0, 10) print(constraint)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a 1D list instead of 2D list
Using incorrect dimensions for the constraint matrix
✗ Incorrect
LinearConstraint expects a 2D array or list of lists for the constraint matrix.
3fill in blank
hardFix the error in the code by completing the constraint definition correctly.
SciPy
from scipy.optimize import minimize, LinearConstraint import numpy as np A = np.array([[1, 2], [3, 4]]) constraint = LinearConstraint(A, [1], [10, 20]) result = minimize(lambda x: x[0]**2 + x[1]**2, [1, 1], constraints=[constraint]) print(result.x)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting lower bounds higher than upper bounds
Using incorrect array sizes for bounds
✗ Incorrect
The lower bounds must be less than or equal to the upper bounds. Using zeros as lower bounds fits the problem constraints.
4fill in blank
hardFill both blanks to create a nonlinear constraint function and define it properly.
SciPy
from scipy.optimize import minimize, NonlinearConstraint import numpy as np def constraint_func(x): return x[0]**2 + x[1]**2 - [1] nlc = NonlinearConstraint(constraint_func, 0, [2]) result = minimize(lambda x: x[0] + x[1], [1, 1], constraints=[nlc]) print(result.x)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using bounds that do not match the function output range
Confusing the constant subtracted in the function
✗ Incorrect
The constraint function subtracts 1, so the lower bound is 0 and upper bound is 2 to allow values between 1 and 3 for the sum of squares.
5fill in blank
hardFill all three blanks to create a dictionary comprehension that filters variables based on bounds.
SciPy
variables = {'x': 5, 'y': 10, 'z': 15}
bounds = {'x': (0, 10), 'y': (5, 15), 'z': (10, 20)}
filtered = [1]: variables[[2]] for [3] in variables if bounds[[3]][0] <= variables[[3]] <= bounds[[3]][1]}
print(filtered) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently
Using values instead of keys in the comprehension
✗ Incorrect
The comprehension uses 'k' as the variable name for keys to filter variables within their bounds.