30 this->useScaling = useScaling;
31 classType =
"MultidimensionalRegression";
32 regressifierType = classType;
33 debugLog.setProceedingText(
"[DEBUG MultidimensionalRegression]");
34 errorLog.setProceedingText(
"[ERROR MultidimensionalRegression]");
35 trainingLog.setProceedingText(
"[TRAINING MultidimensionalRegression]");
36 warningLog.setProceedingText(
"[WARNING MultidimensionalRegression]");
57 if( !rhs.deepCopyRegressionModules( regressionModules ) ){
58 errorLog <<
"const MultidimensionalRegression &rhs - Failed to deep copy regression modules!" << std::endl;
69 if( regressifier == NULL )
return false;
79 if( !ptr->deepCopyRegressionModules( regressionModules ) ){
80 errorLog <<
"deepCopyFrom(const Regressifier *regressifier) - Failed to deep copy regression modules!" << std::endl;
97 trainingResults.clear();
98 deleteRegressionModules();
101 errorLog <<
"train_(RegressionData &trainingData) - The regression module has not been set!" << std::endl;
106 errorLog <<
"train_(RegressionData &trainingData) - Training data has zero samples!" << std::endl;
110 numInputDimensions = N;
111 numOutputDimensions = K;
112 inputVectorRanges.clear();
113 targetVectorRanges.clear();
124 trainingData.
scale(inputVectorRanges,targetVectorRanges,0.0,1.0);
128 regressionModules.
resize( K, NULL );
133 for(UINT k=0; k<K; k++){
134 regressionModules[k] = regressifier->
deepCopy();
135 if( regressionModules[k] == NULL ){
136 errorLog <<
"train(LabelledRegressionData &trainingData) - Failed to deep copy module " << k << std::endl;
142 for(UINT k=0; k<K; k++){
144 trainingLog <<
"Training regression module: " << k << std::endl;
150 for(UINT i=0; i<M; i++){
151 if( !data.
addSample(trainingData[i].getInputVector(),
VectorFloat(1,trainingData[i].getTargetVector()[k]) ) ){
152 errorLog <<
"train_(RegressionData &trainingData) - Failed to add sample to dataset for regression module " << k << std::endl;
157 if( !regressionModules[k]->
train( data ) ){
158 errorLog <<
"train_(RegressionData &trainingData) - Failed to train regression module " << k << std::endl;
164 regressionData.
resize(K,0);
172 errorLog <<
"predict_(VectorFloat &inputVector) - Model Not Trained!" << std::endl;
176 if( !trained )
return false;
178 if( inputVector.
getSize() != numInputDimensions ){
179 errorLog <<
"predict_(VectorFloat &inputVector) - The size of the input Vector (" << inputVector.
getSize() <<
") does not match the num features in the model (" << numInputDimensions << std::endl;
184 for(UINT n=0; n<numInputDimensions; n++){
185 inputVector[n] = grt_scale(inputVector[n], inputVectorRanges[n].minValue, inputVectorRanges[n].maxValue, 0.0, 1.0);
189 for(UINT n=0; n<numOutputDimensions; n++){
190 if( !regressionModules[ n ]->
predict( inputVector ) ){
191 errorLog <<
"predict_(VectorFloat &inputVector) - Failed to predict for regression module " << n << std::endl;
193 regressionData[ n ] = regressionModules[ n ]->getRegressionData()[0];
197 for(UINT n=0; n<numOutputDimensions; n++){
198 regressionData[n] = grt_scale(regressionData[n], 0.0, 1.0, targetVectorRanges[n].minValue, targetVectorRanges[n].maxValue);
209 errorLog <<
"saveModelToFile(fstream &file) - The file is not open!" << std::endl;
214 file <<
"GRT_MULTIDIMENSIONAL_REGRESSION_MODEL_FILE_V2.0\n";
218 errorLog <<
"saveModelToFile(fstream &file) - Failed to save Regressifier base settings to file!" << std::endl;
223 file <<
"Regressifier: " <<
"NOT_SET" << std::endl;
231 errorLog <<
"saveModelToFile(fstream &file) - Failed to save regressifier!" << std::endl;
235 for(UINT i=0; i<regressionModules.size(); i++){
237 errorLog <<
"saveModelToFile(fstream &file) - Failed to save regression module " << i << std::endl;
248 numInputDimensions = 0;
253 errorLog <<
"loadModelFromFile(string filename) - Could not open file to load model" << std::endl;
263 if( word ==
"GRT_MULTIDIMENSIONAL_REGRESSION_MODEL_FILE_V1.0" ){
267 if( word !=
"GRT_MULTIDIMENSIONAL_REGRESSION_MODEL_FILE_V2.0" ){
268 errorLog <<
"loadModelFromFile( fstream &file ) - Could not find Model File Header" << std::endl;
274 errorLog <<
"loadModelFromFile( fstream &file ) - Failed to save Regressifier base settings to file!" << std::endl;
279 if( word !=
"Regressifier:" ){
280 errorLog <<
"loadModelFromFile(string filename) - Failed to find Regressifier!" << std::endl;
285 std::string regressifierType;
286 file >> regressifierType;
287 if( regressifierType ==
"NOT_SET" ){
294 if( regressifier == NULL ){
295 errorLog <<
"loadModelFromFile(fstream &file) - Failed to create regression instance from string!" << std::endl;
300 errorLog <<
"loadModelFromFile(fstream &file) - Failed to load regressifier!" << std::endl;
304 if( numOutputDimensions > 0 ){
306 regressionModules.
resize(numOutputDimensions, NULL);
308 for(UINT i=0; i<regressionModules.
getSize(); i++){
311 errorLog <<
"loadModelFromFile(fstream &file) - Failed to load regression module " << i << std::endl;
321 return regressifier != NULL ?
true :
false;
330 if( !deleteRegressionModules() ){
336 if( this->regressifier != NULL )
delete this->regressifier;
338 this->regressifier = regressifier.
deepCopy();
340 if( this->regressifier == NULL )
return false;
347 const UINT N = regressionModules.
getSize();
350 if( newModules.size() > 0 )
return false;
353 if( N == 0 )
return true;
358 for(UINT i=0; i<N; i++){
360 newModules[i] = regressionModules[i]->deepCopy();
361 if( newModules[i] == NULL ){
362 for(UINT j=0; j<i; j++){
363 delete newModules[j];
364 newModules[j] = NULL;
374 bool MultidimensionalRegression::deleteAll(){
375 if( regressifier != NULL ){
379 return deleteRegressionModules();
382 bool MultidimensionalRegression::deleteRegressionModules(){
384 const UINT N = regressionModules.
getSize();
386 if( N == 0 )
return true;
388 for(UINT i=0; i<N; i++){
389 delete regressionModules[i];
390 regressionModules[i] = NULL;
392 regressionModules.clear();
401 if(word !=
"NumFeatures:"){
402 errorLog <<
"loadModelFromFile(string filename) - Could not find NumFeatures!" << std::endl;
405 file >> numInputDimensions;
408 if(word !=
"NumOutputDimensions:"){
409 errorLog <<
"loadModelFromFile(string filename) - Could not find NumOutputDimensions!" << std::endl;
412 file >> numOutputDimensions;
415 if(word !=
"UseScaling:"){
416 errorLog <<
"loadModelFromFile(string filename) - Could not find UseScaling!" << std::endl;
424 inputVectorRanges.
resize(numInputDimensions);
425 targetVectorRanges.
resize(numOutputDimensions);
429 if(word !=
"InputVectorRanges:"){
431 errorLog <<
"loadModelFromFile(string filename) - Failed to find InputVectorRanges!" << std::endl;
434 for(UINT j=0; j<inputVectorRanges.
getSize(); j++){
435 file >> inputVectorRanges[j].minValue;
436 file >> inputVectorRanges[j].maxValue;
440 if(word !=
"OutputVectorRanges:"){
442 errorLog <<
"loadModelFromFile(string filename) - Failed to find OutputVectorRanges!" << std::endl;
445 for(UINT j=0; j<targetVectorRanges.
getSize(); j++){
446 file >> targetVectorRanges[j].minValue;
447 file >> targetVectorRanges[j].maxValue;
452 if( word !=
"Regressifier:" ){
453 errorLog <<
"loadModelFromFile(string filename) - Failed to find Regressifier!" << std::endl;
458 std::string regressifierType;
459 file >> regressifierType;
460 if( regressifierType ==
"NOT_SET" ){
467 if( regressifier == NULL ){
468 errorLog <<
"loadModelFromFile(fstream &file) - Failed to create regression instance from string!" << std::endl;
473 errorLog <<
"loadModelFromFile(fstream &file) - Failed to load regressifier!" << std::endl;
477 if( numOutputDimensions > 0 ){
479 regressionModules.
resize(numOutputDimensions, NULL);
481 for(UINT i=0; i<regressionModules.
getSize(); i++){
484 errorLog <<
"loadModelFromFile(fstream &file) - Failed to load regression module " << i << std::endl;
490 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)
unsigned int getSize() const
MultidimensionalRegression & operator=(const MultidimensionalRegression &rhs)
Regressifier * getRegressifier() const
virtual ~MultidimensionalRegression(void)
bool setInputAndTargetDimensions(const UINT numInputDimensions, const UINT numTargetDimensions)
Vector< MinMax > getTargetRanges() const
bool saveBaseSettingsToFile(std::fstream &file) const
bool scale(const Float minTarget, const Float maxTarget)
UINT getNumTargetDimensions() const
std::string getRegressifierType() const
virtual bool saveModelToFile(std::string filename) const
virtual bool loadModelFromFile(std::fstream &file)
virtual bool saveModelToFile(std::fstream &file) const
bool loadBaseSettingsFromFile(std::fstream &file)
virtual bool loadModelFromFile(std::string filename)
Regressifier * deepCopy() const
bool setRegressionModule(const Regressifier ®ressifier)
This class implements the Multidimensional Regression meta algorithm. Multidimensional Regressionacts...
bool getIsRegressionModuleSet() const
virtual bool deepCopyFrom(const Regressifier *regressifier)
bool addSample(const VectorFloat &inputVector, const VectorFloat &targetVector)
UINT getNumSamples() const