21 #define GRT_DLL_EXPORTS 27 const std::string MultidimensionalRegression::id =
"MultidimensionalRegression";
35 this->useScaling = useScaling;
60 if( !rhs.deepCopyRegressionModules( regressionModules ) ){
61 errorLog <<
"const MultidimensionalRegression &rhs - Failed to deep copy regression modules!" << std::endl;
72 if( regressifier == NULL )
return false;
82 if( !ptr->deepCopyRegressionModules( regressionModules ) ){
83 errorLog <<
"deepCopyFrom(const Regressifier *regressifier) - Failed to deep copy regression modules!" << std::endl;
100 trainingResults.clear();
101 deleteRegressionModules();
104 errorLog <<
"train_(RegressionData &trainingData) - The regression module has not been set!" << std::endl;
109 errorLog <<
"train_(RegressionData &trainingData) - Training data has zero samples!" << std::endl;
113 numInputDimensions = N;
114 numOutputDimensions = K;
115 inputVectorRanges.clear();
116 targetVectorRanges.clear();
127 trainingData.
scale(inputVectorRanges,targetVectorRanges,0.0,1.0);
131 regressionModules.
resize( K, NULL );
136 for(UINT k=0; k<K; k++){
137 regressionModules[k] = regressifier->
deepCopy();
138 if( regressionModules[k] == NULL ){
139 errorLog <<
"train(LabelledRegressionData &trainingData) - Failed to deep copy module " << k << std::endl;
145 for(UINT k=0; k<K; k++){
147 trainingLog <<
"Training regression module: " << k << std::endl;
153 for(UINT i=0; i<M; i++){
154 if( !data.
addSample(trainingData[i].getInputVector(),
VectorFloat(1,trainingData[i].getTargetVector()[k]) ) ){
155 errorLog <<
"train_(RegressionData &trainingData) - Failed to add sample to dataset for regression module " << k << std::endl;
160 if( !regressionModules[k]->
train( data ) ){
161 errorLog <<
"train_(RegressionData &trainingData) - Failed to train regression module " << k << std::endl;
167 regressionData.
resize(K,0);
175 errorLog <<
"predict_(VectorFloat &inputVector) - Model Not Trained!" << std::endl;
179 if( !trained )
return false;
181 if( inputVector.
getSize() != numInputDimensions ){
182 errorLog <<
"predict_(VectorFloat &inputVector) - The size of the input Vector (" << inputVector.
getSize() <<
") does not match the num features in the model (" << numInputDimensions << std::endl;
187 for(UINT n=0; n<numInputDimensions; n++){
188 inputVector[n] = grt_scale(inputVector[n], inputVectorRanges[n].minValue, inputVectorRanges[n].maxValue, 0.0, 1.0);
192 for(UINT n=0; n<numOutputDimensions; n++){
193 if( !regressionModules[ n ]->
predict( inputVector ) ){
194 errorLog <<
"predict_(VectorFloat &inputVector) - Failed to predict for regression module " << n << std::endl;
196 regressionData[ n ] = regressionModules[ n ]->getRegressionData()[0];
200 for(UINT n=0; n<numOutputDimensions; n++){
201 regressionData[n] = grt_scale(regressionData[n], 0.0, 1.0, targetVectorRanges[n].minValue, targetVectorRanges[n].maxValue);
212 errorLog <<
"save(fstream &file) - The file is not open!" << std::endl;
217 file <<
"GRT_MULTIDIMENSIONAL_REGRESSION_MODEL_FILE_V2.0\n";
221 errorLog <<
"save(fstream &file) - Failed to save Regressifier base settings to file!" << std::endl;
226 file <<
"Regressifier: " <<
"NOT_SET" << std::endl;
231 file <<
"Regressifier: " << regressifier->
getId() << std::endl;
233 if( !regressifier->
save( file ) ){
234 errorLog <<
"save(fstream &file) - Failed to save regressifier!" << std::endl;
238 for(UINT i=0; i<regressionModules.size(); i++){
239 if( !regressionModules[i]->
save( file ) ){
240 errorLog <<
"save(fstream &file) - Failed to save regression module " << i << std::endl;
251 numInputDimensions = 0;
256 errorLog <<
"load(string filename) - Could not open file to load model" << std::endl;
266 if( word ==
"GRT_MULTIDIMENSIONAL_REGRESSION_MODEL_FILE_V1.0" ){
270 if( word !=
"GRT_MULTIDIMENSIONAL_REGRESSION_MODEL_FILE_V2.0" ){
271 errorLog <<
"load( fstream &file ) - Could not find Model File Header" << std::endl;
277 errorLog <<
"load( fstream &file ) - Failed to save Regressifier base settings to file!" << std::endl;
282 if( word !=
"Regressifier:" ){
283 errorLog <<
"load(string filename) - Failed to find Regressifier!" << std::endl;
288 std::string regressifierType;
289 file >> regressifierType;
290 if( regressifierType ==
"NOT_SET" ){
295 regressifier =
create( regressifierType );
297 if( regressifier == NULL ){
298 errorLog <<
"load(fstream &file) - Failed to create regression instance from string!" << std::endl;
302 if( !regressifier->
load( file ) ){
303 errorLog <<
"load(fstream &file) - Failed to load regressifier!" << std::endl;
307 if( numOutputDimensions > 0 ){
309 regressionModules.
resize(numOutputDimensions, NULL);
311 for(UINT i=0; i<regressionModules.
getSize(); i++){
312 regressionModules[i] =
create( regressifierType );
313 if( !regressionModules[i]->
load( file ) ){
314 errorLog <<
"load(fstream &file) - Failed to load regression module " << i << std::endl;
324 return regressifier != NULL ?
true :
false;
333 if( !deleteRegressionModules() ){
339 if( this->regressifier != NULL )
delete this->regressifier;
341 this->regressifier = regressifier.
deepCopy();
343 if( this->regressifier == NULL )
return false;
350 const UINT N = regressionModules.
getSize();
353 if( newModules.size() > 0 )
return false;
356 if( N == 0 )
return true;
361 for(UINT i=0; i<N; i++){
363 newModules[i] = regressionModules[i]->deepCopy();
364 if( newModules[i] == NULL ){
365 for(UINT j=0; j<i; j++){
366 delete newModules[j];
367 newModules[j] = NULL;
377 bool MultidimensionalRegression::deleteAll(){
378 if( regressifier != NULL ){
382 return deleteRegressionModules();
385 bool MultidimensionalRegression::deleteRegressionModules(){
387 const UINT N = regressionModules.
getSize();
389 if( N == 0 )
return true;
391 for(UINT i=0; i<N; i++){
392 delete regressionModules[i];
393 regressionModules[i] = NULL;
395 regressionModules.clear();
404 if(word !=
"NumFeatures:"){
405 errorLog <<
"load(string filename) - Could not find NumFeatures!" << std::endl;
408 file >> numInputDimensions;
411 if(word !=
"NumOutputDimensions:"){
412 errorLog <<
"load(string filename) - Could not find NumOutputDimensions!" << std::endl;
415 file >> numOutputDimensions;
418 if(word !=
"UseScaling:"){
419 errorLog <<
"load(string filename) - Could not find UseScaling!" << std::endl;
427 inputVectorRanges.
resize(numInputDimensions);
428 targetVectorRanges.
resize(numOutputDimensions);
432 if(word !=
"InputVectorRanges:"){
434 errorLog <<
"load(string filename) - Failed to find InputVectorRanges!" << std::endl;
437 for(UINT j=0; j<inputVectorRanges.
getSize(); j++){
438 file >> inputVectorRanges[j].minValue;
439 file >> inputVectorRanges[j].maxValue;
443 if(word !=
"OutputVectorRanges:"){
445 errorLog <<
"load(string filename) - Failed to find OutputVectorRanges!" << std::endl;
448 for(UINT j=0; j<targetVectorRanges.
getSize(); j++){
449 file >> targetVectorRanges[j].minValue;
450 file >> targetVectorRanges[j].maxValue;
455 if( word !=
"Regressifier:" ){
456 errorLog <<
"load(string filename) - Failed to find Regressifier!" << std::endl;
461 std::string regressifierType;
462 file >> regressifierType;
463 if( regressifierType ==
"NOT_SET" ){
468 regressifier =
create( regressifierType );
470 if( regressifier == NULL ){
471 errorLog <<
"load(fstream &file) - Failed to create regression instance from string!" << std::endl;
475 if( !regressifier->
load( file ) ){
476 errorLog <<
"load(fstream &file) - Failed to load regressifier!" << std::endl;
480 if( numOutputDimensions > 0 ){
482 regressionModules.
resize(numOutputDimensions, NULL);
484 for(UINT i=0; i<regressionModules.
getSize(); i++){
485 regressionModules[i] =
create( regressifierType );
486 if( !regressionModules[i]->
load( file ) ){
487 errorLog <<
"load(fstream &file) - Failed to load regression module " << i << std::endl;
493 regressionData.
resize(numOutputDimensions,0);
std::string getId() const
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)
Regressifier * create() const
virtual bool predict_(VectorFloat &inputVector)
virtual bool train_(RegressionData &trainingData)
MultidimensionalRegression(const Regressifier ®ressifier=LinearRegression(), bool useScaling=false)
bool copyBaseVariables(const Regressifier *regressifier)
UINT getNumInputDimensions() const
virtual bool save(const std::string &filename) 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)
Vector< MinMax > getTargetRanges() const
bool saveBaseSettingsToFile(std::fstream &file) const
bool scale(const Float minTarget, const Float maxTarget)
UINT getNumTargetDimensions() const
bool loadBaseSettingsFromFile(std::fstream &file)
Regressifier * deepCopy() const
virtual bool load(std::fstream &file)
bool setRegressionModule(const Regressifier ®ressifier)
bool getIsRegressionModuleSet() const
virtual bool load(const std::string &filename)
virtual bool save(std::fstream &file) const
static std::string getId()
virtual bool deepCopyFrom(const Regressifier *regressifier)
bool addSample(const VectorFloat &inputVector, const VectorFloat &targetVector)
UINT getNumSamples() const