bool MeasureRecordMeasureVectorIndexLess::operator()( const MeasureRecord& left, const MeasureRecord& right) const { OptionalInt leftIndex = left.measureVectorIndex(); OptionalInt rightIndex = right.measureVectorIndex(); if (leftIndex && rightIndex) { return (leftIndex.get() < rightIndex.get()); } return false; }
std::vector<VariableRecord> FunctionRecord_Impl::variableRecords() const { VariableRecordVector result; ProjectDatabase database = projectDatabase(); QSqlQuery query(*(database.qSqlDatabase())); query.prepare(toQString("SELECT * FROM " + VariableRecord::databaseTableName() + " WHERE functionRecordId=:functionRecordId ORDER BY variableVectorIndex")); query.bindValue(":functionRecordId",id()); assertExec(query); OptionalInt previousIndex; bool resort(false); while (query.next()) { result.push_back(VariableRecord::factoryFromQuery(query, database).get()); int index = result.back().variableVectorIndex(); if (previousIndex) { if (index <= previousIndex.get()) { resort = true; // if variables are changed around, order in database itself can // get out of date (as compared to database + dirty records). } } previousIndex = index; } if (resort) { VariableRecordVariableVectorIndexLess comparator; std::sort(result.begin(),result.end(),comparator); } return result; }
std::vector<FunctionRecord> OptimizationProblemRecord_Impl::objectiveRecords() const { FunctionRecordVector result; ProjectDatabase database = projectDatabase(); QSqlQuery query(*(database.qSqlDatabase())); query.prepare(toQString("SELECT * FROM " + FunctionRecord::databaseTableName() + " WHERE problemRecordId=:problemRecordId AND functionType=:functionType " + "ORDER BY functionVectorIndex")); query.bindValue(":problemRecordId", id()); query.bindValue(":functionType",FunctionType::Objective); assertExec(query); OptionalInt previousIndex; bool resort(false); while (query.next()) { result.push_back(FunctionRecord::factoryFromQuery(query, database).get()); int index = result.back().functionVectorIndex(); if (previousIndex) { if (index <= previousIndex.get()) { resort = true; // if functions are moved around, order in database itself can // get out of date (as compared to database + dirty records). } } previousIndex = index; } if (resort) { FunctionRecordFunctionVectorIndexLess comparator; std::sort(result.begin(),result.end(),comparator); } return result; }
std::vector<WorkflowRecord> ProblemRecord_Impl::workflowRecords() const { WorkflowRecordVector result; ProjectDatabase database = projectDatabase(); QSqlQuery query(*(database.qSqlDatabase())); query.prepare(toQString("SELECT * FROM " + WorkflowRecord::databaseTableName() + " WHERE problemRecordId=:problemRecordId ORDER BY workflowIndex")); query.bindValue(":problemRecordId",id()); assertExec(query); OptionalInt previousIndex; bool resort(false); while (query.next()) { result.push_back(WorkflowRecord::factoryFromQuery(query, database).get()); int index = result.back().workflowIndex(); if (previousIndex) { if (index < previousIndex.get()) { resort = true; } } previousIndex = index; } if (resort) { WorkflowRecordWorkflowIndexLess comparator; std::sort(result.begin(),result.end(),comparator); } return result; }
std::vector<MeasureRecord> MeasureGroupRecord_Impl::measureRecords(bool selectedMeasuresOnly) const { std::vector<MeasureRecord> result; ProjectDatabase database = this->projectDatabase(); QSqlQuery query(*(database.qSqlDatabase())); if (selectedMeasuresOnly){ query.prepare(toQString("SELECT * FROM " + MeasureRecord::databaseTableName() + " WHERE variableRecordId=:id AND isSelected=:isSelected ORDER BY measureVectorIndex")); query.bindValue(":id", this->id()); query.bindValue(":isSelected", true); }else{ query.prepare(toQString("SELECT * FROM " + MeasureRecord::databaseTableName() + " WHERE variableRecordId=:id ORDER BY measureVectorIndex")); query.bindValue(":id", this->id()); } assertExec(query); OptionalInt previousIndex; bool resort(false); while (query.next()){ boost::optional<MeasureRecord> measure = MeasureRecord::factoryFromQuery(query, database); OS_ASSERT(measure); result.push_back(*measure); OptionalInt index = result.back().measureVectorIndex(); if (previousIndex && index) { if (index.get() <= previousIndex.get()) { resort = true; } } if (index) { previousIndex = index; } } if (resort) { MeasureRecordMeasureVectorIndexLess comparator; std::sort(result.begin(),result.end(),comparator); } return result; }
boost::optional<IdfObject> ForwardTranslator::translateTableMultiVariableLookup( TableMultiVariableLookup& modelObject ) { OptionalString s; OptionalDouble d; OptionalModelObject temp; OptionalInt n; // Create a new IddObjectType::Table_MultiVariableLookup IdfObject idfObject(IddObjectType::Table_MultiVariableLookup); m_idfObjects.push_back(idfObject); // Name s = modelObject.name(); if(s) { idfObject.setName(*s); } // InterpolationMethod if( (s = modelObject.interpolationMethod()) ) { idfObject.setString(Table_MultiVariableLookupFields::InterpolationMethod,s.get()); } // NumberofInterpolationPoints if( (n = modelObject.numberofInterpolationPoints()) ) { idfObject.setInt(Table_MultiVariableLookupFields::NumberofInterpolationPoints,n.get()); } // CurveType if( (s = modelObject.curveType()) ) { idfObject.setString(Table_MultiVariableLookupFields::CurveType,s.get()); } // TableDataFormat if( (s = modelObject.tableDataFormat()) ) { idfObject.setString(Table_MultiVariableLookupFields::TableDataFormat,s.get()); } // ExternalFileName // Not supported // X1SortOrder idfObject.setString(Table_MultiVariableLookupFields::X1SortOrder,"Ascending"); // X2SortOrder idfObject.setString(Table_MultiVariableLookupFields::X2SortOrder,"Ascending"); // NormalizationReference if( (d = modelObject.normalizationReference()) ) { idfObject.setDouble(Table_MultiVariableLookupFields::NormalizationReference,d.get()); } // MinimumValueofX1 if( (d = modelObject.minimumValueofX1()) ) { idfObject.setDouble(Table_MultiVariableLookupFields::MinimumValueofX1,d.get()); } // MaximumValueofX1 if( (d = modelObject.maximumValueofX1()) ) { idfObject.setDouble(Table_MultiVariableLookupFields::MaximumValueofX1,d.get()); } // MinimumValueofX2 if( (d = modelObject.minimumValueofX2()) ) { idfObject.setDouble(Table_MultiVariableLookupFields::MinimumValueofX2,d.get()); } // MaximumValueofX2 if( (d = modelObject.maximumValueofX2()) ) { idfObject.setDouble(Table_MultiVariableLookupFields::MaximumValueofX2,d.get()); } // MinimumValueofX3 if( (d = modelObject.minimumValueofX3()) ) { idfObject.setDouble(Table_MultiVariableLookupFields::MinimumValueofX3,d.get()); } // MaximumValueofX3 if( (d = modelObject.maximumValueofX3()) ) { idfObject.setDouble(Table_MultiVariableLookupFields::MaximumValueofX3,d.get()); } // MinimumValueofX4 if( (d = modelObject.minimumValueofX4()) ) { idfObject.setDouble(Table_MultiVariableLookupFields::MinimumValueofX4,d.get()); } // MaximumValueofX4 if( (d = modelObject.maximumValueofX4()) ) { idfObject.setDouble(Table_MultiVariableLookupFields::MaximumValueofX4,d.get()); } // MinimumValueofX5 if( (d = modelObject.minimumValueofX5()) ) { idfObject.setDouble(Table_MultiVariableLookupFields::MinimumValueofX5,d.get()); } // MaximumValueofX5 if( (d = modelObject.maximumValueofX5()) ) { idfObject.setDouble(Table_MultiVariableLookupFields::MaximumValueofX5,d.get()); } // MinimumTableOutput if( (d = modelObject.minimumTableOutput()) ) { idfObject.setDouble(Table_MultiVariableLookupFields::MinimumTableOutput,d.get()); } // MaximumTableOutput if( (d = modelObject.maximumTableOutput()) ) { idfObject.setDouble(Table_MultiVariableLookupFields::MaximumTableOutput,d.get()); } // InputUnitTypeforX1 if( (s = modelObject.inputUnitTypeforX1()) ) { idfObject.setString(Table_MultiVariableLookupFields::InputUnitTypeforX1,s.get()); } // InputUnitTypeforX2 if( (s = modelObject.inputUnitTypeforX2()) ) { idfObject.setString(Table_MultiVariableLookupFields::InputUnitTypeforX2,s.get()); } // InputUnitTypeforX3 if( (s = modelObject.inputUnitTypeforX3()) ) { idfObject.setString(Table_MultiVariableLookupFields::InputUnitTypeforX3,s.get()); } // InputUnitTypeforX4 if( (s = modelObject.inputUnitTypeforX4()) ) { idfObject.setString(Table_MultiVariableLookupFields::InputUnitTypeforX4,s.get()); } // InputUnitTypeforX5 if( (s = modelObject.inputUnitTypeforX5()) ) { idfObject.setString(Table_MultiVariableLookupFields::InputUnitTypeforX5,s.get()); } // OutputUnitType if( (s = modelObject.outputUnitType()) ) { idfObject.setString(Table_MultiVariableLookupFields::OutputUnitType,s.get()); } // NumberofIndependentVariables if( (n = modelObject.numberofIndependentVariables()) ) { idfObject.setInt(Table_MultiVariableLookupFields::NumberofIndependentVariables,n.get()); } unsigned t_numNonextensibleFields = idfObject.numNonextensibleFields(); unsigned t_currentFieldIndex = t_numNonextensibleFields; unsigned t_numberofIndependentVariables = modelObject.numberofIndependentVariables(); // Set the number of xValues for each independent variable for(unsigned i = 0; i != t_numberofIndependentVariables; ++i) { unsigned xValueCount = modelObject.getImpl<model::detail::TableMultiVariableLookup_Impl>()->xValues(i).size(); idfObject.setUnsigned(t_currentFieldIndex,xValueCount); ++t_currentFieldIndex; } // Set the xValues for each independent variable for(unsigned i = 0; i != t_numberofIndependentVariables; ++i) { std::vector<double> xValues = modelObject.getImpl<model::detail::TableMultiVariableLookup_Impl>()->xValues(i); for(auto it = xValues.begin(); it != xValues.end(); ++it) { idfObject.setDouble(t_currentFieldIndex,*it); ++t_currentFieldIndex; } } // Set the table data if(t_numberofIndependentVariables == 1u) { // If there is just one variable then we just make a list of the y values. std::vector<double> xValues = modelObject.getImpl<model::detail::TableMultiVariableLookup_Impl>()->xValues(0); for(auto it = xValues.begin(); it != xValues.end(); ++it) { std::vector<double> coord(1); coord[0] = *it; boost::optional<double> yValue = modelObject.getImpl<model::detail::TableMultiVariableLookup_Impl>()->yValue(coord); if(yValue) { idfObject.setDouble(t_currentFieldIndex,yValue.get()); } else { idfObject.setString(t_currentFieldIndex,""); } ++t_currentFieldIndex; } } else if(t_numberofIndependentVariables == 2u) { // If there are two variables we make a list of the y values corresponding to x1 and x2, // iterating over x2 in the outer layer std::vector<double> x1Values = modelObject.getImpl<model::detail::TableMultiVariableLookup_Impl>()->xValues(0); std::vector<double> x2Values = modelObject.getImpl<model::detail::TableMultiVariableLookup_Impl>()->xValues(1); for(auto it2 = x2Values.begin(); it2 != x2Values.end(); ++it2) { for(auto it1 = x1Values.begin(); it1 != x1Values.end(); ++it1) { std::vector<double> coord(2); coord[0] = *it1; coord[1] = *it2; boost::optional<double> yValue = modelObject.yValue(coord); if(yValue) { idfObject.setDouble(t_currentFieldIndex,yValue.get()); } else { idfObject.setString(t_currentFieldIndex,""); } ++t_currentFieldIndex; } } } else { std::vector<std::pair<std::vector<double>,double> > points = modelObject.points(); // Slice the first and second x values off the coordinates std::vector<std::vector<double> > slices; for(std::vector<std::pair<std::vector<double>,double> >::const_iterator it = points.begin(); it != points.end(); ++it) { OS_ASSERT(it->first.size() == t_numberofIndependentVariables); std::vector<double> slice(it->first.begin() + 2,it->first.begin() + t_numberofIndependentVariables); slices.push_back(slice); } // Remove duplicate slices std::sort(slices.begin(),slices.end()); slices.erase(std::unique(slices.begin(),slices.end()),slices.end()); // Iterate over each slice that is left, creating a 2D table for each one for(std::vector<std::vector<double> >::const_iterator it = slices.begin(); it != slices.end(); ++it) { for(auto it2 = it->begin(); it2 != it->end(); ++it2) { idfObject.setDouble(t_currentFieldIndex,*it2); ++t_currentFieldIndex; } std::vector<double> x1Values = modelObject.getImpl<model::detail::TableMultiVariableLookup_Impl>()->xValues(0); std::vector<double> x2Values = modelObject.getImpl<model::detail::TableMultiVariableLookup_Impl>()->xValues(1); for(auto x2It = x2Values.begin(); x2It != x2Values.end(); ++x2It) { for(auto x1It = x1Values.begin(); x1It != x1Values.end(); ++x1It) { std::vector<double> coord(2); coord[0] = *x1It; coord[1] = *x2It; coord.insert(coord.end(),it->begin(),it->end()); boost::optional<double> yValue = modelObject.yValue(coord); if(yValue) { idfObject.setDouble(t_currentFieldIndex,yValue.get()); } else { idfObject.setString(t_currentFieldIndex,""); } ++t_currentFieldIndex; } } } } return idfObject; }
int DesignOfExperiments_Impl::createNextIteration(Analysis& analysis) { int result(0); // to make sure problem type check has already occurred. this is stated usage in header. OS_ASSERT(analysis.algorithm().get() == getPublicObject<DesignOfExperiments>()); // nothing else is supported yet DesignOfExperimentsOptions options = designOfExperimentsOptions(); OS_ASSERT(options.designType() == DesignOfExperimentsType::FullFactorial); if (isComplete()) { LOG(Info,"Algorithm is already marked as complete. Returning without creating new points."); return result; } if (options.maxIter() && options.maxIter().get() < 1) { LOG(Info,"Maximum iterations set to less than one. No DataPoints will be added to Analysis '" << analysis.name() << "', and the Algorithm will be marked complete."); markComplete(); return result; } OptionalInt mxSim = options.maxSims(); DataPointVector dataPoints = analysis.getDataPoints("DOE"); int totPoints = dataPoints.size(); if (mxSim && (totPoints >= *mxSim)) { LOG(Info,"Analysis '" << analysis.name() << "' already contains " << totPoints << " DataPoints added by the DesignOfExperiments algorithm, which meets or exceeds the " << "maximum number specified in this algorithm's options object, " << *mxSim << ". " << "No data points will be added and the Algorithm will be marked complete."); markComplete(); return result; } m_iter = 1; // determine all combinations std::vector< std::vector<QVariant> > variableValues; for (const Variable& variable : analysis.problem().variables()) { // variable must be DiscreteVariable, otherwise !isCompatibleProblemType(analysis.problem()) DiscreteVariable discreteVariable = variable.cast<DiscreteVariable>(); IntVector dvValues = discreteVariable.validValues(true); std::vector< std::vector<QVariant> > currentValues = variableValues; for (IntVector::const_iterator it = dvValues.begin(), itEnd = dvValues.end(); it != itEnd; ++it) { std::vector< std::vector<QVariant> > nextSet = currentValues; if (currentValues.empty()) { variableValues.push_back(std::vector<QVariant>(1u,QVariant(*it))); } else { for (std::vector<QVariant>& point : nextSet) { point.push_back(QVariant(*it)); } if (it == dvValues.begin()) { variableValues = nextSet; } else { variableValues.insert(variableValues.end(),nextSet.begin(),nextSet.end()); } } } } // create data points and add to analysis for (const std::vector<QVariant>& value : variableValues) { DataPoint dataPoint = analysis.problem().createDataPoint(value).get(); dataPoint.addTag("DOE"); bool added = analysis.addDataPoint(dataPoint); if (added) { ++result; ++totPoints; if (mxSim && (totPoints == mxSim.get())) { break; } } } if (result == 0) { LOG(Trace,"No new points were added, so marking this DesignOfExperiments complete."); markComplete(); } return result; }