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>
|
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>
|
|
|
|
|
2020-11-23 10:06:45 +01:00
|
|
|
int main(int argc, char *argv[]) {
|
2021-01-04 13:12:25 +01:00
|
|
|
|
2020-11-27 11:45:20 +01:00
|
|
|
// Create reduced models
|
2020-12-16 20:31:58 +01:00
|
|
|
// FormFinder::runUnitTests();
|
2020-11-27 11:45:20 +01:00
|
|
|
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);
|
2021-02-04 15:05:48 +01:00
|
|
|
singleBarReducedModel.setLabel("SingleBar_reduced");
|
2020-12-22 17:47:29 +01:00
|
|
|
singleBarReducedModel.scale(0.03);
|
2020-11-27 11:45:20 +01:00
|
|
|
|
2020-12-14 10:07:43 +01:00
|
|
|
std::vector<vcg::Point2i> CWreducedModelEdges{vcg::Point2i(1, 5),
|
|
|
|
vcg::Point2i(3, 1)};
|
2020-12-22 17:47:29 +01:00
|
|
|
FlatPattern CWReducedModel(numberOfNodesPerSlot, CWreducedModelEdges);
|
2021-02-04 15:05:48 +01:00
|
|
|
CWReducedModel.setLabel("CW_reduced");
|
2020-12-22 17:47:29 +01:00
|
|
|
CWReducedModel.scale(0.03);
|
|
|
|
|
|
|
|
std::vector<vcg::Point2i> CCWreducedModelEdges{vcg::Point2i(1, 5),
|
|
|
|
vcg::Point2i(3, 5)};
|
|
|
|
FlatPattern CCWReducedModel(numberOfNodesPerSlot, CCWreducedModelEdges);
|
2021-02-04 15:05:48 +01:00
|
|
|
CCWReducedModel.setLabel("CCW_reduced");
|
2020-12-22 17:47:29 +01:00
|
|
|
CCWReducedModel.scale(0.03);
|
2020-11-27 11:45:20 +01:00
|
|
|
|
2020-12-14 10:07:43 +01:00
|
|
|
std::vector<FlatPattern *> reducedModels{&singleBarReducedModel,
|
|
|
|
&CWReducedModel, &CCWReducedModel};
|
2020-11-27 11:45:20 +01:00
|
|
|
|
2021-02-04 15:05:48 +01:00
|
|
|
// 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-01 15:10:24 +01:00
|
|
|
// Test set of full patterns
|
|
|
|
std::string fullPatternsTestSetDirectory = "../TestSet";
|
|
|
|
// "/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();
|
|
|
|
pReducedPattern->copy(*reducedModels[0]);
|
|
|
|
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;
|
2021-02-04 15:05:48 +01:00
|
|
|
for (settings.maxSimulations = 100; settings.maxSimulations < 3500;
|
|
|
|
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};
|
2021-02-04 15:05:48 +01:00
|
|
|
// 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>>
|
2021-02-01 15:10:24 +01:00
|
|
|
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
|
|
|
|
2021-02-04 15:05:48 +01:00
|
|
|
//#pragma omp parallel for
|
2021-02-01 15:10:24 +01:00
|
|
|
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]);
|
2021-02-01 15:10:24 +01:00
|
|
|
const std::vector<size_t> numberOfNodesPerSlot{1, 0, 0, 2, 1, 2, 1};
|
|
|
|
ReducedModelOptimizer optimizer(numberOfNodesPerSlot);
|
2021-02-04 15:05:48 +01:00
|
|
|
optimizer.initializePatterns(*patternPairs[patternPairIndex].first,
|
2021-02-01 15:10:24 +01:00
|
|
|
*patternPairs[patternPairIndex].second,
|
2021-02-04 15:05:48 +01:00
|
|
|
optimizationSpaceSamplingDisplacements,
|
2021-01-22 15:39:36 +01:00
|
|
|
optimizationExcludedEi);
|
|
|
|
// optimizer.optimize({ReducedModelOptimizer::Axial});
|
|
|
|
ReducedModelOptimizer::Results optimizationResults =
|
|
|
|
optimizer.optimize(settings);
|
|
|
|
|
|
|
|
totalError += optimizationResults.objectiveValue;
|
2021-02-01 15:10:24 +01:00
|
|
|
resultsPerPattern[patternPairIndex] =
|
|
|
|
std::make_pair(pPattern->getLabel(), optimizationResults);
|
2021-01-22 15:39:36 +01:00
|
|
|
totalNumberOfSimulationCrashes +=
|
|
|
|
optimizationResults.numberOfSimulationCrashes;
|
2021-02-01 15:10:24 +01:00
|
|
|
// 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
|
|
|
// }
|
2020-12-14 10:07:43 +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
|
|
|
|
2021-02-04 15:05:48 +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-01-22 15:39:36 +01:00
|
|
|
csvfile statistics(std::filesystem::path("../OptimizationResults")
|
|
|
|
.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) {
|
|
|
|
statistics << patternObjectiveValue.second.objectiveValue;
|
|
|
|
for (const double &optimalX : patternObjectiveValue.second.x) {
|
|
|
|
statistics << optimalX;
|
|
|
|
}
|
|
|
|
}
|
2021-02-04 15:05:48 +01:00
|
|
|
statistics << runtime_ms.count() / 1000.0;
|
2021-01-29 18:07:13 +01:00
|
|
|
|
|
|
|
statistics << endrow;
|
2021-01-22 15:39:36 +01:00
|
|
|
}
|
2021-02-04 15:05:48 +01:00
|
|
|
|
|
|
|
for (auto patternPair : patternPairs) {
|
|
|
|
delete patternPair.first;
|
|
|
|
delete patternPair.second;
|
|
|
|
}
|
2020-11-23 10:06:45 +01:00
|
|
|
return 0;
|
|
|
|
}
|