26 RegisterRegressifierModule< MultidimensionalRegression > MultidimensionalRegression::registerModule(
"MultidimensionalRegression");
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!" << endl;
69 if( regressifier == NULL )
return false;
79 if( !ptr->deepCopyRegressionModules( regressionModules ) ){
80 errorLog <<
"deepCopyFrom(const Regressifier *regressifier) - Failed to deep copy regression modules!" << endl;
97 trainingResults.clear();
98 deleteRegressionModules();
101 errorLog <<
"train_(RegressionData &trainingData) - The regression module has not been set!" << endl;
106 errorLog <<
"train_(RegressionData &trainingData) - Training data has zero samples!" << 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 << endl;
142 for(UINT k=0; k<K; k++){
144 trainingLog <<
"Training regression module: " << k << endl;
150 for(UINT i=0; i<M; i++){
151 if( !data.
addSample(trainingData[i].getInputVector(), VectorDouble(1,trainingData[i].getTargetVector()[k]) ) ){
152 errorLog <<
"train_(RegressionData &trainingData) - Failed to add sample to dataset for regression module " << k << endl;
157 if( !regressionModules[k]->
train( data ) ){
158 errorLog <<
"train_(RegressionData &trainingData) - Failed to train regression module " << k << endl;
164 regressionData.resize(K,0);
172 errorLog <<
"predict_(VectorDouble &inputVector) - Model Not Trained!" << endl;
176 if( !trained )
return false;
178 if( inputVector.size() != numInputDimensions ){
179 errorLog <<
"predict_(VectorDouble &inputVector) - The size of the input vector (" << int( inputVector.size() ) <<
") does not match the num features in the model (" << numInputDimensions << endl;
184 for(UINT n=0; n<numInputDimensions; n++){
185 inputVector[n] =
scale(inputVector[n], inputVectorRanges[n].minValue, inputVectorRanges[n].maxValue, 0, 1);
189 for(UINT n=0; n<numOutputDimensions; n++){
190 if( !regressionModules[ n ]->
predict( inputVector ) ){
191 errorLog <<
"predict_(VectorDouble &inputVector) - Failed to predict for regression module " << n << endl;
193 regressionData[ n ] = regressionModules[ n ]->getRegressionData()[0];
197 for(UINT n=0; n<numOutputDimensions; n++){
198 regressionData[n] =
scale(regressionData[n], 0, 1, targetVectorRanges[n].minValue, targetVectorRanges[n].maxValue);
209 errorLog <<
"saveModelToFile(fstream &file) - The file is not open!" << endl;
214 file <<
"GRT_MULTIDIMENSIONAL_REGRESSION_MODEL_FILE_V2.0\n";
218 errorLog <<
"saveModelToFile(fstream &file) - Failed to save Regressifier base settings to file!" << endl;
223 file <<
"Regressifier: " <<
"NOT_SET" << endl;
231 errorLog <<
"saveModelToFile(fstream &file) - Failed to save regressifier!" << endl;
235 for(UINT i=0; i<regressionModules.size(); i++){
237 errorLog <<
"saveModelToFile(fstream &file) - Failed to save regression module " << i << endl;
248 numInputDimensions = 0;
253 errorLog <<
"loadModelFromFile(string filename) - Could not open file to load model" << 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" << endl;
274 errorLog <<
"loadModelFromFile( fstream &file ) - Failed to save Regressifier base settings to file!" << endl;
279 if( word !=
"Regressifier:" ){
280 errorLog <<
"loadModelFromFile(string filename) - Failed to find Regressifier!" << endl;
285 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!" << endl;
300 errorLog <<
"loadModelFromFile(fstream &file) - Failed to load regressifier!" << endl;
304 if( numOutputDimensions > 0 ){
306 regressionModules.resize(numOutputDimensions, NULL);
308 for(UINT i=0; i<regressionModules.size(); i++){
311 errorLog <<
"loadModelFromFile(fstream &file) - Failed to load regression module " << i << 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;
345 bool MultidimensionalRegression::deepCopyRegressionModules( vector< Regressifier* > &newModules )
const{
347 const UINT N = (UINT)regressionModules.size();
350 if( newModules.size() > 0 )
return false;
353 if( N == 0 )
return true;
356 newModules.resize( N );
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 = (UINT)regressionModules.size();
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!" << endl;
405 file >> numInputDimensions;
408 if(word !=
"NumOutputDimensions:"){
409 errorLog <<
"loadModelFromFile(string filename) - Could not find NumOutputDimensions!" << endl;
412 file >> numOutputDimensions;
415 if(word !=
"UseScaling:"){
416 errorLog <<
"loadModelFromFile(string filename) - Could not find UseScaling!" << endl;
424 inputVectorRanges.resize(numInputDimensions);
425 targetVectorRanges.resize(numOutputDimensions);
429 if(word !=
"InputVectorRanges:"){
431 errorLog <<
"loadModelFromFile(string filename) - Failed to find InputVectorRanges!" << endl;
434 for(UINT j=0; j<inputVectorRanges.size(); j++){
435 file >> inputVectorRanges[j].minValue;
436 file >> inputVectorRanges[j].maxValue;
440 if(word !=
"OutputVectorRanges:"){
442 errorLog <<
"loadModelFromFile(string filename) - Failed to find OutputVectorRanges!" << endl;
445 for(UINT j=0; j<targetVectorRanges.size(); j++){
446 file >> targetVectorRanges[j].minValue;
447 file >> targetVectorRanges[j].maxValue;
452 if( word !=
"Regressifier:" ){
453 errorLog <<
"loadModelFromFile(string filename) - Failed to find Regressifier!" << endl;
458 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!" << endl;
473 errorLog <<
"loadModelFromFile(fstream &file) - Failed to load regressifier!" << endl;
477 if( numOutputDimensions > 0 ){
479 regressionModules.resize(numOutputDimensions, NULL);
481 for(UINT i=0; i<regressionModules.size(); i++){
484 errorLog <<
"loadModelFromFile(fstream &file) - Failed to load regression module " << i << endl;
490 regressionData.resize(numOutputDimensions,0);
virtual bool saveModelToFile(fstream &file) const
virtual bool saveModelToFile(string filename) const
static Regressifier * createInstanceFromString(string const ®ressifierType)
bool copyBaseVariables(const Regressifier *regressifier)
bool setRegressionModule(const Regressifier ®ressifier)
virtual bool loadModelFromFile(string filename)
bool setInputAndTargetDimensions(const UINT numInputDimensions, const UINT numTargetDimensions)
virtual bool train(ClassificationData trainingData)
bool loadBaseSettingsFromFile(fstream &file)
virtual bool loadModelFromFile(fstream &file)
vector< MinMax > getInputRanges() const
bool loadLegacyModelFromFile(fstream &file)
bool saveBaseSettingsToFile(fstream &file) const
bool enableScaling(bool useScaling)
Regressifier * getRegressifier() const
UINT getNumSamples() const
double scale(const double &x, const double &minSource, const double &maxSource, const double &minTarget, const double &maxTarget, const bool constrain=false)
Regressifier * deepCopy() const
virtual bool predict(VectorDouble inputVector)
string getRegressifierType() const
virtual ~MultidimensionalRegression(void)
vector< MinMax > getTargetRanges() const
virtual bool predict_(VectorDouble &inputVector)
bool getIsRegressionModuleSet() const
UINT getNumTargetDimensions() const
bool addSample(const VectorDouble &inputVector, const VectorDouble &targetVector)
virtual bool train_(RegressionData &trainingData)
This class implements the Multidimensional Regression meta algorithm. Multidimensional Regressionacts...
UINT getNumInputDimensions() const
virtual bool deepCopyFrom(const Regressifier *regressifier)
MultidimensionalRegression & operator=(const MultidimensionalRegression &rhs)
bool scale(const double minTarget, const double maxTarget)
MultidimensionalRegression(const Regressifier ®ressifier=LinearRegression(), bool useScaling=false)