3 #include <stdlib.h> /* free() malloc() */
4 #include <string.h> /* strdup() */
5 #include "forc.h" /* forecaster functions */
6 #include "diagnostic.h" /* FAIL() */
7 #include "forecast_api.h"
10 struct FORECASTAPI_ForecastStateStruct {
11 char *Forecaster_state;
16 FORECASTAPI_FreeForecastState(FORECASTAPI_ForecastState **state) {
17 FreeForcl((*state)->Forecaster_state);
23 FORECASTAPI_MethodName(unsigned int methodIndex) {
26 static int methodCount = 0;
27 static char *methodNames[MAX_FORC];
30 if(methodCount == 0) {
31 stateForNames = InitForcl(MAX_FORC, MAX_DATA_ENTRIES);
32 GetForcNames(stateForNames, methodNames, MAX_FORC, &methodCount);
34 ** GetForNames() gives us pointers which will be freed by FreeForcl(), so
35 ** we have to strdup() them.
37 for(i = 0; i < methodCount; i++) {
38 methodNames[i] = strdup(methodNames[i]);
40 FreeForcl(stateForNames);
43 return((methodIndex < methodCount) ? methodNames[methodIndex] : NULL);
48 FORECASTAPI_ForecastState *
49 FORECASTAPI_NewForecastState(void) {
50 FORECASTAPI_ForecastState *returnValue;
52 * init the forecaster state
55 (FORECASTAPI_ForecastState *)malloc(sizeof(FORECASTAPI_ForecastState));
56 if(returnValue == NULL)
57 FAIL("NewForecastState: out of memory\n");
59 returnValue->Forecaster_state = InitForcl(MAX_FORC,MAX_DATA_ENTRIES);
60 if(returnValue->Forecaster_state == NULL) {
62 FAIL("NewForecastState: out of memory\n");
69 FORECASTAPI_UpdateForecastState(FORECASTAPI_ForecastState *state,
70 const FORECASTAPI_Measurement *measurements,
71 size_t howManyMeasurements,
72 FORECASTAPI_ForecastCollection *forecasts,
73 size_t howManyForecasts) {
81 /* check if we want forecast */
82 if (forecasts == NULL) {
86 for(i = howManyMeasurements - 1; i >= 0; i--) {
87 UpdateForecasts(state->Forecaster_state, (double)measurements[i].timeStamp, (double)measurements[i].measurement);
88 if(i < howManyForecasts) {
89 FORECASTAPI_ComputeForecast(state, &forecasts[i]);
90 forecasts[i].measurement = measurements[i];
96 FORECASTAPI_ComputeForecast( FORECASTAPI_ForecastState *state,
97 FORECASTAPI_ForecastCollection *forecast) {
100 if (state == NULL || forecast == NULL) {
104 forecast->forecasts[FORECASTAPI_MAE_FORECAST].forecast = MAEForecast(state->Forecaster_state);
105 forecast->forecasts[FORECASTAPI_MAE_FORECAST].error = MAEError(state->Forecaster_state);
106 forecast->forecasts[FORECASTAPI_MAE_FORECAST].methodUsed = MAEMethod(state->Forecaster_state);
107 forecast->forecasts[FORECASTAPI_MSE_FORECAST].forecast = MSEForecast(state->Forecaster_state);
108 forecast->forecasts[FORECASTAPI_MSE_FORECAST].error = MSEError(state->Forecaster_state);
109 forecast->forecasts[FORECASTAPI_MSE_FORECAST].methodUsed = MSEMethod(state->Forecaster_state);