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: 1505 $ $LastChangedBy: jdetaeye $ 00004 date : $LastChangedDate: 2011-08-26 18:55:08 +0200 (Fri, 26 Aug 2011) $ 00005 ***************************************************************************/ 00006 00007 /*************************************************************************** 00008 * * 00009 * Copyright (C) 2007-2011 by Johan De Taeye, frePPLe bvba * 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 Environment::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 (Environment::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 // Register the Python extensions 00153 PyGILState_STATE state = PyGILState_Ensure(); 00154 try 00155 { 00156 // Register new Python data types 00157 if (Forecast::initialize()) 00158 throw RuntimeException("Error registering forecast"); 00159 if (ForecastBucket::initialize()) 00160 throw RuntimeException("Error registering forecastbucket"); 00161 if (ForecastSolver::initialize()) 00162 throw RuntimeException("Error registering forecastsolver"); 00163 PyGILState_Release(state); 00164 } 00165 catch (exception &e) 00166 { 00167 PyGILState_Release(state); 00168 logger << "Error: " << e.what() << endl; 00169 } 00170 catch (...) 00171 { 00172 PyGILState_Release(state); 00173 logger << "Error: unknown exception" << endl; 00174 } 00175 00176 // Return the name of the module 00177 return name; 00178 } 00179 00180 } // end namespace