21 #define GRT_DLL_EXPORTS
31 this->useScaling = useScaling;
32 classType =
"MultidimensionalRegression";
33 regressifierType = classType;
34 debugLog.setProceedingText(
"[DEBUG MultidimensionalRegression]");
35 errorLog.setProceedingText(
"[ERROR MultidimensionalRegression]");
36 trainingLog.setProceedingText(
"[TRAINING MultidimensionalRegression]");
37 warningLog.setProceedingText(
"[WARNING MultidimensionalRegression]");
58 if( !rhs.deepCopyRegressionModules( regressionModules ) ){
59 errorLog <<
"const MultidimensionalRegression &rhs - Failed to deep copy regression modules!" << std::endl;
70 if( regressifier == NULL )
return false;
80 if( !ptr->deepCopyRegressionModules( regressionModules ) ){
81 errorLog <<
"deepCopyFrom(const Regressifier *regressifier) - Failed to deep copy regression modules!" << std::endl;
98 trainingResults.clear();
99 deleteRegressionModules();
102 errorLog <<
"train_(RegressionData &trainingData) - The regression module has not been set!" << std::endl;
107 errorLog <<
"train_(RegressionData &trainingData) - Training data has zero samples!" << std::endl;
111 numInputDimensions = N;
112 numOutputDimensions = K;
113 inputVectorRanges.clear();
114 targetVectorRanges.clear();
125 trainingData.
scale(inputVectorRanges,targetVectorRanges,0.0,1.0);
129 regressionModules.
resize( K, NULL );
134 for(UINT k=0; k<K; k++){
135 regressionModules[k] = regressifier->
deepCopy();
136 if( regressionModules[k] == NULL ){
137 errorLog <<
"train(LabelledRegressionData &trainingData) - Failed to deep copy module " << k << std::endl;
143 for(UINT k=0; k<K; k++){
145 trainingLog <<
"Training regression module: " << k << std::endl;
151 for(UINT i=0; i<M; i++){
152 if( !data.
addSample(trainingData[i].getInputVector(),
VectorFloat(1,trainingData[i].getTargetVector()[k]) ) ){
153 errorLog <<
"train_(RegressionData &trainingData) - Failed to add sample to dataset for regression module " << k << std::endl;
158 if( !regressionModules[k]->
train( data ) ){
159 errorLog <<
"train_(RegressionData &trainingData) - Failed to train regression module " << k << std::endl;
165 regressionData.
resize(K,0);
173 errorLog <<
"predict_(VectorFloat &inputVector) - Model Not Trained!" << std::endl;
177 if( !trained )
return false;
179 if( inputVector.
getSize() != numInputDimensions ){
180 errorLog <<
"predict_(VectorFloat &inputVector) - The size of the input Vector (" << inputVector.
getSize() <<
") does not match the num features in the model (" << numInputDimensions << std::endl;
185 for(UINT n=0; n<numInputDimensions; n++){
186 inputVector[n] = grt_scale(inputVector[n], inputVectorRanges[n].minValue, inputVectorRanges[n].maxValue, 0.0, 1.0);
190 for(UINT n=0; n<numOutputDimensions; n++){
191 if( !regressionModules[ n ]->
predict( inputVector ) ){
192 errorLog <<
"predict_(VectorFloat &inputVector) - Failed to predict for regression module " << n << std::endl;
194 regressionData[ n ] = regressionModules[ n ]->getRegressionData()[0];
198 for(UINT n=0; n<numOutputDimensions; n++){
199 regressionData[n] = grt_scale(regressionData[n], 0.0, 1.0, targetVectorRanges[n].minValue, targetVectorRanges[n].maxValue);
210 errorLog <<
"save(fstream &file) - The file is not open!" << std::endl;
215 file <<
"GRT_MULTIDIMENSIONAL_REGRESSION_MODEL_FILE_V2.0\n";
219 errorLog <<
"save(fstream &file) - Failed to save Regressifier base settings to file!" << std::endl;
224 file <<
"Regressifier: " <<
"NOT_SET" << std::endl;
231 if( !regressifier->
save( file ) ){
232 errorLog <<
"save(fstream &file) - Failed to save regressifier!" << std::endl;
236 for(UINT i=0; i<regressionModules.size(); i++){
237 if( !regressionModules[i]->
save( file ) ){
238 errorLog <<
"save(fstream &file) - Failed to save regression module " << i << std::endl;
249 numInputDimensions = 0;
254 errorLog <<
"load(string filename) - Could not open file to load model" << std::endl;
264 if( word ==
"GRT_MULTIDIMENSIONAL_REGRESSION_MODEL_FILE_V1.0" ){
268 if( word !=
"GRT_MULTIDIMENSIONAL_REGRESSION_MODEL_FILE_V2.0" ){
269 errorLog <<
"load( fstream &file ) - Could not find Model File Header" << std::endl;
275 errorLog <<
"load( fstream &file ) - Failed to save Regressifier base settings to file!" << std::endl;
280 if( word !=
"Regressifier:" ){
281 errorLog <<
"load(string filename) - Failed to find Regressifier!" << std::endl;
286 std::string regressifierType;
287 file >> regressifierType;
288 if( regressifierType ==
"NOT_SET" ){
295 if( regressifier == NULL ){
296 errorLog <<
"load(fstream &file) - Failed to create regression instance from string!" << std::endl;
300 if( !regressifier->
load( file ) ){
301 errorLog <<
"load(fstream &file) - Failed to load regressifier!" << std::endl;
305 if( numOutputDimensions > 0 ){
307 regressionModules.
resize(numOutputDimensions, NULL);
309 for(UINT i=0; i<regressionModules.
getSize(); i++){
311 if( !regressionModules[i]->
load( file ) ){
312 errorLog <<
"load(fstream &file) - Failed to load regression module " << i << std::endl;
322 return regressifier != NULL ?
true :
false;
331 if( !deleteRegressionModules() ){
337 if( this->regressifier != NULL )
delete this->regressifier;
339 this->regressifier = regressifier.
deepCopy();
341 if( this->regressifier == NULL )
return false;
348 const UINT N = regressionModules.
getSize();
351 if( newModules.size() > 0 )
return false;
354 if( N == 0 )
return true;
359 for(UINT i=0; i<N; i++){
361 newModules[i] = regressionModules[i]->deepCopy();
362 if( newModules[i] == NULL ){
363 for(UINT j=0; j<i; j++){
364 delete newModules[j];
365 newModules[j] = NULL;
375 bool MultidimensionalRegression::deleteAll(){
376 if( regressifier != NULL ){
380 return deleteRegressionModules();
383 bool MultidimensionalRegression::deleteRegressionModules(){
385 const UINT N = regressionModules.
getSize();
387 if( N == 0 )
return true;
389 for(UINT i=0; i<N; i++){
390 delete regressionModules[i];
391 regressionModules[i] = NULL;
393 regressionModules.clear();
402 if(word !=
"NumFeatures:"){
403 errorLog <<
"load(string filename) - Could not find NumFeatures!" << std::endl;
406 file >> numInputDimensions;
409 if(word !=
"NumOutputDimensions:"){
410 errorLog <<
"load(string filename) - Could not find NumOutputDimensions!" << std::endl;
413 file >> numOutputDimensions;
416 if(word !=
"UseScaling:"){
417 errorLog <<
"load(string filename) - Could not find UseScaling!" << std::endl;
425 inputVectorRanges.
resize(numInputDimensions);
426 targetVectorRanges.
resize(numOutputDimensions);
430 if(word !=
"InputVectorRanges:"){
432 errorLog <<
"load(string filename) - Failed to find InputVectorRanges!" << std::endl;
435 for(UINT j=0; j<inputVectorRanges.
getSize(); j++){
436 file >> inputVectorRanges[j].minValue;
437 file >> inputVectorRanges[j].maxValue;
441 if(word !=
"OutputVectorRanges:"){
443 errorLog <<
"load(string filename) - Failed to find OutputVectorRanges!" << std::endl;
446 for(UINT j=0; j<targetVectorRanges.
getSize(); j++){
447 file >> targetVectorRanges[j].minValue;
448 file >> targetVectorRanges[j].maxValue;
453 if( word !=
"Regressifier:" ){
454 errorLog <<
"load(string filename) - Failed to find Regressifier!" << std::endl;
459 std::string regressifierType;
460 file >> regressifierType;
461 if( regressifierType ==
"NOT_SET" ){
468 if( regressifier == NULL ){
469 errorLog <<
"load(fstream &file) - Failed to create regression instance from string!" << std::endl;
473 if( !regressifier->
load( file ) ){
474 errorLog <<
"load(fstream &file) - Failed to load regressifier!" << std::endl;
478 if( numOutputDimensions > 0 ){
480 regressionModules.
resize(numOutputDimensions, NULL);
482 for(UINT i=0; i<regressionModules.
getSize(); i++){
484 if( !regressionModules[i]->
load( file ) ){
485 errorLog <<
"load(fstream &file) - Failed to load regression module " << i << std::endl;
491 regressionData.
resize(numOutputDimensions,0);
virtual bool predict(VectorFloat inputVector)
Vector< MinMax > getInputRanges() const
bool enableScaling(const bool useScaling)
virtual bool resize(const unsigned int size)
virtual bool train(ClassificationData trainingData)
virtual bool predict_(VectorFloat &inputVector)
virtual bool train_(RegressionData &trainingData)
static Regressifier * createInstanceFromString(const std::string ®ressifierType)
MultidimensionalRegression(const Regressifier ®ressifier=LinearRegression(), bool useScaling=false)
bool copyBaseVariables(const Regressifier *regressifier)
UINT getNumInputDimensions() const
bool loadLegacyModelFromFile(std::fstream &file)
MultidimensionalRegression & operator=(const MultidimensionalRegression &rhs)
Regressifier * getRegressifier() const
virtual ~MultidimensionalRegression(void)
bool setInputAndTargetDimensions(const UINT numInputDimensions, const UINT numTargetDimensions)
virtual bool save(const std::string filename) const
Vector< MinMax > getTargetRanges() const
bool saveBaseSettingsToFile(std::fstream &file) const
virtual bool load(const std::string filename)
bool scale(const Float minTarget, const Float maxTarget)
UINT getNumTargetDimensions() const
std::string getRegressifierType() const
bool loadBaseSettingsFromFile(std::fstream &file)
Regressifier * deepCopy() const
virtual bool load(std::fstream &file)
bool setRegressionModule(const Regressifier ®ressifier)
This class implements the Multidimensional Regression meta algorithm. Multidimensional Regressionacts...
bool getIsRegressionModuleSet() const
virtual bool save(std::fstream &file) const
virtual bool deepCopyFrom(const Regressifier *regressifier)
bool addSample(const VectorFloat &inputVector, const VectorFloat &targetVector)
UINT getNumSamples() const