ReducedModelOptimization/src/main.cpp

216 lines
10 KiB
C++

#include "csvfile.hpp"
#include "drmsimulationmodel.hpp"
#include "edgemesh.hpp"
#include "reducedmodelevaluator.hpp"
#include "reducedmodeloptimizer.hpp"
#include "simulationhistoryplotter.hpp"
#include "trianglepattterntopology.hpp"
#include <boost/algorithm/string.hpp>
#include <chrono>
#include <filesystem>
#include <iostream>
#include <iterator>
#include <stdexcept>
#include <string>
#include <string_view>
#include <vcg/complex/algorithms/update/position.h>
#ifdef POLYSCOPE_DEFINED
#include "polyscope/curve_network.h"
#include "polyscope/point_cloud.h"
#include "polyscope/polyscope.h"
#endif
int main(int argc, char *argv[])
{
// ReducedModelOptimization::Results optResults;
// optResults.load("/home/iason/Desktop/dlib_ensmallen_comparison/TestSets/"
// "singlePattern_dlib_firstSubmission/12@single_reduced(100000_1.20)");
// optResults.load("/home/iason/Coding/Projects/Approximating shapes with flat "
// "patterns/ReducedModelOptimization/Results/OptimizationResults/"
// "objectiveFunction/ConvergedJobs/testSet/7#12");
// optResults.load("/home/iason/Desktop/dlib_ensmallen_comparison/TestSets/"
// "singlePattern_dlib_23_12/12@single_reduced(100000_1.20)");
// optResults.load("/home/iason/Desktop/dlib_ensmallen_comparison/TestSets/"
// "singlePattern_ensmallen_AllVars_optParameters/7#12");
// ReducedModelEvaluator::evaluateReducedModel(optResults);
if (argc <= 5) {
std::cerr << "Wrong number of input parameters. Expects at least 4 input parameters."
"Usage:\n"
"1)full pattern file path\n"
"2)reduced pattern file path\n"
"3)Optimization settings json file path\n"
"4)Optimization results directory path\n"
"5)[optional]Intermediate results directory path\n"
"Exiting.."
<< std::endl;
std::cerr << "Input arguments are:" << std::endl;
std::copy(argv + 1, argv + argc, std::ostream_iterator<const char *>(std::cout, "\n"));
return 1;
}
// Populate the pattern pair to be optimized
const int interfaceNodeIndex = 3;
////Full pattern
const std::string filepath_fullPattern = argv[1];
PatternGeometry fullPattern(filepath_fullPattern);
// fullPattern.prependToLabel(std::to_string(fullPattern.EN()) + "#");
fullPattern.scale(0.03, interfaceNodeIndex);
////Reduced pattern
const std::string filepath_reducedPattern = argv[2];
PatternGeometry reducedPattern(filepath_reducedPattern);
reducedPattern.scale(0.03, interfaceNodeIndex);
// Set the optization settings
const std::filesystem::path optimizationSettingsFilePath = argv[3];
if (!std::filesystem::exists(optimizationSettingsFilePath)) {
std::cerr << "Input optimization settings file does not exist:"
<< optimizationSettingsFilePath << std::endl;
}
ReducedModelOptimization::Settings settings_optimization;
#ifdef POLYSCOPE_DEFINED
// settings_optimization.save(optimizationSettingsFilePath.parent_path());
// std::cout << "Save settings to:" << optimizationSettingsFilePath << std::endl;
#else
settings_optimization.load(optimizationSettingsFilePath);
#endif
// settings_optimization.setDefault();
// settings_optimization.rotationNormalizationEpsilon = 0;
// Optimize pairthere
const std::string optimizationName = std::to_string(fullPattern.EN()) + "#"
+ fullPattern.getLabel();
const std::string optimizationResultsDirectory = argv[4];
std::string resultsOutputDir;
bool optimizationResultFolderExists = false;
const std::filesystem::path crashedJobsDirPath(
std::filesystem::path(optimizationResultsDirectory)
.append("CrashedJobs")
.append(optimizationName));
if (std::filesystem::exists(crashedJobsDirPath)) {
resultsOutputDir = crashedJobsDirPath.string();
optimizationResultFolderExists = true;
}
const std::filesystem::path convergedJobsDirPath(
std::filesystem::path(optimizationResultsDirectory)
.append("ConvergedJobs")
.append(optimizationName));
if (std::filesystem::exists(convergedJobsDirPath)) {
resultsOutputDir = convergedJobsDirPath.string();
optimizationResultFolderExists = true;
}
ReducedModelOptimization::Results optimizationResults;
constexpr bool shouldReoptimize = true;
bool optimizationAlreadyComputed = false;
if (!shouldReoptimize && optimizationResultFolderExists) {
const bool resultsWereSuccessfullyLoaded = optimizationResults.load(resultsOutputDir);
if (resultsWereSuccessfullyLoaded && optimizationResults.settings == settings_optimization) {
optimizationAlreadyComputed = true;
}
}
if (!optimizationAlreadyComputed) {
auto start = std::chrono::system_clock::now();
const std::vector<size_t> numberOfNodesPerSlot{1, 0, 0, 2, 1, 2, 1};
assert(interfaceNodeIndex == numberOfNodesPerSlot[0] + numberOfNodesPerSlot[3]);
ReducedModelOptimizer optimizer(numberOfNodesPerSlot);
const bool input_intermediateResultsDirectoryDefined = argc == 6;
if (input_intermediateResultsDirectoryDefined) {
optimizer.setIntermediateResultsDirectoryPath(std::filesystem::path(argv[5]));
}
optimizer.initializePatterns(fullPattern,
reducedPattern,
settings_optimization.variablesRanges);
optimizer.optimize(settings_optimization, optimizationResults);
optimizationResults.label = optimizationName;
optimizationResults.baseTriangleFullPattern.copy(fullPattern);
optimizationResults.settings = settings_optimization;
auto end = std::chrono::system_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
optimizationResults.time = elapsed.count() / 1000.0;
if (!optimizationResults.wasSuccessful) {
// resultsOutputDir = crashedJobsDirPath.string();
return 1;
}
resultsOutputDir = convergedJobsDirPath.string();
optimizationResults.save(resultsOutputDir, true);
csvFile csv_resultsLocalFile(std::filesystem::path(resultsOutputDir).append("results.csv"),
true);
csvFile csv_results({}, false);
std::vector<csvFile *> csvVector{&csv_resultsLocalFile, &csv_results};
csv_results << "Name";
csv_resultsLocalFile << "Name";
optimizationResults.writeHeaderTo(csvVector);
settings_optimization.writeHeaderTo(csv_results);
csv_results << endrow;
csv_resultsLocalFile << endrow;
csv_results << optimizationName;
csv_resultsLocalFile << optimizationName;
optimizationResults.writeResultsTo(csvVector);
settings_optimization.writeSettingsTo(csv_results);
csv_results << endrow;
csv_resultsLocalFile << endrow;
}
ReducedModelEvaluator::evaluateReducedModel(optimizationResults);
#ifdef POLYSCOPE_DEFINED
std::vector<std::string> scenarioLabels(
optimizationResults.objectiveValue.perSimulationScenario_total.size());
const double colorAxial = 1;
const double colorShear = 3;
const double colorBending = 5;
const double colorDome = 0.1;
const double colorSaddle = 0;
std::vector<double> colors(
optimizationResults.objectiveValue.perSimulationScenario_total.size());
for (int scenarioIndex = 0; scenarioIndex < scenarioLabels.size(); scenarioIndex++) {
scenarioLabels[scenarioIndex]
= optimizationResults.reducedPatternSimulationJobs[scenarioIndex]->getLabel();
if (scenarioLabels[scenarioIndex].rfind("Axial", 0) == 0) {
colors[scenarioIndex] = colorAxial;
} else if (scenarioLabels[scenarioIndex].rfind("Shear", 0) == 0) {
colors[scenarioIndex] = colorShear;
} else if (scenarioLabels[scenarioIndex].rfind("Bending", 0) == 0) {
colors[scenarioIndex] = colorBending;
} else if (scenarioLabels[scenarioIndex].rfind("Dome", 0) == 0) {
colors[scenarioIndex] = colorDome;
} else if (scenarioLabels[scenarioIndex].rfind("Saddle", 0) == 0) {
colors[scenarioIndex] = colorSaddle;
} else {
std::cerr << "Label could not be identified" << std::endl;
}
}
std::vector<double> y(optimizationResults.objectiveValue.perSimulationScenario_total.size());
for (int scenarioIndex = 0; scenarioIndex < scenarioLabels.size(); scenarioIndex++) {
y[scenarioIndex]
// = optimizationResults.objectiveValue.perSimulationScenario_rawTranslational[scenarioIndex]
// + optimizationResults.objectiveValue.perSimulationScenario_rawRotational[scenarioIndex];
= optimizationResults.objectiveValue
.perSimulationScenario_total_unweighted[scenarioIndex];
}
std::vector<double> x = matplot::linspace(0, y.size() - 1, y.size());
std::vector<double> markerSizes(y.size(), 5);
SimulationResultsReporter::createPlot("scenario index",
"error",
x,
y,
markerSizes,
colors,
std::filesystem::path(resultsOutputDir)
.append("perScenarioObjectiveValues.png"));
// optimizationResults.saveMeshFiles();
std::cout << "Saved results to:" << resultsOutputDir << std::endl;
// optimizationResults.draw();
// ReducedModelOptimization::Results optResults;
// optResults.load("/home/iason/Desktop/dlib_ensmallen_comparison/TestSets/"
// "singlePattern_dlib_firstSubmission/12@single_reduced(100000_1.20)");
#endif
return 0;
}