-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquadraticObjective.py
67 lines (49 loc) · 1.55 KB
/
quadraticObjective.py
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
56
57
58
59
60
61
62
63
64
65
66
67
# Optimization for Engineers - Dr.Johannes Hild
# quadratic objective
# Do not change this file
# n-dimensional quadratic function mapping x -> 0.5*x'*A*x + b'*x +c
# Class parameters:
# A: real valued matrix nxn
# b: column vector in R^n
# c: real number
# Input Definition:
# x: vector in R**n (domain space)
# Output Definition:
# objective(): real number, evaluation at x for parameters p
# gradient(): vector in R**n, evaluation of gradient wrt x
# hessian(): matrix in R**nxn, evaluation of hessian wrt x
# Required files:
# < none >
# [myValue,myGradient,myHessian]=quadraticConstraint([1;1],[1,0;0,1],[1;1],1);
# should return
# myValue=4; myGradient=[2;2]; myHessian=[1,0;0,1];
# Test cases:
# A = np.eye(2)
# b = np.ones((2,1))
# c = 1
# myObjective = quadraticObjective(A,b,c)
# y = myObjective.objective(b)
# should return y = 4
# grad = myObjective.gradient(b)
# should return grad = [[2],[2]]
# hess = myObjective.hessian(b)
# should return hess = [[1, 0],[0, 1]]
import numpy as np
def matrnr():
# set your matriculation number here
matrnr = 0
return matrnr
class quadraticObjective:
def __init__(self, A: np.array, b: np.array, c: float):
self.A = A
self.b = b
self.c = c
def objective(self, x: np.array):
f = 0.5 * (x.T @ (self.A @ x)) + self.b.T @ x + self.c
return f
def gradient(self, x: np.array):
g = self.A @ x + self.b
return g
def hessian(self, x: np.array):
h = self.A
return h