Brief description of class still missing. More...
#include <ModelSet.h>
Public Member Functions | |
| int | add (const RealSpace ¶meterSpace, const double *misfit=0) |
| int | bestModel () const |
| void | clear () |
| int | count () const |
| const int * | firstModel () const |
| bool | importModels (RealSpace ¶meterSpace, QString fileName, bool strict=true) |
| bool | importModelsFromAscii (RealSpace ¶meterSpace, QString fileName) |
| bool | importModelsFromReport (RealSpace ¶meterSpace, ReportReader &report, bool strict=true) |
| void | lock () |
| const double * | misfit (int modelIndex) const |
| const int * | model (int modelIndex) const |
| ModelSet (int parameterCount, int targetCount, int defaultCapacity=16384) | |
| int | parameterCount () const |
| void | print (int modelIndex) const |
| void | reserve (int n) |
| void | setMisfit (int modelIndex, int targetIndex, double m) |
| int | targetCount () const |
| void | unlock () |
| ~ModelSet () | |
Brief description of class still missing.
Full description of class still missing
| DinverCore::ModelSet::ModelSet | ( | int | parameterCount, |
| int | targetCount, | ||
| int | defaultCapacity = 16384 |
||
| ) |
Description of constructor still missing
References QGpCoreTools::IncreaseStorage::allocateVector(), parameterCount(), and targetCount().
: IncreaseStorage(defaultCapacity) { ASSERT(parameterCount>0); _parameterCount=parameterCount; _targetCount=targetCount; _parameters=(int*)allocateVector(sizeof(int)*_parameterCount); _misfits=(double*)allocateVector(sizeof(double)*_targetCount); //_volumes=(double*)allocateVector(sizeof(double)); _highestValidIndex=-1; }
{
free(_parameters);
free(_misfits);
}
| int DinverCore::ModelSet::add | ( | const RealSpace & | parameterSpace, |
| const double * | misfits = 0 |
||
| ) |
Add model to the model set. Returns -1 if model already exists. Else, it returns the index of the added model. This function is thread safe.
References contains(), DinverCore::Model::data(), DinverCore::Parameter::gridValue(), QGpCoreTools::IncreaseStorage::size(), and DinverCore::RealSpace::variableParameter().
Referenced by importModelsFromAscii(), and importModelsFromReport().
{
// Used to test whether the new model already exist in main model set
ModelSet newModelSet(_parameterCount, 1);
int * v=newModelSet._parameters;
for(int ip=0; ip<_parameterCount; ip++) {
v[ip]=parameterSpace.variableParameter(ip)->gridValue();
}
Model newModel(0, &newModelSet);
_setLock.lock();
if(!contains(newModel)) {
int index=IncreaseStorage::size();
double * misfitPtr=_misfits+_targetCount*index;
// highestValidIndex is increased only if models have their misfit values
// If no misfit is provided here, we expect a setMisfit() call once computed.
if(misfits) {
memcpy((char *)misfitPtr, misfits, sizeof(double)*_targetCount);
if(index>_highestValidIndex) {
_highestValidIndex=index;
}
} else {
for(int i=0;i<_targetCount;i++) {
misfitPtr[i]=1e99;
}
}
//_volumes[index]=0.0; // It will generate error immediately detectable if not initialized
// // A null volume is considered as a non initialized volume
int * paramPtr=_parameters+_parameterCount*index;
memcpy((char *)paramPtr, newModel.data(), sizeof(int)*_parameterCount);
// Misfits and model values are already written, now the size can be changed.
// This way is always safe to access all models with index < size() without
// locking if no capacity increase required.
IncreaseStorage::add();
insert(Model(index, this));
_setLock.unlock();
return index;
} else {
_setLock.unlock();
return -1;
}
}
| int DinverCore::ModelSet::bestModel | ( | ) | const |
Returns the index of the best model. All models are checked. This function is valid only for single misfit inversion.
References count(), misfit(), and TRACE.
Referenced by DinverCore::ModelRepository::bestModelIndex(), and DinverCore::Neighborhood::bestModelIndex().
| void DinverCore::ModelSet::clear | ( | ) |
Reimplemented from QGpCoreTools::IncreaseStorage.
References QGpCoreTools::IncreaseStorage::allocateVector().
Referenced by DinverCore::Neighborhood::clear(), and DinverCore::ModelRepository::clear().
{
free(_parameters);
free(_misfits);
IncreaseStorage::clear();
_parameters=(int*)allocateVector(sizeof(int)*_parameterCount);
_misfits=(double*)allocateVector(sizeof(double)*_targetCount);
//_volumes=(double*)allocateVector(sizeof(double));
_highestValidIndex=-1;
}
| int DinverCore::ModelSet::count | ( | ) | const [inline] |
Referenced by bestModel(), DinverCore::ImportanceSampling::generate(), DinverCore::Neighborhood::importModels(), DinverCore::ModelRepository::importModels(), DinverCore::ScaledModels::ScaledModels(), DinverCore::ActiveModels::setNavigatorHits(), DinverCore::ModelRepository::validModelCount(), DinverCore::Neighborhood::visitedModelCount(), and DinverCore::ModelRepository::visitedModelCount().
{return _highestValidIndex+1;}
| const int* DinverCore::ModelSet::firstModel | ( | ) | const [inline] |
{return _parameters;}
| bool DinverCore::ModelSet::importModels | ( | RealSpace & | parameterSpace, |
| QString | fileName, | ||
| bool | strict = true |
||
| ) |
If strict is true (default), a strict match of the current parameterization and the one of the report is checked. Else, only the number of parameters is checked.
References QGpCoreTools::endl(), importModelsFromAscii(), importModelsFromReport(), DinverCore::ReportReader::isReportFormat(), DinverCore::ReportReader::open(), DinverCore::ReportReader::synchronize(), QGpCoreTools::tr(), and TRACE.
Referenced by DinverCore::ImportanceSampling::importModels(), and DinverCore::Neighborhood::importModels().
{
TRACE;
if(ReportReader::isReportFormat(fileName)) {
ReportReader report(fileName);
if(!report.open()) {
App::stream() << tr("Error opening report %1").arg(fileName) << endl;
return false;
}
report.synchronize();
return importModelsFromReport(parameterSpace, report, strict);
} else {
return importModelsFromAscii(parameterSpace, fileName);
}
}
| bool DinverCore::ModelSet::importModelsFromAscii | ( | RealSpace & | parameterSpace, |
| QString | fileName | ||
| ) |
Format is:
References add(), DinverCore::RealSpace::allParameterCount(), QGpCoreTools::endl(), QGpCoreTools::StringSection::isValid(), QGpCoreTools::StringSection::nextField(), DinverCore::RealSpace::parameter(), DinverCore::Parameter::setRealValue(), QGpCoreTools::StringSection::toDouble(), QGpCoreTools::tr(), TRACE, and DinverCore::RealSpace::variableParameterCount().
Referenced by importModels(), and DinverCore::ModelRepository::importModels().
{
TRACE;
ASSERT(parameterSpace.variableParameterCount()==_parameterCount);
QFile f(fileName);
if( !f.open(QIODevice::ReadOnly) ) {
App::stream() << tr("Error opening file %1").arg(fileName) << endl;
return false;
}
QTextStream s(&f);
QString line;
StringSection val, content(line);
const QChar * ptr;
int i;
int iLine=0;
int nParams=parameterSpace.allParameterCount();
while(!s.atEnd()) {
line=s.readLine();
iLine++;
StringSection content(line);
if(line[0]!='#') {
ptr=0;
for(i=0; i<nParams; i++) {
val=content.nextField(ptr);
if(val.isValid()) parameterSpace.parameter(i)->setRealValue(val.toDouble()); else break;
}
if(i==nParams) {
double misfits[_targetCount];
for(i=0; i<_targetCount; i++) {
val=content.nextField(ptr);
if(val.isValid()) misfits[i]=val.toDouble(); else break;
}
// All parameter values were correctly retreived and also misfit (last one), then add a new model
if(i==_targetCount) {
add(parameterSpace, misfits);
} else {
App::stream() << tr("Erreur parsing file %1 at line %2, misfit[%3]").arg(fileName).arg(iLine).arg(i) << endl;
return false;
}
} else {
App::stream() << tr("Erreur parsing file %1 at line %2, parameter[%3]").arg(fileName).arg(iLine).arg(i) << endl;
return false;
}
}
}
return true;
}
| bool DinverCore::ModelSet::importModelsFromReport | ( | RealSpace & | parameterSpace, |
| ReportReader & | report, | ||
| bool | strict = true |
||
| ) |
References add(), DinverCore::RealSpace::allParameterCount(), DinverCore::RealSpace::checksum(), QGpCoreTools::endl(), DinverCore::ReportReader::fileName(), DinverCore::ReportReader::modelBlock(), DinverCore::ReportReader::nModels(), DinverCore::RealSpace::parameter(), DinverCore::Parameter::setRealValue(), DinverCore::ReportReader::stream(), QGpCoreTools::tr(), TRACE, TRACE_BUG, TRACE_BUG_INT, DinverCore::RealSpace::variableParameter(), and DinverCore::RealSpace::variableParameterCount().
Referenced by importModels(), and DinverCore::ModelRepository::importModels().
{
TRACE;
TRACE_BUG;
TRACE_BUG_INT(parameterSpace.variableParameterCount());
TRACE_BUG_INT(_parameterCount);
ASSERT(parameterSpace.variableParameterCount()==_parameterCount);
printf("importing models start\n");
uint refChecksum=parameterSpace.checksum();
int nParams, nTargets;
uint checksum;
double misfits[_targetCount];
int nImportedModels=0;
int varAllCompat=0;
int n=report.nModels();
QDataStream& s=report.stream();
printf("importing models %i\n", n);
for(int im=0;im<n;im++) {
report.modelBlock(im);
if(_targetCount>1) {
s >> nParams >> nTargets >> checksum;
if(nTargets!=_targetCount) {
App::stream() << tr("N targets (%1) must be %2").arg(nTargets).arg(_targetCount) << endl;
continue;
}
for(int im=0;im<_targetCount; im++) {
s >> misfits[im];
}
} else { // needed for compatibility (<=20091120 target count always equal 1)
s >> misfits[0] >> nParams >> checksum;
}
if(!strict || checksum==refChecksum) {
if(nParams==parameterSpace.allParameterCount()) {
int ip;
for(ip=0; ip<nParams; ip++) {
if(!parameterSpace.parameter(ip)->setRealValue(s)) break;
}
if(ip==nParams) {
add(parameterSpace, misfits);
nImportedModels++;
} else {
App::stream() << tr("Warning cannot import model at index %1 in file %2").arg(im).arg(report.fileName()) << endl;
}
} else if(nParams==parameterSpace.variableParameterCount()) {
varAllCompat++;
int ip;
for(ip=0; ip<nParams; ip++) {
if(!parameterSpace.variableParameter(ip)->setRealValue(s) ) break;
}
if(ip==nParams) {
add(parameterSpace, misfits);
nImportedModels++;
printf("%i imported models\n", nImportedModels);
} else {
App::stream() << tr("Warning cannot import model at index %1 in file %2").arg(im).arg(report.fileName()) << endl;
}
} else {
App::stream() << tr("N parameters (%1) must be %2").arg(nParams).arg(_parameterCount) << endl;
}
} else {
App::stream() << tr("Checksum (%1) must be %2").arg(checksum).arg(refChecksum) << endl;
}
}
App::stream() << tr("Importing %1 models from report %2").arg(nImportedModels).arg(report.fileName()) << endl;
if(varAllCompat>0) {
App::stream() << tr("Compatibility with previous reports, only variable parameters "
"stored in report file (%1 models).").arg(varAllCompat) << endl;
}
return true;
}
| void DinverCore::ModelSet::lock | ( | ) | [inline] |
Referenced by DinverCore::ModelRepository::createModel(), DinverCore::Neighborhood::lock(), and DinverCore::ModelRepository::lock().
{_modelLock.lockForRead();}
| const double* DinverCore::ModelSet::misfit | ( | int | modelIndex | ) | const [inline] |
Referenced by bestModel(), DinverCore::ActiveModels::misfit(), DinverCore::Neighborhood::misfit(), DinverCore::ModelRepository::misfit(), print(), and DinverCore::PdfPoint::probability().
{return _misfits+_targetCount*modelIndex;}
| const int* DinverCore::ModelSet::model | ( | int | modelIndex | ) | const [inline] |
Referenced by DinverCore::ImportanceSampling::generate(), DinverCore::ActiveModels::model(), DinverCore::ModelRepository::model(), DinverCore::Neighborhood::model(), print(), DinverCore::ScaledModels::ScaledModels(), DinverCore::Neighborhood::variableParameterValue(), and DinverCore::ModelRepository::variableParameterValue().
{return _parameters+_parameterCount*modelIndex;}
| int DinverCore::ModelSet::parameterCount | ( | ) | const [inline] |
Referenced by ModelSet(), DinverCore::Neighborhood::optimization(), DinverCore::ScaledModels::ScaledModels(), DinverCore::Neighborhood::variableParameterCount(), and DinverCore::ModelRepository::variableParameterCount().
{return _parameterCount;}
| void DinverCore::ModelSet::print | ( | int | modelIndex | ) | const |
References misfit(), and model().
Referenced by DinverCore::ActiveModels::print().
{
const int * paramPtr=model(modelIndex);
printf("%i : ", modelIndex);
for(int i=0; i<_parameterCount; i++) {
printf("%i\t", paramPtr[ i ]);
}
printf(" --> ");
const double * misfitPtr=misfit(modelIndex);
for(int i=0; i<_targetCount; i++) {
printf("%15lg\t", misfitPtr[ i ]);
}
//printf(" --vol-- ");
//for(int i=0; i<_targetCount; i++) {
// printf("%15lg\t", misfitPtr[ i ]/_volumes[modelIndex]);
//}
printf( "\n");
}
| void DinverCore::ModelSet::reserve | ( | int | n | ) | [inline] |
Reimplemented from QGpCoreTools::IncreaseStorage.
Referenced by DinverCore::Neighborhood::optimization().
{IncreaseStorage::reserve(n);}
| void DinverCore::ModelSet::setMisfit | ( | int | modelIndex, |
| int | targetIndex, | ||
| double | m | ||
| ) | [inline] |
{
_misfits[_targetCount*modelIndex+targetIndex]=m;
if(modelIndex>_highestValidIndex) {
_highestValidIndex=modelIndex;
}
}
| int DinverCore::ModelSet::targetCount | ( | ) | const [inline] |
Referenced by ModelSet().
{return _targetCount;}
| void DinverCore::ModelSet::unlock | ( | ) | [inline] |
Referenced by DinverCore::ModelRepository::createModel(), DinverCore::Neighborhood::unlock(), and DinverCore::ModelRepository::unlock().
{_modelLock.unlock();}