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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* */
/* This file is part of the HiGHS linear optimization suite */
/* */
/* Available as open-source under the MIT License */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef __SRC_LIB_GRADIENT_HPP__
#define __SRC_LIB_GRADIENT_HPP__
#include "qpvector.hpp"
#include "runtime.hpp"
class Gradient {
Runtime& runtime;
QpVector gradient;
bool uptodate;
HighsInt numupdates = 0;
public:
Gradient(Runtime& rt)
: runtime(rt), gradient(QpVector(rt.instance.num_var)), uptodate(false) {}
void recompute() {
runtime.instance.Q.vec_mat(runtime.primal, gradient);
gradient += runtime.instance.c;
uptodate = true;
numupdates = 0;
}
QpVector& getGradient() {
if (!uptodate ||
numupdates >= runtime.settings.gradientrecomputefrequency) {
recompute();
}
return gradient;
}
void update(QpVector& buffer_Qp, double stepsize) {
gradient.saxpy(stepsize, buffer_Qp);
numupdates++;
}
};
#endif