below
Here is a template for python-lab.html, in this case minimizing \((x-2)^2 + (y-1)^2 + x y + x + y\) subject to the constraints
$$ x \leq 1, \quad y \leq 1\quad\text{and}\quad x + y \geq 1. $$
``` from scipy.optimize import minimize
def f(v): x, y = v r = (x - 2)2 + (y-1)2 + x*y +x + y return r # minimize # return -r # maximize
def g1(v): x, y = v return 1 - x # must be >= 0
def g2(v): x, y = v return 1 - y # must be >= 0
def g3(v): x, y = v return x + y - 1 # must be >= 0
cons = [ {"type": "ineq", "fun": g1}, {"type": "ineq", "fun": g2}, {"type": "ineq", "fun": g3}, ]
res = minimize(f, [1, 1], constraints=cons) print(res.x) ```
Here [1,1] is an initial point used as a guess.
