ReducedModelOptimization/src/main.cpp

204 lines
8.5 KiB
C++
Raw Normal View History

2020-11-23 10:06:45 +01:00
#include "beamformfinder.hpp"
2021-01-22 15:39:36 +01:00
#include "csvfile.hpp"
2020-11-23 10:06:45 +01:00
#include "edgemesh.hpp"
#include "flatpattern.hpp"
#include "polyscope/curve_network.h"
#include "polyscope/point_cloud.h"
#include "polyscope/polyscope.h"
#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>
#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>
2020-11-23 10:06:45 +01:00
int main(int argc, char *argv[]) {
// Create reduced models
2020-12-16 20:31:58 +01:00
// FormFinder::runUnitTests();
const std::vector<size_t> numberOfNodesPerSlot{1, 0, 0, 2, 1, 2, 1};
std::vector<vcg::Point2i> singleBarReducedModelEdges{vcg::Point2i(0, 3)};
FlatPattern singleBarReducedModel(numberOfNodesPerSlot,
singleBarReducedModelEdges);
singleBarReducedModel.setLabel("SingleBar_reduced");
singleBarReducedModel.scale(0.03);
std::vector<vcg::Point2i> CWreducedModelEdges{vcg::Point2i(1, 5),
vcg::Point2i(3, 1)};
FlatPattern CWReducedModel(numberOfNodesPerSlot, CWreducedModelEdges);
CWReducedModel.setLabel("CW_reduced");
CWReducedModel.scale(0.03);
std::vector<vcg::Point2i> CCWreducedModelEdges{vcg::Point2i(1, 5),
vcg::Point2i(3, 5)};
FlatPattern CCWReducedModel(numberOfNodesPerSlot, CCWreducedModelEdges);
CCWReducedModel.setLabel("CCW_reduced");
CCWReducedModel.scale(0.03);
std::vector<FlatPattern *> reducedModels{&singleBarReducedModel,
&CWReducedModel, &CCWReducedModel};
// Define the ranges that the optimizer will use
ReducedModelOptimizer::xRange beamWidth{"B", 0.5, 1.5};
ReducedModelOptimizer::xRange beamDimensionsRatio{"bOverh", 0.7, 1.3};
ReducedModelOptimizer::xRange beamE{"E", 0.1, 1.9};
2021-02-09 19:55:44 +01:00
ReducedModelOptimizer::xRange innerHexagonSize{"HexagonSize", 0.1, 0.9};
// Test set of full patterns
std::string fullPatternsTestSetDirectory = "../TestSet";
2021-02-09 20:43:49 +01:00
if (!std::filesystem::exists(
std::filesystem::path(fullPatternsTestSetDirectory))) {
std::cerr << "Full pattern directory does not exist: "
<< fullPatternsTestSetDirectory << std::endl;
return 1;
}
// "/home/iason/Documents/PhD/Research/Approximating shapes with flat "
// "patterns/Pattern_enumerator/Results/1v_0v_2e_1e_1c_6fan/3/Valid";
std::vector<std::pair<FlatPattern *, FlatPattern *>> patternPairs;
for (const auto &entry :
filesystem::directory_iterator(fullPatternsTestSetDirectory)) {
const auto filepath =
// std::filesystem::path(fullPatternsTestSetDirectory).append("305.ply");
entry.path();
const std::string filepathString = filepath.string();
const std::string tiledSuffix = "_tiled.ply";
if (filepathString.compare(filepathString.size() - tiledSuffix.size(),
tiledSuffix.size(), tiledSuffix) == 0) {
continue;
}
FlatPattern *pFullPattern = new FlatPattern(filepathString);
pFullPattern->setLabel(filepath.stem().string());
pFullPattern->scale(0.03);
FlatPattern *pReducedPattern = new FlatPattern();
2021-02-09 19:55:44 +01:00
pReducedPattern->copy(*reducedModels[1]);
patternPairs.push_back(std::make_pair(pFullPattern, pReducedPattern));
}
2021-01-29 18:07:13 +01:00
// for (double rangeOffset = 0.15; rangeOffset <= 0.95; rangeOffset += 0.05)
// {
ReducedModelOptimizer::Settings settings;
for (settings.maxSimulations = 100; settings.maxSimulations < 5000;
settings.maxSimulations += 100) {
2021-01-22 15:39:36 +01:00
std::string xRangesString = beamWidth.toString() + " " +
beamDimensionsRatio.toString() + " " +
beamE.toString();
std::cout << xRangesString << std::endl;
settings.xRanges = {beamWidth, beamDimensionsRatio, beamE,
innerHexagonSize};
// std::filesystem::path thisOptimizationDirectory(
// std::filesystem::path("../OptimizationResults").append(xRangesString));
// std::filesystem::create_directories(thisOptimizationDirectory);
2021-01-29 18:07:13 +01:00
// csvfile thisOptimizationStatistics(
// std::filesystem::path(thisOptimizationDirectory)
// .append("statistics.csv")
// .string(),
// true);
2021-01-22 15:39:36 +01:00
double totalError = 0;
int totalNumberOfSimulationCrashes = 0;
2021-01-29 18:07:13 +01:00
std::vector<std::pair<std::string, ReducedModelOptimizer::Results>>
resultsPerPattern(patternPairs.size());
2021-01-29 18:07:13 +01:00
auto start = std::chrono::high_resolution_clock::now();
2021-01-31 22:36:46 +01:00
//#pragma omp parallel for
for (int patternPairIndex = 0; patternPairIndex < patternPairs.size();
patternPairIndex++) {
// const auto filepathString = filepath.string();
2021-01-22 15:39:36 +01:00
// Use only the base triangle version
// std::cout << "Full pattern:" << filepathString << std::endl;
// for (int reducedPatternIndex = 0;
// reducedPatternIndex < reducedModels.size();
// reducedPatternIndex++) {
// FlatPattern *pReducedModel =
// reducedModels[reducedPatternIndex];
std::unordered_set<size_t> optimizationExcludedEi;
// if (pReducedModel !=
// reducedModels[0]) { // assumes that the singleBar reduced model
// // is
// // the
// // first in the reducedModels vector
// optimizationExcludedEi.insert(0);
// }
// FlatPattern cp;
// cp.copy(*reducedModels[0]);
const std::vector<size_t> numberOfNodesPerSlot{1, 0, 0, 2, 1, 2, 1};
ReducedModelOptimizer optimizer(numberOfNodesPerSlot);
optimizer.initializePatterns(*patternPairs[patternPairIndex].first,
*patternPairs[patternPairIndex].second,
2021-02-09 20:43:49 +01:00
optimizationExcludedEi);
2021-01-22 15:39:36 +01:00
// optimizer.optimize({ReducedModelOptimizer::Axial});
ReducedModelOptimizer::Results optimizationResults =
optimizer.optimize(settings);
totalError += optimizationResults.objectiveValue;
resultsPerPattern[patternPairIndex] =
2021-02-09 20:43:49 +01:00
std::make_pair(patternPairs[patternPairIndex].first->getLabel(),
optimizationResults);
2021-01-22 15:39:36 +01:00
totalNumberOfSimulationCrashes +=
optimizationResults.numberOfSimulationCrashes;
// std::cout << "Have optimized " << ++patternsOptimized << "/"
// << static_cast<int>(
// std::distance(std::filesystem::directory_iterator(
// fullPatternsTestSetDirectory),
// std::filesystem::directory_iterator()))
// << " patterns." << std::endl;
2021-01-22 15:39:36 +01:00
// }
}
2021-01-29 18:07:13 +01:00
auto end = std::chrono::high_resolution_clock::now();
auto runtime_ms =
std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
2020-11-23 10:06:45 +01:00
// for (int patternPairIndex = 0; patternPairIndex < patternPairs.size();
// patternPairIndex++) {
// std::filesystem::path
// saveToPath(std::filesystem::path("../OptimizationResults")
// .append(resultsPerPattern[patternPairIndex].first));
// std::filesystem::create_directory(std::filesystem::path(saveToPath));
// resultsPerPattern[patternPairIndex].second.save(saveToPath);
// }
2021-02-09 20:43:49 +01:00
if (!std::filesystem::exists(
std::filesystem::path("OptimizationResults/"))) {
std::filesystem::create_directory(
std::filesystem::path("OptimizationResults"));
}
csvfile statistics(std::filesystem::path("OptimizationResults")
2021-01-22 15:39:36 +01:00
.append("statistics.csv")
.string(),
false);
for (const auto &range : settings.xRanges) {
statistics << range.min << range.max;
}
statistics << settings.maxSimulations;
if (totalNumberOfSimulationCrashes == 0) {
statistics << "No crashes";
} else {
statistics << totalNumberOfSimulationCrashes;
}
2020-12-09 16:58:48 +01:00
2021-01-29 18:07:13 +01:00
statistics << totalError;
for (const auto &patternObjectiveValue : resultsPerPattern) {
2021-02-09 19:01:29 +01:00
statistics << patternObjectiveValue.first;
2021-01-29 18:07:13 +01:00
statistics << patternObjectiveValue.second.objectiveValue;
for (const double &optimalX : patternObjectiveValue.second.x) {
statistics << optimalX;
}
}
statistics << runtime_ms.count() / 1000.0;
2021-01-29 18:07:13 +01:00
statistics << endrow;
2021-01-22 15:39:36 +01:00
}
for (auto patternPair : patternPairs) {
delete patternPair.first;
delete patternPair.second;
}
2020-11-23 10:06:45 +01:00
return 0;
}