DuQuad  v1.0
Quadratic Programming Optimizations
 All Data Structures Files Functions Variables Typedefs Macros
fgm.c
Go to the documentation of this file.
1 /*
2  * FGM.c
3  *
4  * Created on: Aug 21, 2014
5  * Author: Sverre
6  */
7 
8 #include "fgm.h"
9 
10 // NOTE: z0 must be feasible
11 
13 {
14 
15  s->exitflag = 1;
16 
17  // z = z0, y = z
18  vector_copy(s->z0,s->z,N);
19  vector_copy(s->z,s->y,N);
20 
21  // Initialize the function value, and difference between the new and previous function value
22  real_t f = obj(s->z, s->H, s->c, s->temp1_dim_N);
23  real_t fnew = 0.0;
24  real_t f_diff = s->eps + 1;
25 
26  // Declaration and initialization of other necessary variables
27  real_t L = s->eigH_max;
28  real_t mu = s->eigH_min;
29  real_t one_over_L = 1.0/L;
30  real_t q = mu/L;
31  real_t alpha = 0.5;
32  real_t alpha_new = 0.0;
33  real_t theta = 0.0;
34  real_t beta = 0.0;
35  real_t alpha_pow2 = 0.0;
36 
37  // iteration counter
38  uint32_t niter = 0;
39 
40 
41  /* ##################################################################################
42  START WHILE-LOOP
43  ################################################################################## */
44  while(f_diff > s->eps)
45  {
46 
47  if (niter > s->maxiter){
48  //fprintf(stderr, "reached maximum number of iterations in FGM (inner problem)\n");
49  s->exitflag = 2;
50  break;
51  }
52 
53  /* *********************************************************************
54  Finding znew
55  ********************************************************************* */
56 
57  // Calculating gradient f'(y) = H*y + c
58  mtx_vec_mul(s->H,s->y,s->temp1_dim_N,N,N); // H * y
59  vector_add(s->c,s->temp1_dim_N,s->temp1_dim_N,N); // + c
60 
61  // z_new = y - (1/L * f'(y))
62  vector_scalar_mul(s->temp1_dim_N, one_over_L, s->temp1_dim_N, N); // 1/L * f'(y)
63  vector_sub(s->y, s->temp1_dim_N, s->znew, N); // y - ans
64 
65  // Projection znew on the feasible set made of lb and ub
66  if(!s->ub_is_inf)
67  vector_min(s->ub,s->znew,s->znew,N);
68  if(!s->lb_is_inf)
69  vector_max(s->lb,s->znew,s->znew,N);
70 
71  /* *********************************************************************
72  Finding ynew
73  ********************************************************************* */
74 
75  // Calculating beta: beta = alpha(1-alpha) / alpha²+alpha_new,
76  // where: alpha² = (1-alpha_new)*alpha² + q*alpha_new
77  alpha_pow2 = alpha*alpha;
78  theta = q - alpha_pow2;
79  alpha_new = 0.5 * (theta + sqrt((theta*theta) + 4.0*alpha_pow2));
80  beta = (alpha*(1.0-alpha)) / (alpha_pow2 + alpha_new);
81 
82  // ynew = znew + beta*(znew-z)
83  vector_sub(s->znew,s->z,s->temp1_dim_N,N); // znew-z
84  vector_scalar_mul(s->temp1_dim_N, beta, s->temp1_dim_N, N); // beta * (znew-z)
85  vector_add(s->znew,s->temp1_dim_N,s->ynew,N); // + znew
86 
87  /* *********************************************************************
88  Finding f_diff
89  ********************************************************************* */
90 
91  // Calculate the difference between the new and previous function value
92  fnew = obj(s->znew, s->H, s->c, s->temp1_dim_N);
93  f_diff = abs_2(fnew - f);
94 
95  /* *********************************************************************
96  Update the variables
97  ********************************************************************* */
98 
99  vector_copy(s->znew,s->z,N);
100  vector_copy(s->ynew,s->y,N);
101  f = fnew;
102  alpha = alpha_new;
103  niter++;
104 
105  } /* END WHILE-LOOP */
106 
107  vector_copy(s->znew,s->zopt,N);
108  s->fopt = fnew;
109 
110  return niter;
111 }
112 
113 // This function is not used by fgm or dfgm
114 void clean_up_FGM_C(struct Struct_FGM *s)
115 {
116  free_pointer(s->H);
117  free_pointer(s->c);
118  free_pointer(s->lb);
119  free_pointer(s->ub);
120  free_pointer(s->z0);
121  free_pointer(s->zopt);
122  free_pointer(s->z);
123  free_pointer(s->y);
124  free_pointer(s->znew);
125  free_pointer(s->ynew);
127 }
void vector_add(const real_t *v1, const real_t *v2, real_t *res, const uint32_t length)
void free_pointer(real_t *pointer)
real_t abs_2(const real_t a)
unsigned int uint32_t
Definition: typedefs.h:19
real_t eigH_min
Definition: fgm.h:37
real_t obj(const real_t *z, const real_t *H, const real_t *c, real_t *temp)
boolean ub_is_inf
Definition: fgm.h:35
void vector_min(const real_t *v1, const real_t *v2, real_t *res, const uint32_t length)
void clean_up_FGM_C(struct Struct_FGM *s)
Definition: fgm.c:114
real_t * ub
Definition: fgm.h:25
real_t * temp1_dim_N
Definition: fgm.h:44
real_t * znew
Definition: fgm.h:42
real_t eps
Definition: fgm.h:48
real_t * lb
Definition: fgm.h:24
real_t * y
Definition: fgm.h:41
real_t eigH_max
Definition: fgm.h:36
real_t * H
Definition: fgm.h:22
boolean lb_is_inf
Definition: fgm.h:34
uint32_t exitflag
Definition: fgm.h:31
void vector_sub(const real_t *v1, const real_t *v2, real_t *res, const uint32_t length)
uint32_t FGM(struct Struct_FGM *s)
Definition: fgm.c:12
void vector_copy(const real_t *v1, real_t *v2, const uint32_t length)
real_t * z
Definition: fgm.h:40
float64_t real_t
Definition: typedefs.h:25
void vector_scalar_mul(const real_t *v1, const real_t scalar, real_t *res, const uint32_t length)
void mtx_vec_mul(const real_t *mtx, const real_t *v, real_t *res, const uint32_t rows, const uint32_t cols)
real_t * ynew
Definition: fgm.h:43
real_t * z0
Definition: fgm.h:26
real_t * c
Definition: fgm.h:23
void vector_max(const real_t *v1, const real_t *v2, real_t *res, const uint32_t length)
real_t fopt
Definition: fgm.h:30
uint32_t maxiter
Definition: fgm.h:47
Definition: fgm.h:20
real_t * zopt
Definition: fgm.h:29
uint32_t N
Definition: head.h:33