0
0
SciPydata~10 mins

Nonlinear constraint optimization in SciPy - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the function for nonlinear optimization from scipy.

SciPy
from scipy.optimize import [1]
Drag options to blanks, or click blank then click option'
Aminimize
Blinprog
Ccurve_fit
Droot
Attempts:
3 left
💡 Hint
Common Mistakes
Using linprog which is for linear programming.
Using curve_fit which is for curve fitting.
Using root which is for finding roots of equations.
2fill in blank
medium

Complete the code to define the nonlinear constraint function that must be greater than or equal to zero.

SciPy
def constraint(x):
    return x[0]**2 + x[1]**2 - [1]
Drag options to blanks, or click blank then click option'
A1
B0
C-1
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 which would change the constraint meaning.
Using negative values which are not correct for this constraint.
Using 2 which changes the radius.
3fill in blank
hard

Fix the error in the constraint dictionary by completing the 'type' field correctly.

SciPy
nonlinear_constraint = {'type': '[1]', 'fun': constraint}
Drag options to blanks, or click blank then click option'
Amax
Beq
Cmin
Dineq
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'eq' which means equality constraint.
Using 'min' or 'max' which are not valid types.
4fill in blank
hard

Fill both blanks to complete the call to minimize with the objective function and constraints.

SciPy
result = minimize([1], x0, constraints=[2])
Drag options to blanks, or click blank then click option'
Aobjective
Bx0
C[nonlinear_constraint]
Dconstraints
Attempts:
3 left
💡 Hint
Common Mistakes
Passing x0 as the first argument instead of the objective function.
Passing constraints directly without a list.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps indices to their optimized values if the value is greater than zero.

SciPy
positive_vars = { [1]: result.x[[2]] for [3] in range(len(result.x)) if result.x[[3]] > 0 }
Drag options to blanks, or click blank then click option'
A{var
Bi
Cvar
D}
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string like '{var' for the key.
Using incorrect loop variable names.
Not filtering values greater than zero.