forecast/module.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002   file : $URL: https://frepple.svn.sourceforge.net/svnroot/frepple/tags/0.8.0/modules/forecast/module.cpp $
00003   version : $LastChangedRevision: 1108 $  $LastChangedBy: jdetaeye $
00004   date : $LastChangedDate: 2009-12-06 18:54:18 +0100 (Sun, 06 Dec 2009) $
00005  ***************************************************************************/
00006 
00007 /***************************************************************************
00008  *                                                                         *
00009  * Copyright (C) 2007 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_MadAlfa(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     // Netting
00061     if (x->first == "Net_CustomerThenItemHierarchy")
00062       Forecast::setCustomerThenItemHierarchy(x->second.getBool());
00063     else if (x->first == "Net_MatchUsingDeliveryOperation")
00064       Forecast::setMatchUsingDeliveryOperation(x->second.getBool());
00065     else if (x->first == "Net_NetEarly")
00066       Forecast::setNetEarly(x->second.getTimeperiod());
00067     else if (x->first == "Net_NetLate")
00068       Forecast::setNetLate(x->second.getTimeperiod());
00069     // Forecasting
00070     else if (x->first == "Forecast_Iterations")
00071       Forecast::setForecastIterations(x->second.getUnsignedLong());
00072     else if (x->first == "Forecast_madAlfa")
00073       Forecast::setForecastMadAlfa(x->second.getDouble());
00074     else if (x->first == "Forecast_Skip")
00075       Forecast::setForecastSkip(x->second.getUnsignedLong());
00076     // Moving average forecast method
00077     else if (x->first == "MovingAverage_buckets")
00078       Forecast::MovingAverage::setDefaultBuckets(x->second.getUnsignedLong());
00079     // Single exponential forecast method
00080     else if (x->first == "Forecast_SingleExponential_initialAlfa")
00081       Forecast::SingleExponential::setInitialAlfa(x->second.getDouble());
00082     else if (x->first == "Forecast_SingleExponential_minAlfa")
00083       Forecast::SingleExponential::setMinAlfa(x->second.getDouble());
00084     else if (x->first == "Forecast_SingleExponential_maxAlfa")
00085       Forecast::SingleExponential::setMaxAlfa(x->second.getDouble());
00086     // Double exponential forecast method
00087     else if (x->first == "Forecast_DoubleExponential_initialAlfa")
00088       Forecast::DoubleExponential::setInitialAlfa(x->second.getDouble());
00089     else if (x->first == "Forecast_DoubleExponential_minAlfa")
00090       Forecast::DoubleExponential::setMinAlfa(x->second.getDouble());
00091     else if (x->first == "Forecast_DoubleExponential_maxAlfa")
00092       Forecast::DoubleExponential::setMaxAlfa(x->second.getDouble());
00093     else if (x->first == "Forecast_DoubleExponential_initialGamma")
00094       Forecast::DoubleExponential::setInitialGamma(x->second.getDouble());
00095     else if (x->first == "Forecast_DoubleExponential_minGamma")
00096       Forecast::DoubleExponential::setMinGamma(x->second.getDouble());
00097     else if (x->first == "Forecast_DoubleExponential_maxGamma")
00098       Forecast::DoubleExponential::setMaxGamma(x->second.getDouble());
00099     // Bullshit
00100     else
00101       logger << "Warning: Unrecognized parameter '" << x->first << "'" << endl;
00102   }
00103   catch (exception &e)
00104   {
00105     // Avoid throwing errors during the initialization!
00106     logger << "Error: " << e.what() << endl;
00107   }
00108   catch (...)
00109   {
00110     logger << "Error: unknown exception" << endl;
00111   }
00112 
00113   try
00114   {
00115     // Register the Python extensions
00116     PyThreadState *myThreadState = PyGILState_GetThisThreadState();
00117     if (!Py_IsInitialized() || !myThreadState)
00118       throw RuntimeException("Python isn't initialized correctly");
00119     try
00120     {
00121       // Get the global lock.
00122       PyEval_RestoreThread(myThreadState);
00123       // Register new Python data types
00124       if (Forecast::initialize())
00125         throw RuntimeException("Error registering forecast");
00126       if (ForecastBucket::initialize())
00127         throw RuntimeException("Error registering forecastbucket");
00128       if (ForecastSolver::initialize())
00129         throw RuntimeException("Error registering forecastsolver");
00130     }
00131     // Release the global lock when leaving the function
00132     catch (...)
00133     {
00134       PyEval_ReleaseLock();
00135       throw;  // Rethrow the exception
00136     }
00137     PyEval_ReleaseLock();
00138   }
00139   catch (exception &e)
00140   {
00141     // Avoid throwing errors during the initialization!
00142     logger << "Error: " << e.what() << endl;
00143   }
00144   catch (...)
00145   {
00146     logger << "Error: unknown exception" << endl;
00147   }
00148 
00149   // Return the name of the module
00150   return name;
00151 }
00152 
00153 }       // end namespace

Generated on 16 Apr 2010 for frePPLe by  doxygen 1.6.1