forecast/module.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002   file : $URL: https://frepple.svn.sourceforge.net/svnroot/frepple/trunk/modules/forecast/module.cpp $
00003   version : $LastChangedRevision: 1315 $  $LastChangedBy: jdetaeye $
00004   date : $LastChangedDate: 2010-07-17 18:08:53 +0200 (Sat, 17 Jul 2010) $
00005  ***************************************************************************/
00006 
00007 /***************************************************************************
00008  *                                                                         *
00009  * Copyright (C) 2007-2010 by Johan De Taeye                               *
00010  *                                                                         *
00011  * This library is free software; you can redistribute it and/or modify it *
00012  * under the terms of the GNU Lesser General Public License as published   *
00013  * by the Free Software Foundation; either version 2.1 of the License, or  *
00014  * (at your option) any later version.                                     *
00015  *                                                                         *
00016  * This library is distributed in the hope that it will be useful,         *
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of          *
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
00019  * General Public License for more details.                                *
00020  *                                                                         *
00021  * You should have received a copy of the GNU Lesser General Public        *
00022  * License along with this library; if not, write to the Free Software     *
00023  * Foundation Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 *
00024  * USA                                                                     *
00025  *                                                                         *
00026  ***************************************************************************/
00027 
00028 #include "forecast.h"
00029 
00030 namespace module_forecast
00031 {
00032 
00033 Forecast::MapOfForecasts Forecast::ForecastDictionary;
00034 bool Forecast::Customer_Then_Item_Hierarchy = true;
00035 bool Forecast::Match_Using_Delivery_Operation = true;
00036 TimePeriod Forecast::Net_Late(0L);
00037 TimePeriod Forecast::Net_Early(0L);
00038 unsigned long Forecast::Forecast_Iterations(15L);
00039 double Forecast::Forecast_SmapeAlfa(0.95);
00040 unsigned long Forecast::Forecast_Skip(5);
00041 
00042 
00043 MODULE_EXPORT const char* initialize(const CommandLoadLibrary::ParameterList& z)
00044 {
00045   // Initialize only once
00046   static bool init = false;
00047   static const char* name = "forecast";
00048   if (init)
00049   {
00050     logger << "Warning: Initializing module forecast more than once." << endl;
00051     return name;
00052   }
00053   init = true;
00054 
00055   // Process the module parameters
00056   for (CommandLoadLibrary::ParameterList::const_iterator x = z.begin();
00057     x != z.end(); ++x)
00058   try
00059   {
00060     // Forecast buckets
00061     if (x->first == "DueAtEndOfBucket")
00062       ForecastBucket::setDueAtEndOfBucket(x->second.getBool());
00063     // Netting
00064     else if (x->first == "Net_CustomerThenItemHierarchy")
00065       Forecast::setCustomerThenItemHierarchy(x->second.getBool());
00066     else if (x->first == "Net_MatchUsingDeliveryOperation")
00067       Forecast::setMatchUsingDeliveryOperation(x->second.getBool());
00068     else if (x->first == "Net_NetEarly")
00069       Forecast::setNetEarly(x->second.getTimeperiod());
00070     else if (x->first == "Net_NetLate")
00071       Forecast::setNetLate(x->second.getTimeperiod());
00072     // Forecasting
00073     else if (x->first == "Forecast_Iterations")
00074       Forecast::setForecastIterations(x->second.getUnsignedLong());
00075     else if (x->first == "Forecast_SmapeAlfa")
00076       Forecast::setForecastSmapeAlfa(x->second.getDouble());
00077     else if (x->first == "Forecast_Skip")
00078       Forecast::setForecastSkip(x->second.getUnsignedLong());
00079     // Moving average forecast method
00080     else if (x->first == "MovingAverage_buckets")
00081       Forecast::MovingAverage::setDefaultBuckets(x->second.getUnsignedLong());
00082     // Single exponential forecast method
00083     else if (x->first == "Forecast_SingleExponential_initialAlfa")
00084       Forecast::SingleExponential::setInitialAlfa(x->second.getDouble());
00085     else if (x->first == "Forecast_SingleExponential_minAlfa")
00086       Forecast::SingleExponential::setMinAlfa(x->second.getDouble());
00087     else if (x->first == "Forecast_SingleExponential_maxAlfa")
00088       Forecast::SingleExponential::setMaxAlfa(x->second.getDouble());
00089     // Double exponential forecast method
00090     else if (x->first == "Forecast_DoubleExponential_initialAlfa")
00091       Forecast::DoubleExponential::setInitialAlfa(x->second.getDouble());
00092     else if (x->first == "Forecast_DoubleExponential_minAlfa")
00093       Forecast::DoubleExponential::setMinAlfa(x->second.getDouble());
00094     else if (x->first == "Forecast_DoubleExponential_maxAlfa")
00095       Forecast::DoubleExponential::setMaxAlfa(x->second.getDouble());
00096     else if (x->first == "Forecast_DoubleExponential_initialGamma")
00097       Forecast::DoubleExponential::setInitialGamma(x->second.getDouble());
00098     else if (x->first == "Forecast_DoubleExponential_minGamma")
00099       Forecast::DoubleExponential::setMinGamma(x->second.getDouble());
00100     else if (x->first == "Forecast_DoubleExponential_maxGamma")
00101       Forecast::DoubleExponential::setMaxGamma(x->second.getDouble());
00102     else if (x->first == "Forecast_DoubleExponential_dampenTrend")
00103       Forecast::DoubleExponential::setDampenTrend(x->second.getDouble());
00104     // Seasonal forecast method
00105     else if (x->first == "Forecast_Seasonal_initialAlfa")
00106       Forecast::Seasonal::setInitialAlfa(x->second.getDouble());
00107     else if (x->first == "Forecast_Seasonal_minAlfa")
00108       Forecast::Seasonal::setMinAlfa(x->second.getDouble());
00109     else if (x->first == "Forecast_Seasonal_maxAlfa")
00110       Forecast::Seasonal::setMaxAlfa(x->second.getDouble());
00111     else if (x->first == "Forecast_Seasonal_initialBeta")
00112       Forecast::Seasonal::setInitialGamma(x->second.getDouble());
00113     else if (x->first == "Forecast_Seasonal_minBeta")
00114       Forecast::Seasonal::setMinBeta(x->second.getDouble());
00115     else if (x->first == "Forecast_Seasonal_maxBeta")
00116       Forecast::Seasonal::setMaxBeta(x->second.getDouble());    
00117     else if (x->first == "Forecast_Seasonal_initialGamma")
00118       Forecast::Seasonal::setInitialBeta(x->second.getDouble());
00119     else if (x->first == "Forecast_Seasonal_minGamma")
00120       Forecast::Seasonal::setMinGamma(x->second.getDouble());
00121     else if (x->first == "Forecast_Seasonal_maxGamma")
00122       Forecast::Seasonal::setMaxGamma(x->second.getDouble());
00123     else if (x->first == "Forecast_Seasonal_dampenTrend")
00124       Forecast::Seasonal::setDampenTrend(x->second.getDouble());
00125     else if (x->first == "Forecast_Seasonal_minPeriod")
00126       Forecast::Seasonal::setMinPeriod(x->second.getInt());
00127     else if (x->first == "Forecast_Seasonal_maxPeriod")
00128       Forecast::Seasonal::setMaxPeriod(x->second.getInt());
00129     // Croston forecast method
00130     else if (x->first == "Forecast_Croston_initialAlfa")
00131       Forecast::Croston::setInitialAlfa(x->second.getDouble());
00132     else if (x->first == "Forecast_Croston_minAlfa")
00133       Forecast::Croston::setMinAlfa(x->second.getDouble());
00134     else if (x->first == "Forecast_Croston_maxAlfa")
00135       Forecast::Croston::setMaxAlfa(x->second.getDouble());
00136     else if (x->first == "Forecast_Croston_minIntermittence")
00137       Forecast::Croston::setMinIntermittence(x->second.getDouble());
00138     // That's bullshit
00139     else
00140       logger << "Warning: Unrecognized parameter '" << x->first << "'" << endl;
00141   }
00142   catch (exception &e)
00143   {
00144     // Avoid throwing errors during the initialization!
00145     logger << "Error: " << e.what() << endl;
00146   }
00147   catch (...)
00148   {
00149     logger << "Error: unknown exception" << endl;
00150   }
00151 
00152   try
00153   {
00154     // Register the Python extensions
00155     PyThreadState *myThreadState = PyGILState_GetThisThreadState();
00156     if (!Py_IsInitialized() || !myThreadState)
00157       throw RuntimeException("Python isn't initialized correctly");
00158     try
00159     {
00160       // Get the global lock.
00161       PyEval_RestoreThread(myThreadState);
00162       // Register new Python data types
00163       if (Forecast::initialize())
00164         throw RuntimeException("Error registering forecast");
00165       if (ForecastBucket::initialize())
00166         throw RuntimeException("Error registering forecastbucket");
00167       if (ForecastSolver::initialize())
00168         throw RuntimeException("Error registering forecastsolver");
00169     }
00170     // Release the global lock when leaving the function
00171     catch (...)
00172     {
00173       PyEval_ReleaseLock();
00174       throw;  // Rethrow the exception
00175     }
00176     PyEval_ReleaseLock();
00177   }
00178   catch (exception &e)
00179   {
00180     // Avoid throwing errors during the initialization!
00181     logger << "Error: " << e.what() << endl;
00182   }
00183   catch (...)
00184   {
00185     logger << "Error: unknown exception" << endl;
00186   }
00187 
00188   // Return the name of the module
00189   return name;
00190 }
00191 
00192 }       // end namespace

Documentation generated for frePPLe by  doxygen