summaryrefslogtreecommitdiff
path: root/nlp/demo.py
blob: 545df8bbf448906ea6a6f370ced0a29c2ec4aa0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from scipy.optimize import minimize, LinearConstraint, rosen, rosen_der, rosen_hess
import numpy as np
import matplotlib.pyplot as plt


def test1():
    fun = lambda x: x**2 + 2*x - 3
    x0 = np.asarray(1)
    res = minimize(fun, x0, bounds=[[0, None]], )
    print(res)


def test2():
    fun = lambda x: (x[0]-2)**2 + 4*(x[1]-1)**2
    x0 = [0, 0]
    cons = ({'type': 'ineq', 'fun': lambda x: 2 - x[0] - 2*x[1]})
    res = minimize(fun, np.asarray(x0),
                   method='slsqp',
                   constraints=cons, options={'disp': True})
    print(res)


def test3():
    fun = lambda x: -x[0]**2*x[1]
    x0 = np.asarray([0, 0])
    cons = ({'type': 'eq', 'fun': lambda x: x[0]**2+x[1]**2-1})
    res = minimize(fun, x0, constraints=cons, options={'disp': True})
    print(res)


def example_16_4():
    func = lambda x: (x[0]-1)**2 + (x[1] - 2.5)**2
    x0 = np.asarray([0, 0])

    # bounds = np.asarray([[0, None],
    #                      [0, None]])

    cons = ({'type': 'ineq', 'fun': lambda x: x[0] - 2*x[1] + 2},
            {'type': 'ineq', 'fun': lambda x: -x[0] - 2*x[1] + 6},
            {'type': 'ineq', 'fun': lambda x: -x[0] + 2*x[1] + 2},
            {'type': 'ineq', 'fun': lambda x: x[0]},
            {'type': 'ineq', 'fun': lambda x: x[1]})

    res = minimize(func, x0,
                   # bounds=bounds,
                   constraints=cons,
                   jac=lambda x: np.asarray([2*(x[0]-1), 2*(x[1] - 2.5)]),
                   # hess=lambda x: np.asarray([[2, 0], [0, 2]]),
                   method='SLSQP')
    print(res)


if __name__ == '__main__':
    example_16_4()