Added Settings struct. Expanded printing of evaluation results.Refactoring

This commit is contained in:
iasonmanolas 2022-02-18 17:46:28 +02:00
parent 45eed0e3da
commit 5c4f8c0bd5
2 changed files with 288 additions and 95 deletions

View File

@ -1,5 +1,6 @@
#include "reducedmodelevaluator.hpp"
#include "hexagonremesher.hpp"
#include "reducedmodel.hpp"
#include "trianglepatterngeometry.hpp"
#include <filesystem>
@ -19,9 +20,9 @@ ReducedModelEvaluator::Results ReducedModelEvaluator::evaluateReducedModel(
const std::filesystem::path scenariosDirectoryPath
= "/home/iason/Coding/Projects/Approximating shapes with flat "
"patterns/ReducedModelEvaluator/Scenarios";
const std::filesystem::path reducedPatternFilePath
= "/home/iason/Coding/Projects/Approximating shapes with flat "
"patterns/ReducedModelOptimization/TestSet/ReducedPatterns/single_reduced.ply";
// const std::filesystem::path reducedPatternFilePath
// = "/home/iason/Coding/Projects/Approximating shapes with flat "
// "patterns/ReducedModelOptimization/TestSet/ReducedPatterns/single_reduced.ply";
const std::filesystem::path fullPatternTessellatedResultsDirectoryPath
= "/home/iason/Coding/Projects/Approximating shapes with flat "
"patterns/ReducedModelEvaluator/TessellatedResults";
@ -29,65 +30,194 @@ ReducedModelEvaluator::Results ReducedModelEvaluator::evaluateReducedModel(
return evaluateReducedModel(optimizationResult,
tileInto_triMesh_filePath,
scenariosDirectoryPath,
reducedPatternFilePath,
// reducedPatternFilePath,
fullPatternTessellatedResultsDirectoryPath);
}
//void ReducedModelEvaluator::printResults(const Results &evaluationResults,
// const std::string &resultsLabel,
// const std::filesystem::path &resultsOutputPath)
//{
// const bool outputPathIsDirectory = !resultsOutputPath.empty()
// && !resultsOutputPath.has_extension();
// const bool outputPathIsFile = !resultsOutputPath.empty() && resultsOutputPath.has_extension();
// assert(outputPathIsDirectory && outputPathIsFile);
// if (outputPathIsDirectory) {
// std::filesystem::create_directories(resultsOutputPath);
// }
//#else
// std::filesystem::path csvOutputFilePath;
// bool shouldOverwrite = false;
// if (outputPathIsDirectory) {
// csvOutputFilePath = std::filesystem::path(resultsOutputPath)
// .append("distances_" + resultsLabel + ".csv")
// .string();
// shouldOverwrite = true;
// } else if (outputPathIsFile) {
// csvOutputFilePath = resultsOutputPath;
// }
// csvFile csvOutput(csvOutputFilePath, shouldOverwrite);
// printResults(evaluationResults, resultsLabel, csvOutput);
//}
void ReducedModelEvaluator::printResults(const Results &evaluationResults,
const std::string &resultsLabel)
{
csvFile csvOutputToCout({}, true);
Settings exportSettings;
exportSettings.exportingDirection = Vertical;
exportSettings.shouldWriteHeader = false;
exportSettings.resultsLabel = resultsLabel;
printResults(evaluationResults, exportSettings, csvOutputToCout);
}
void ReducedModelEvaluator::printResults(const Results &evaluationResults,
const std::string &resultsLabel,
const std::filesystem::path &resultsOutputDirPath)
const Settings &settings,
csvFile &csvOutput)
{
//report distance
// csvFile csv_results("", flse);
if (!resultsOutputDirPath.empty()) {
std::filesystem::create_directories(resultsOutputDirPath);
if (settings.shouldWriteHeader) {
printHeader(settings, csvOutput);
// csvOutput << "Average error";
// csvOutput<<"Cumulative error";
csvOutput << endrow;
}
#ifdef POLYSCOPE_DEFINED
csvFile csv_results({}, false);
#else
std::filesystem::path csvOutputFilePath = resultsOutputDirPath.empty()
? ""
: std::filesystem::path(resultsOutputDirPath)
.append("distances_" + resultsLabel + ".csv")
.string();
csvFile csv_results(csvOutputFilePath, true);
#endif
csv_results << resultsLabel;
csv_results << endrow;
#ifdef POLYSCOPE_DEFINED
csv_results /*<< "Job Label"*/
<< "drm2Reduced"
<< "norm_drm2Reduced";
#else
csv_results << "Job Label"
<< "drm2Reduced"
<< "norm_drm2Reduced";
if (!settings.resultsLabel.empty()) {
csvOutput << settings.resultsLabel;
}
if (settings.exportingDirection == Vertical) {
printResultsVertically(evaluationResults, csvOutput);
} else {
printResultsHorizontally(evaluationResults, csvOutput);
}
}
#endif
csv_results << endrow;
// double sumOfNormalizedFull2Reduced = 0;
void ReducedModelEvaluator::printHeader(const Settings &settings, csvFile &csvOutput)
{
if (settings.exportingDirection == Horizontal) {
csvOutput << csvExportingDataStrings[settings.exportingData];
csvOutput.endrow();
// csvOutput << "Job label";
for (int jobIndex = 0; jobIndex < ReducedModelEvaluator::scenariosTestSetLabels.size();
jobIndex++) {
const std::string &jobLabel = ReducedModelEvaluator::scenariosTestSetLabels[jobIndex];
csvOutput << jobLabel;
}
} else {
std::cout << "Vertical header not defined" << std::endl;
assert(false);
std::terminate();
}
}
void ReducedModelEvaluator::printResultsHorizontally(const Results &evaluationResults,
csvFile &csvOutput)
{
//print header
//print raw error
constexpr bool shouldPrintRawError = false;
if (shouldPrintRawError) {
// csvOutput << "drm2Reduced";
double sumOfFull2Reduced = 0;
int numOfNonEvaluatedScenarios = 0;
for (int jobIndex = 0; jobIndex < ReducedModelEvaluator::scenariosTestSetLabels.size();
jobIndex++) {
const double &distance_fullDrmToReduced = evaluationResults
.distances_drm2reduced[jobIndex];
if (distance_fullDrmToReduced == -1) {
csvOutput << "notEvaluated";
numOfNonEvaluatedScenarios++;
} else {
csvOutput << distance_fullDrmToReduced;
sumOfFull2Reduced += distance_fullDrmToReduced;
}
}
// const int numOfEvaluatedScenarios = ReducedModelEvaluator::scenariosTestSetLabels.size()
// - numOfNonEvaluatedScenarios;
// const double averageDistance_full2Reduced = sumOfFull2Reduced / numOfEvaluatedScenarios;
// csvOutput << averageDistance_full2Reduced;
// csvOutput << endrow;
}
//print normalized error
// csvOutput << "norm_drm2Reduced";
double sumOfNormalizedFull2Reduced = 0;
for (int jobIndex = 0; jobIndex < ReducedModelEvaluator::scenariosTestSetLabels.size();
jobIndex++) {
const double &distance_normalizedFullDrmToReduced
= evaluationResults.distances_normalizedDrm2reduced[jobIndex];
if (distance_normalizedFullDrmToReduced == -1) {
csvOutput << "notEvaluated";
} else {
csvOutput << distance_normalizedFullDrmToReduced;
sumOfNormalizedFull2Reduced += distance_normalizedFullDrmToReduced;
}
}
// const double averageDistance_normalizedFull2Reduced = sumOfNormalizedFull2Reduced
// / numOfEvaluatedScenarios;
// csvOutput << averageDistance_normalizedFull2Reduced;
// csvOutput << endrow;
}
void ReducedModelEvaluator::printResultsVertically(const Results &evaluationResults,
csvFile &csvOutput)
{
#ifdef POLYSCOPE_DEFINED
csvOutput << "drm2Reduced"
<< "norm_drm2Reduced";
#else
csvOutput << "Job Label"
<< "drm2Reduced"
<< "norm_drm2Reduced";
#endif
csvOutput << endrow;
double sumOfFull2Reduced = 0;
double sumOfNormalizedFull2Reduced = 0;
int numOfNonEvaluatedScenarios = 0;
for (int jobIndex = 0; jobIndex < ReducedModelEvaluator::scenariosTestSetLabels.size();
jobIndex++) {
const std::string &jobLabel = ReducedModelEvaluator::scenariosTestSetLabels[jobIndex];
const double &distance_fullDrmToReduced = evaluationResults.distances_drm2reduced[jobIndex];
const double &distance_normalizedFullDrmToReduced
= evaluationResults.distances_normalizedDrm2reduced[jobIndex];
#ifdef POLYSCOPE_DEFINED
csv_results /*<< jobLabel*/ << distance_fullDrmToReduced
<< distance_normalizedFullDrmToReduced;
#else
csv_results << jobLabel << distance_fullDrmToReduced << distance_normalizedFullDrmToReduced;
#ifndef POLYSCOPE_DEFINED
const std::string &jobLabel = ReducedModelEvaluator::scenariosTestSetLabels[jobIndex];
csvOutput << jobLabel;
#endif
csv_results << endrow;
if (distance_fullDrmToReduced == -1) {
csvOutput << "notEvaluated"
<< "notEvaluated";
numOfNonEvaluatedScenarios++;
} else {
csvOutput << distance_fullDrmToReduced << distance_normalizedFullDrmToReduced;
sumOfFull2Reduced += distance_fullDrmToReduced;
sumOfNormalizedFull2Reduced += distance_normalizedFullDrmToReduced;
}
csvOutput << endrow;
// sumOfNormalizedFull2Reduced += distance_normalizedFullDrmToReduced;
}
const int numOfEvaluatedScenarios = ReducedModelEvaluator::scenariosTestSetLabels.size()
- numOfNonEvaluatedScenarios;
const double averageDistance_full2Reduced = sumOfFull2Reduced / numOfEvaluatedScenarios;
const double averageDistance_normalizedFull2Reduced = sumOfNormalizedFull2Reduced
/ numOfEvaluatedScenarios;
#ifndef POLYSCOPE_DEFINED
csvOutput << "Average error";
#endif
csvOutput << averageDistance_full2Reduced << averageDistance_normalizedFull2Reduced;
csvOutput << endrow;
#ifndef POLYSCOPE_DEFINED
csvOutput << "Cumulative error";
#endif
csvOutput << sumOfFull2Reduced << sumOfNormalizedFull2Reduced;
csvOutput << endrow;
csvOutput << endrow;
}
ReducedModelEvaluator::Results ReducedModelEvaluator::evaluateReducedModel(
ReducedModelOptimization::Results &optimizationResult,
const std::filesystem::path &tileInto_triMesh_filePath,
const std::filesystem::path &scenariosDirectoryPath,
const std::filesystem::path &reducedPatternFilePath,
// const std::filesystem::path &reducedPatternFilePath,
const std::filesystem::path &fullPatternTessellatedResultsDirectoryPath)
{
// std::shared_ptr<VCGPolyMesh> pTileIntoSurface = std::make_shared<VCGPolyMesh>();
@ -151,14 +281,16 @@ ReducedModelEvaluator::Results ReducedModelEvaluator::evaluateReducedModel(
pTiledFullPattern->setLabel("Tiled_full_patterns");
// pTiledFullPattern->registerForDrawing();
//Tile reduced pattern into surface
PatternGeometry reducedPattern;
reducedPattern.load(reducedPatternFilePath);
reducedPattern.deleteDanglingVertices();
reducedPattern.interfaceNodeIndex = 1;
assert(reducedPattern.interfaceNodeIndex == 1);
// PatternGeometry reducedPattern;
ReducedModel reducedModel;
// reducedModel.registerForDrawing();
// polyscope::show();
reducedModel.deleteDanglingVertices();
// reducedPattern.interfaceNodeIndex = 1;
// assert(reducedPattern.interfaceNodeIndex == 1);
std::vector<PatternGeometry> reducedPatterns(1);
reducedPatterns[0].copy(reducedPattern);
const auto reducedPatternBaseTriangle = reducedPattern.computeBaseTriangle();
reducedPatterns[0].copy(reducedModel);
const auto reducedPatternBaseTriangle = reducedModel.computeBaseTriangle();
ReducedModelOptimization::Results::applyOptimizationResults_innerHexagon(
optimizationResult, reducedPatternBaseTriangle, reducedPatterns[0]);
@ -172,7 +304,8 @@ ReducedModelEvaluator::Results ReducedModelEvaluator::evaluateReducedModel(
tileIntoEdgeToTiledReducedVi,
perPatternIndexTiledReducedPatternEdgeIndices);
pTiledReducedPattern->setLabel("Tiled_reduced_patterns");
// pTiledReducedPattern->registerForDrawing();
// pTiledReducedPattern->registerForDrawing();
// polyscope::show();
std::unordered_map<FullPatternVertexIndex, ReducedPatternVertexIndex>
fullToReducedViMap; //of only the common vertices
@ -214,8 +347,8 @@ ReducedModelEvaluator::Results ReducedModelEvaluator::evaluateReducedModel(
optimizationResult, pTiledReducedPattern_simulationMesh);
pTiledReducedPattern_simulationMesh->reset();
Results evaluationResults;
evaluationResults.distances_drm2reduced;
evaluationResults.distances_normalizedDrm2reduced;
evaluationResults.distances_drm2reduced.fill(-1);
evaluationResults.distances_normalizedDrm2reduced.fill(-1);
for (int jobIndex = 0; jobIndex < scenariosTestSetLabels.size(); jobIndex++) {
const std::string &jobLabel = scenariosTestSetLabels[jobIndex];
const std::filesystem::path tiledReducedPatternJobFilePath
@ -255,7 +388,11 @@ ReducedModelEvaluator::Results ReducedModelEvaluator::evaluateReducedModel(
pJob_tiledReducedPattern->save(reducedJobDirectoryPath);
//Run scenario
////Full
const std::string patternLabel = optimizationResult.baseTriangleFullPattern.getLabel();
// const std::string patternLabel = std::to_string(
// optimizationResult.baseTriangleFullPattern.EN())
// + "_"
// + optimizationResult.baseTriangleFullPattern.getLabel();
const std::string &patternLabel = optimizationResult.baseTriangleFullPattern.getLabel();
const auto fullResultsFolderPath
= std::filesystem::path(scenarioDirectoryPath).append(patternLabel).append("Results");
if (shouldRerunFullPatternSimulation && std::filesystem::exists(fullResultsFolderPath)) {
@ -272,8 +409,34 @@ ReducedModelEvaluator::Results ReducedModelEvaluator::evaluateReducedModel(
assert(std::filesystem::exists(fullPatternJobFolderPath));
simulationResults_tiledFullPattern_drm.load(fullResultsFolderPath,
fullPatternJobFolderPath);
//#ifdef POLYSCOPE_DEFINED
// std::array<double, 3> resultsColor({28.0, 99.0, 227.0});
// simulationResults_tiledFullPattern_drm.registerForDrawing(resultsColor);
// std::ifstream ifs("CameraSettings.json");
// nlohmann::json json;
// ifs >> json;
// polyscope::view::setCameraFromJson(json.dump(), false);
// // polyscope::show();
// const std::string cameraJson = polyscope::view::getCameraJson();
// std::filesystem::path jsonFilePath("CameraSettings.json");
// std::ofstream jsonFile_cameraSettings(jsonFilePath.string());
// jsonFile_cameraSettings << cameraJson;
// jsonFile_cameraSettings.close();
// std::filesystem::create_directories("screenshots");
// const std::string screenshotOutputFilePath
// = (std::filesystem::current_path()
// .append("screenshots")
// .append(optimizationResult.label + "_" + pJob_tiledFullPattern->getLabel()))
// .string()
// + ".png";
// // std::cout << "Saving image to:" << screenshotOutputFilePath << std::endl;
// polyscope::screenshot(screenshotOutputFilePath, false);
// simulationResults_tiledFullPattern_drm.unregister();
//#endif
simulationResults_tiledFullPattern_drm.converged = true;
} else {
std::cout << "Drm results not found in:" << fullResultsFolderPath << std::endl;
//Full
std::cout << "Executing:" << jobLabel << std::endl;
DRMSimulationModel drmSimulationModel;

View File

@ -6,7 +6,24 @@
class ReducedModelEvaluator
{
public:
inline static constexpr int NumberOfEvaluationScenarios{36};
enum CSVExportingDirection { Vertical = 0, Horizontal };
enum CSVExportingData {
raw_drm2Reduced = 0,
norm_drm2Reduced,
raw_and_norm_drm2Reduced,
NumberOfDataTypes
};
inline static std::array<std::string, NumberOfDataTypes>
csvExportingDataStrings{"raw_drm2Reduced", "norm_drm2Reduced", "raw_and_norm_drm2Reduced"};
struct Settings
{
CSVExportingDirection exportingDirection{Horizontal};
CSVExportingData exportingData{norm_drm2Reduced};
bool shouldWriteHeader{true};
std::string resultsLabel;
};
inline static constexpr int NumberOfEvaluationScenarios{22};
struct Results
{
std::array<double, NumberOfEvaluationScenarios> distances_drm2reduced;
@ -18,51 +35,64 @@ public:
ReducedModelOptimization::Results &optimizationResult,
const std::filesystem::path &tileInto_triMesh_filePath,
const std::filesystem::path &scenariosDirectoryPath,
const std::filesystem::path &reducedPatternFilePath,
// const std::filesystem::path &reducedPatternFilePath,
const std::filesystem::path &fullPatternTessellatedResultsDirectoryPath);
static Results evaluateReducedModel(ReducedModelOptimization::Results &optimizationResult);
static void printResults(
const ReducedModelEvaluator::Results &evaluationResults,
const std::string &resultsLabel,
const std::filesystem::path &resultsOutputDirPath = std::filesystem::path());
static void printResultsVertically(const ReducedModelEvaluator::Results &evaluationResults,
csvFile &csvOutput);
static void printResults(const ReducedModelEvaluator::Results &evaluationResults,
const std::string &resultsLabel);
inline static std::array<std::string, NumberOfEvaluationScenarios>
scenariosTestSetLabels{"22Hex_randomBending0",
"22Hex_randomBending1",
"22Hex_randomBending2",
"22Hex_randomBending3",
"22Hex_randomBending4",
"22Hex_randomBending5",
"22Hex_randomBending6",
"22Hex_randomBending7",
"22Hex_randomBending8",
"22Hex_randomBending9",
"22Hex_randomBending10",
"22Hex_randomBending11",
"22Hex_randomBending12",
"22Hex_randomBending13",
"22Hex_randomBending14",
"22Hex_randomBending15",
"22Hex_randomBending16",
"22Hex_randomBending17",
"22Hex_randomBending18",
"22Hex_randomBending19",
"22Hex_randomBending20",
"22Hex_bending_0.005N",
"22Hex_bending_0.01N",
"22Hex_bending_0.03N",
"22Hex_bending_0.05N",
"22Hex_pullOppositeVerts_0.05N",
"22Hex_pullOppositeVerts_0.1N",
"22Hex_pullOppositeVerts_0.3N",
"22Hex_shear_2N",
"22Hex_shear_5N",
"22Hex_axial_10N",
"22Hex_axial_20N",
"22Hex_cylinder_0.05N",
"22Hex_cylinder_0.1N",
"22Hex_s_0.05N",
"22Hex_s_0.1N"};
inline static std::array<std::string, NumberOfEvaluationScenarios> scenariosTestSetLabels{
"22Hex_randomBending0",
"22Hex_randomBending1",
"22Hex_randomBending2",
// "22Hex_randomBending3",
"22Hex_randomBending4",
"22Hex_randomBending5",
// "22Hex_randomBending6",
// "22Hex_randomBending7",
"22Hex_randomBending8",
"22Hex_randomBending9",
"22Hex_randomBending10",
"22Hex_randomBending11",
"22Hex_randomBending12",
// "22Hex_randomBending13",
// "22Hex_randomBending14",
// "22Hex_randomBending15",
"22Hex_randomBending16",
"22Hex_randomBending17",
"22Hex_randomBending18",
"22Hex_randomBending19",
// "22Hex_randomBending20",
"22Hex_bending_0.005N",
"22Hex_bending_0.01N",
"22Hex_bending_0.03N",
// "22Hex_bending_0.05N",
"22Hex_pullOppositeVerts_0.05N",
"22Hex_pullOppositeVerts_0.1N",
// "22Hex_pullOppositeVerts_0.3N",
//#ifdef POLYSCOPE_DEFINED
// "22Hex_shear_2N",
// "22Hex_shear_5N",
// "22Hex_axial_10N",
// "22Hex_axial_20N",
//#else
// "notUsed_22Hex_shear_2N",
// "notUsed_22Hex_shear_5N",
// "notUsed_22Hex_axial_10N",
// "notUsed_22Hex_axial_20N",
//#endif
"22Hex_cylinder_0.05N",
"22Hex_cylinder_0.1N",
"22Hex_s_0.05N",
// "22Hex_s_0.1N"
};
static void printResultsHorizontally(const Results &evaluationResults, csvFile &csvOutput);
static void printResults(const Results &evaluationResults,
const Settings &settings,
csvFile &csvOutput);
static void printHeader(const Settings &settings, csvFile &csvOutput);
};
#endif // REDUCEDMODELEVALUATOR_HPP