Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
document last changes
[simgrid.git] / src / nws_portability / Forecast / exp_smooth.c
1 /* $Id$ */
2
3 #include <unistd.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <math.h>
7 #include <string.h>
8 #include <strings.h>
9
10 #include "fbuff.h"
11 #include "exp_smooth.h"
12
13 struct exp_smooth_state
14 {
15         fbuff series;                   /* the series so far */
16         fbuff time_stamps;              /* the time stamps */
17         double last_pred;
18         double gain;
19 };
20
21 /*
22  * init local state.  can save a copy of the pointer to the
23  * series and time stamps, if desired
24  */
25 char *
26 InitExpSmooth(fbuff series, fbuff time_stamps, char *params)
27 {
28         struct exp_smooth_state *state;
29         double gain;
30         char *p_str;
31         
32         state = (struct exp_smooth_state *)
33                 malloc(sizeof(struct exp_smooth_state));
34         
35         if(state == NULL)
36         {
37                 return(NULL);
38         }
39         
40         if(params == NULL)
41         {
42                 free(state);
43                 return(NULL);
44         }
45         
46         p_str = params;
47         gain = strtod(p_str,&p_str);
48         
49         /*
50          * all functions take a series and time_stamps
51          */
52         state->series = series;
53         state->time_stamps = time_stamps;
54         state->last_pred = 0.0;
55         state->gain = gain;
56         
57         return((char *)state);
58 }
59
60 void
61 FreeExpSmooth(char *state)
62 {
63         free(state);
64         return;
65 }
66
67 void
68 UpdateExpSmooth(char *state,
69                 double ts,
70                 double value)
71 {
72         double pred;
73         struct exp_smooth_state *s = (struct exp_smooth_state *)state;
74
75         pred = s->last_pred + s->gain * (value - s->last_pred);
76         
77         /*
78          * if there is only one value, last pred is the first valeu in the
79          * series
80          */
81         if(F_COUNT(s->series) <= 1)
82         {
83                 s->last_pred = F_VAL(s->series,F_FIRST(s->series));
84         }
85         else
86         {
87                 s->last_pred = pred;
88         }
89
90         return;
91 }
92
93 int
94 ForcExpSmooth(char *state, double *v)
95 {
96         struct exp_smooth_state *s = (struct exp_smooth_state *)state;
97         
98         *v = s->last_pred;
99
100         return(1);
101
102 }
103
104