2021-01-22 15:39:36 +01:00
|
|
|
#include "csvfile.hpp"
|
2021-04-16 10:41:40 +02:00
|
|
|
#include "drmsimulationmodel.hpp"
|
2020-11-23 10:06:45 +01:00
|
|
|
#include "edgemesh.hpp"
|
|
|
|
#include "reducedmodeloptimizer.hpp"
|
|
|
|
#include "simulationhistoryplotter.hpp"
|
2020-12-09 16:58:48 +01:00
|
|
|
#include "trianglepattterntopology.hpp"
|
2020-11-23 10:06:45 +01:00
|
|
|
#include <chrono>
|
|
|
|
#include <filesystem>
|
|
|
|
#include <iostream>
|
2021-02-01 15:10:24 +01:00
|
|
|
#include <iterator>
|
2020-11-23 10:06:45 +01:00
|
|
|
#include <stdexcept>
|
|
|
|
#include <string>
|
2020-12-09 16:58:48 +01:00
|
|
|
#include <vcg/complex/algorithms/update/position.h>
|
|
|
|
|
2021-02-24 18:41:12 +01:00
|
|
|
#ifdef POLYSCOPE_DEFINED
|
|
|
|
#include "polyscope/curve_network.h"
|
|
|
|
#include "polyscope/point_cloud.h"
|
|
|
|
#include "polyscope/polyscope.h"
|
|
|
|
#endif
|
2020-11-23 10:06:45 +01:00
|
|
|
int main(int argc, char *argv[]) {
|
2021-02-22 18:23:58 +01:00
|
|
|
if (argc < 3) {
|
|
|
|
std::cerr << "Specify at least the two pattern filepaths to be "
|
|
|
|
"optimized.Exiting.."
|
2021-02-19 16:32:15 +01:00
|
|
|
<< std::endl;
|
2021-02-18 14:33:31 +01:00
|
|
|
std::terminate();
|
|
|
|
}
|
2021-02-19 16:32:15 +01:00
|
|
|
|
2021-02-18 14:33:31 +01:00
|
|
|
// Populate the pattern pair to be optimized
|
2021-03-16 10:57:27 +01:00
|
|
|
const int interfaceNodeIndex=3;
|
2021-02-18 14:33:31 +01:00
|
|
|
////Full pattern
|
2021-02-22 18:23:58 +01:00
|
|
|
const std::string filepath_fullPattern = argv[1];
|
2021-03-15 18:56:14 +01:00
|
|
|
PatternGeometry fullPattern(filepath_fullPattern);
|
2021-07-07 17:19:59 +02:00
|
|
|
// fullPattern.prependToLabel(std::to_string(fullPattern.EN()) + "#");
|
|
|
|
fullPattern.scale(0.03, interfaceNodeIndex);
|
2021-02-18 14:33:31 +01:00
|
|
|
////Reduced pattern
|
2021-02-22 18:23:58 +01:00
|
|
|
const std::string filepath_reducedPattern = argv[2];
|
2021-03-15 18:56:14 +01:00
|
|
|
PatternGeometry reducedPattern(filepath_reducedPattern);
|
2021-03-26 10:58:13 +01:00
|
|
|
reducedPattern.scale(0.03, interfaceNodeIndex);
|
2021-02-18 14:33:31 +01:00
|
|
|
|
|
|
|
// Set the optization settings
|
2021-11-09 21:13:15 +01:00
|
|
|
ReducedPatternOptimization::xRange beamE{"E", 0.001, 1000};
|
2021-04-08 19:55:56 +02:00
|
|
|
ReducedPatternOptimization::xRange beamA{"A", 0.001, 1000};
|
2021-11-09 21:13:15 +01:00
|
|
|
ReducedPatternOptimization::xRange beamI{"I", 0.001, 1000};
|
2021-04-08 19:55:56 +02:00
|
|
|
ReducedPatternOptimization::xRange beamI2{"I2", 0.001, 1000};
|
|
|
|
ReducedPatternOptimization::xRange beamI3{"I3", 0.001, 1000};
|
|
|
|
ReducedPatternOptimization::xRange beamJ{"J", 0.001, 1000};
|
2021-12-03 11:20:37 +01:00
|
|
|
ReducedPatternOptimization::xRange innerHexagonSize{"R", 0.05, 0.95};
|
|
|
|
ReducedPatternOptimization::xRange innerHexagonAngle{"Theta", -30.0, 30.0};
|
2021-04-08 19:55:56 +02:00
|
|
|
ReducedPatternOptimization::Settings settings_optimization;
|
2021-12-05 12:53:13 +01:00
|
|
|
settings_optimization.parameterRanges
|
|
|
|
= {beamE, beamA, beamI2, beamI3, beamJ, innerHexagonSize, innerHexagonAngle};
|
2021-02-25 09:09:38 +01:00
|
|
|
const bool input_numberOfFunctionCallsDefined = argc >= 4;
|
2021-11-09 21:13:15 +01:00
|
|
|
settings_optimization.numberOfFunctionCalls = input_numberOfFunctionCallsDefined
|
|
|
|
? std::atoi(argv[3])
|
|
|
|
: 100;
|
2021-03-26 10:58:13 +01:00
|
|
|
settings_optimization.normalizationStrategy
|
2021-04-08 19:55:56 +02:00
|
|
|
= ReducedPatternOptimization::Settings::NormalizationStrategy::Epsilon;
|
2021-11-15 10:28:12 +01:00
|
|
|
settings_optimization.splitGeometryMaterialOptimization = false;
|
2021-12-03 11:20:37 +01:00
|
|
|
settings_optimization.translationNormalizationParameter = 3e-4;
|
2021-11-08 09:47:35 +01:00
|
|
|
settings_optimization.rotationNormalizationParameter = vcg::math::ToRad(3.0);
|
|
|
|
// settings_optimization.translationNormalizationParameter = 1e-15;
|
|
|
|
// settings_optimization.rotationNormalizationParameter = vcg::math::ToRad(1e-15);
|
2021-12-03 11:20:37 +01:00
|
|
|
settings_optimization.solverAccuracy = 1e-3;
|
|
|
|
// settings_optimization.solverAccuracy = 1e-1;
|
2021-04-16 10:41:40 +02:00
|
|
|
settings_optimization.objectiveWeights.translational = std::atof(argv[4]);
|
|
|
|
settings_optimization.objectiveWeights.rotational = 2 - std::atof(argv[4]);
|
2021-02-22 10:28:01 +01:00
|
|
|
|
2021-11-08 09:47:35 +01:00
|
|
|
std::string xConcatNames;
|
2021-12-05 12:53:13 +01:00
|
|
|
for (const auto &x : settings_optimization.parameterRanges) {
|
2021-11-08 09:47:35 +01:00
|
|
|
xConcatNames.append(x.label + "_");
|
|
|
|
}
|
|
|
|
xConcatNames.pop_back();
|
|
|
|
|
|
|
|
// Optimize pairthere
|
2021-12-03 11:20:37 +01:00
|
|
|
const std::string pairName = fullPattern.getLabel(); // + "@" + reducedPattern.getLabel();
|
2021-04-30 11:47:50 +02:00
|
|
|
const std::string optimizationName = pairName + "("
|
|
|
|
+ std::to_string(settings_optimization.numberOfFunctionCalls)
|
|
|
|
+ "_"
|
|
|
|
+ to_string_with_precision(
|
|
|
|
settings_optimization.objectiveWeights.translational)
|
2021-11-08 09:47:35 +01:00
|
|
|
+ ")" + "_" + xConcatNames;
|
2021-04-30 11:47:50 +02:00
|
|
|
const bool input_resultDirectoryDefined = argc >= 6;
|
|
|
|
const std::string optimizationResultsDirectory = input_resultDirectoryDefined
|
|
|
|
? argv[5]
|
|
|
|
: std::filesystem::current_path().append(
|
2021-05-03 18:13:52 +02:00
|
|
|
"OptimizationResults").string();
|
2021-04-30 11:47:50 +02:00
|
|
|
std::string resultsOutputDir;
|
|
|
|
bool optimizationResultFolderExists = false;
|
|
|
|
const std::filesystem::path crashedJobsDirPath(std::filesystem::path(optimizationResultsDirectory)
|
|
|
|
.append("CrashedJobs")
|
|
|
|
.append(optimizationName));
|
2021-05-24 13:18:36 +02:00
|
|
|
if (std::filesystem::exists(crashedJobsDirPath)) {
|
|
|
|
resultsOutputDir = crashedJobsDirPath.string();
|
|
|
|
optimizationResultFolderExists = true;
|
|
|
|
}
|
2021-04-30 11:47:50 +02:00
|
|
|
const std::filesystem::path convergedJobsDirPath(
|
|
|
|
std::filesystem::path(optimizationResultsDirectory)
|
|
|
|
.append("ConvergedJobs")
|
|
|
|
.append(optimizationName));
|
2021-05-24 13:18:36 +02:00
|
|
|
if (std::filesystem::exists(convergedJobsDirPath)) {
|
|
|
|
resultsOutputDir = convergedJobsDirPath.string();
|
|
|
|
optimizationResultFolderExists = true;
|
|
|
|
}
|
2021-04-30 11:47:50 +02:00
|
|
|
|
|
|
|
ReducedPatternOptimization::Results optimizationResults;
|
2021-11-08 09:47:35 +01:00
|
|
|
constexpr bool shouldReoptimize = true;
|
|
|
|
bool optimizationAlreadyComputed = false;
|
|
|
|
if (!shouldReoptimize && optimizationResultFolderExists) {
|
|
|
|
const bool resultsWereSuccessfullyLoaded = optimizationResults.load(resultsOutputDir);
|
|
|
|
if (resultsWereSuccessfullyLoaded && optimizationResults.settings == settings_optimization) {
|
|
|
|
optimizationAlreadyComputed = true;
|
|
|
|
}
|
|
|
|
}
|
2021-02-19 16:32:15 +01:00
|
|
|
|
2021-05-24 13:18:36 +02:00
|
|
|
if (!optimizationAlreadyComputed) {
|
2021-06-24 08:55:04 +02:00
|
|
|
auto start = std::chrono::system_clock::now();
|
2021-05-24 13:18:36 +02:00
|
|
|
const std::vector<size_t> numberOfNodesPerSlot{1, 0, 0, 2, 1, 2, 1};
|
|
|
|
assert(interfaceNodeIndex == numberOfNodesPerSlot[0] + numberOfNodesPerSlot[3]);
|
|
|
|
ReducedModelOptimizer optimizer(numberOfNodesPerSlot);
|
2021-12-05 12:53:13 +01:00
|
|
|
optimizer.initializePatterns(fullPattern, reducedPattern, settings_optimization.parameterRanges);
|
2021-05-24 13:18:36 +02:00
|
|
|
optimizer.optimize(settings_optimization, optimizationResults);
|
|
|
|
optimizationResults.label = optimizationName;
|
|
|
|
optimizationResults.baseTriangleFullPattern.copy(fullPattern);
|
|
|
|
optimizationResults.settings = settings_optimization;
|
2021-06-24 08:55:04 +02:00
|
|
|
auto end = std::chrono::system_clock::now();
|
|
|
|
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
|
|
|
optimizationResults.time = elapsed.count() / 1000.0;
|
2021-02-22 10:28:01 +01:00
|
|
|
|
2021-12-15 14:21:21 +01:00
|
|
|
if (!optimizationResults.wasSuccessful) {
|
2021-12-03 11:20:37 +01:00
|
|
|
// resultsOutputDir = crashedJobsDirPath.string();
|
2021-11-15 19:16:03 +01:00
|
|
|
return 1;
|
2021-06-30 09:21:32 +02:00
|
|
|
}
|
2021-12-15 14:21:21 +01:00
|
|
|
resultsOutputDir = convergedJobsDirPath.string();
|
2021-07-20 15:30:41 +02:00
|
|
|
optimizationResults.save(resultsOutputDir, true);
|
2021-12-15 14:21:21 +01:00
|
|
|
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 << std::to_string(fullPattern.EN()) + "#" + pairName;
|
|
|
|
optimizationResults.writeResultsTo(csvVector);
|
|
|
|
settings_optimization.writeSettingsTo(csv_results);
|
|
|
|
csv_results << endrow;
|
|
|
|
csv_resultsLocalFile << endrow;
|
2021-05-24 13:18:36 +02:00
|
|
|
}
|
2021-04-30 11:47:50 +02:00
|
|
|
|
2021-10-14 13:58:34 +02:00
|
|
|
#ifdef POLYSCOPE_DEFINED
|
|
|
|
// optimizationResults.saveMeshFiles();
|
|
|
|
optimizationResults.draw();
|
|
|
|
#endif
|
2021-04-08 19:55:56 +02:00
|
|
|
|
2021-05-24 13:18:36 +02:00
|
|
|
return 0;
|
2020-11-23 10:06:45 +01:00
|
|
|
}
|