An Assignment For Optimizing Non-Linear Functions to Machine Learning Students

Sulstice
2 min readFeb 26, 2024

--

The Problem

The function was described as:

Where our goal is to minimize f(x)with a constraint to the x0and x1 parameters. A 3D depiction of that can be found like this as a parabolic function. If we check through wolfram alpha.

To get students adjusted into turning mathematical functions into code you can set up two functions. You will be needing scipy for this:

pip install scipy

Set up two functions:

from scipy.optimize import minimize

def objective(x):
'write objective function'
return
def constraint(x):
return x[0]**2 + x[1]**2 - 10

Tell students to write the objective function with an example on the constraint. It makes it pretty easy for the students to see how to write something.

decision_variables means the starting point of the function so in our case x0 and x1 are [0,0] starting and we will optimize from there.

decision_variables = [0, 0]
solution = minimize(objective, x0, constraints={'type': 'ineq', 'fun': constraint})

The solution

To the problem is to see an optimization performed successfully:

message: Optimization terminated successfully
success: True
status: 0
fun: 25.0
x: [ 6.000e+00 8.000e+00]
nit: 1
jac: [-6.000e+00 -8.000e+00]
nfev: 4
njev: 1

Happy

--

--

Sulstice
Sulstice

No responses yet