Added normalization

This commit is contained in:
iasonmanolas 2021-02-14 14:27:14 +02:00
parent 135c9fd975
commit 3a5ef38645
3 changed files with 169 additions and 115 deletions

View File

@ -16,6 +16,137 @@
#include <string>
#include <vcg/complex/algorithms/update/position.h>
void exportOptimizationResults(const ReducedModelOptimizer::Results& optimizationResults,const std::filesystem::path& optimizationDirectory,const ReducedModelOptimizer::Settings& settings_optimization,csvFile& statistics,const std::pair<FlatPattern *, FlatPattern *> patternPair) {
std::filesystem::path saveToPath(
std::filesystem::path(optimizationDirectory)
.append(patternPair.first->getLabel() + "@" +
patternPair.second->getLabel()));
std::filesystem::create_directories(std::filesystem::path(saveToPath));
optimizationResults.save(saveToPath.string());
statistics << patternPair.first->getLabel() + "@" +
patternPair.second->getLabel();
statistics << optimizationResults.objectiveValue;
for (const double &optimalX :
optimizationResults.x) {
statistics << optimalX;
}
for (int unusedXVarCounter = 0;
unusedXVarCounter <
settings_optimization.xRanges.size() -
optimizationResults.x.size();
unusedXVarCounter++) {
statistics << "-";
}
statistics << optimizationResults.time;
if (optimizationResults.numberOfSimulationCrashes == 0) {
statistics << "No crashes";
} else {
statistics << optimizationResults.numberOfSimulationCrashes;
}
statistics << endrow;
}
void optimizeTestSet(const std::vector<std::pair<FlatPattern *, FlatPattern *>> patternPairs,const ReducedModelOptimizer::Settings& settings_optimization,const std::string& saveTo) {
const std::string optimizationSettingsString =
settings_optimization.toString();
std::filesystem::path thisOptimizationDirectory(
std::filesystem::path(saveTo)
.append(optimizationSettingsString));
//.append("Results"));
std::filesystem::create_directories(thisOptimizationDirectory);
std::cout << optimizationSettingsString << std::endl;
csvFile csv_settings(std::filesystem::path(thisOptimizationDirectory)
.append("settings.csv")
.string(),
true);
settings_optimization.writeTo(csv_settings);
csvFile statistics(std::filesystem::path(thisOptimizationDirectory)
.append("statistics.csv")
.string(),
false);
// Write header to csv
statistics << "FullPattern@ReducedPattern"
<< "Obj value";
for (const ReducedModelOptimizer::xRange &range :
settings_optimization.xRanges) {
statistics << range.label;
}
statistics << "Time(s)";
statistics << "#Crashes";
statistics << endrow;
double totalError = 0;
int totalNumberOfSimulationCrashes = 0;
std::vector<ReducedModelOptimizer::Results> optimizationResults_testSet(
patternPairs.size());
auto start = std::chrono::high_resolution_clock::now();
int pairsOptimized=0;
#pragma omp parallel for
for (int patternPairIndex = 0; patternPairIndex < patternPairs.size();
patternPairIndex++) {
const std::pair<FlatPattern*, FlatPattern*>&
patternPair = patternPairs[patternPairIndex];
const std::vector<size_t> numberOfNodesPerSlot{ 1, 0, 0, 2, 1, 2, 1 };
ReducedModelOptimizer optimizer(numberOfNodesPerSlot);
optimizer.initializePatterns(*patternPair.first,
*patternPair.second, {});
ReducedModelOptimizer::Results optimizationResults =
optimizer.optimize(settings_optimization);
totalError += optimizationResults.objectiveValue;
optimizationResults_testSet[patternPairIndex] = optimizationResults;
totalNumberOfSimulationCrashes +=
optimizationResults.numberOfSimulationCrashes;
std::cout << "Optimized " << ++pairsOptimized << "/" << patternPairs.size() << std::endl;
}
for (int patternPairIndex = 0; patternPairIndex < patternPairs.size();
patternPairIndex++) {
const std::pair<FlatPattern*, FlatPattern*>&
patternPair = patternPairs[patternPairIndex];
const ReducedModelOptimizer::Results optimizationResults=optimizationResults_testSet[patternPairIndex];
//Save results
std::filesystem::path saveToPath(
std::filesystem::path(thisOptimizationDirectory)
.append(patternPair.first->getLabel() + "@" +
patternPair.second->getLabel()));
std::filesystem::create_directories(std::filesystem::path(saveToPath));
optimizationResults.save(saveToPath.string());
//Save statistics
statistics << patternPair.first->getLabel() + "@" +
patternPair.second->getLabel();
statistics << optimizationResults.objectiveValue;
for (const double& optimalX :
optimizationResults.x) {
statistics << optimalX;
}
for (int unusedXVarCounter = 0;
unusedXVarCounter <
settings_optimization.xRanges.size() -
optimizationResults.x.size();
unusedXVarCounter++) {
statistics << "-";
}
statistics << optimizationResults.time;
if (optimizationResults.numberOfSimulationCrashes == 0) {
statistics << "No crashes";
}
else {
statistics << optimizationResults.numberOfSimulationCrashes;
}
statistics << endrow;
}
}
int main(int argc, char *argv[]) {
// Create reduced models
// FormFinder::runUnitTests();
@ -45,7 +176,7 @@ int main(int argc, char *argv[]) {
ReducedModelOptimizer::xRange beamWidth{"B", 0.5, 1.5};
ReducedModelOptimizer::xRange beamDimensionsRatio{"bOverh", 0.7, 1.3};
ReducedModelOptimizer::xRange beamE{"E", 0.1, 1.9};
ReducedModelOptimizer::xRange innerHexagonSize{"HexagonSize", 0.1, 0.9};
ReducedModelOptimizer::xRange innerHexagonSize{"HS", 0.1, 0.9};
// Test set of full patterns
std::string fullPatternsTestSetDirectory = "TestSet";
if (!std::filesystem::exists(
@ -72,15 +203,15 @@ int main(int argc, char *argv[]) {
FlatPattern fullPattern(filepathString);
fullPattern.setLabel(filepath.stem().string());
fullPattern.scale(0.03);
//for (int reducedPatternIndex = 0;
// reducedPatternIndex < reducedModels.size(); reducedPatternIndex++) {
for (int reducedPatternIndex = 0;
reducedPatternIndex < reducedModels.size(); reducedPatternIndex++) {
FlatPattern *pFullPattern = new FlatPattern();
pFullPattern->copy(fullPattern);
FlatPattern *pReducedPattern = new FlatPattern();
//pReducedPattern->copy(*reducedModels[reducedPatternIndex]);
pReducedPattern->copy(*reducedModels[0]);
pReducedPattern->copy(*reducedModels[reducedPatternIndex]);
//pReducedPattern->copy(*reducedModels[0]);
patternPairs.push_back(std::make_pair(pFullPattern, pReducedPattern));
//}
}
}
// for (double rangeOffset = 0.15; rangeOffset <= 0.95; rangeOffset += 0.05)
@ -92,107 +223,23 @@ int main(int argc, char *argv[]) {
// for (settings_optimization.numberOfFunctionCalls = 100;
// settings_optimization.numberOfFunctionCalls < 5000;
// settings_optimization.numberOfFunctionCalls += 100) {
settings_optimization.numberOfFunctionCalls = 100;
const std::string optimizationSettingsString =
settings_optimization.toString();
std::string optimiziationResultsDirectory = "../OptimizationResults";
// if (argc == 1) {
// optimiziationResultsDirectory = argv[0];
//}
std::filesystem::path thisOptimizationDirectory(
std::filesystem::path(optimiziationResultsDirectory)
.append(optimizationSettingsString));
std::filesystem::create_directories(thisOptimizationDirectory);
std::cout << optimizationSettingsString << std::endl;
csvFile csv_settings(std::filesystem::path(thisOptimizationDirectory)
.append("settings.csv")
.string(),
true);
settings_optimization.writeTo(csv_settings);
double totalError = 0;
int totalNumberOfSimulationCrashes = 0;
std::vector<ReducedModelOptimizer::Results> optimizationResults_testSet(
patternPairs.size());
auto start = std::chrono::high_resolution_clock::now();
//#pragma omp parallel for
for (int patternPairIndex = 0; patternPairIndex < patternPairs.size();
patternPairIndex++) {
const std::vector<size_t> numberOfNodesPerSlot{1, 0, 0, 2, 1, 2, 1};
ReducedModelOptimizer optimizer(numberOfNodesPerSlot);
optimizer.initializePatterns(*patternPairs[patternPairIndex].first,
*patternPairs[patternPairIndex].second, {});
ReducedModelOptimizer::Results optimizationResults =
optimizer.optimize(settings_optimization, {ReducedModelOptimizer::SimulationScenario::Dome});
totalError += optimizationResults.objectiveValue;
optimizationResults_testSet[patternPairIndex] = optimizationResults;
totalNumberOfSimulationCrashes +=
optimizationResults.numberOfSimulationCrashes;
optimizationResults.draw();
std::string optimiziationResultsDirectory = "OptimizationResults";
if (argc < 2) {
settings_optimization.numberOfFunctionCalls =100;
}
auto end = std::chrono::high_resolution_clock::now();
auto runtime_ms =
std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
for (int patternPairIndex = 0; patternPairIndex < patternPairs.size();
patternPairIndex++) {
std::filesystem::path saveToPath(
std::filesystem::path(thisOptimizationDirectory)
.append(patternPairs[patternPairIndex].first->getLabel() + "@" +
patternPairs[patternPairIndex].second->getLabel()));
std::filesystem::create_directories(std::filesystem::path(saveToPath));
optimizationResults_testSet[patternPairIndex].save(saveToPath.string());
else if (argc == 2) {
settings_optimization.numberOfFunctionCalls =std::atoi(argv[1]);
}
csvFile statistics(std::filesystem::path(thisOptimizationDirectory)
.append("statistics.csv")
.string(),
false);
else if (argc == 3) {
settings_optimization.numberOfFunctionCalls =std::atoi(argv[1]);
optimiziationResultsDirectory = argv[2];
}
settings_optimization.normalizeObjectiveValue =false;
optimizeTestSet(patternPairs,settings_optimization,optimiziationResultsDirectory);
settings_optimization.normalizeObjectiveValue = true;
optimizeTestSet(patternPairs,settings_optimization,optimiziationResultsDirectory);
// Write header to csv
statistics << "FullPattern@ReducedPattern"
<< "Obj value";
for (const ReducedModelOptimizer::xRange &range :
settings_optimization.xRanges) {
statistics << range.label;
}
statistics << "Time(s)";
statistics << "#Crashes";
statistics << endrow;
// Write data
for (int patternPairIndex = 0; patternPairIndex < patternPairs.size();
patternPairIndex++) {
statistics << patternPairs[patternPairIndex].first->getLabel() + "@" +
patternPairs[patternPairIndex].second->getLabel();
statistics << optimizationResults_testSet[patternPairIndex].objectiveValue;
for (const double &optimalX :
optimizationResults_testSet[patternPairIndex].x) {
statistics << optimalX;
}
for (int unusedXVarCounter = 0;
unusedXVarCounter <
settings_optimization.xRanges.size() -
optimizationResults_testSet[patternPairIndex].x.size();
unusedXVarCounter++) {
statistics << "-";
}
statistics << optimizationResults_testSet[patternPairIndex].time;
if (totalNumberOfSimulationCrashes == 0) {
statistics << "No crashes";
} else {
statistics << totalNumberOfSimulationCrashes;
}
statistics << endrow;
}
statistics << endrow;
// }
for (auto patternPair : patternPairs) {
for (auto &patternPair : patternPairs) {
delete patternPair.first;
delete patternPair.second;
}

View File

@ -33,6 +33,7 @@ struct GlobalOptimizationVariables {
int numOfSimulationCrashes{false};
int numberOfFunctionCalls{0};
int numberOfOptimizationParameters{3};
ReducedModelOptimizer::Settings optimizationSettings;
};
// static GlobalOptimizationVariables global;
@ -127,6 +128,7 @@ double ReducedModelOptimizer::computeError(
const std::vector<Vector6d> &fullPatternDisplacements,const double& interfaceDisplacementsNormSum,
const std::unordered_map<ReducedPatternVertexIndex, FullPatternVertexIndex>
&reducedToFullInterfaceViMap) {
auto &global = tls[omp_get_thread_num()];
double error = 0;
for (const auto reducedFullViPair : reducedToFullInterfaceViMap) {
VertexIndex reducedModelVi = reducedFullViPair.first;
@ -153,8 +155,10 @@ double ReducedModelOptimizer::computeError(
error += errorVector.norm();
}
if (global.optimizationSettings.normalizeObjectiveValue) {
return error/std::max(interfaceDisplacementsNormSum,0.00003);
//return error;
}
return error;
}
void updateMesh(long n, const double *x) {
@ -322,10 +326,10 @@ double ReducedModelOptimizer::objective(long n, const double *x) {
global.minY = error;
global.minX.assign(x, x + n);
}
if (++global.numberOfFunctionCalls % 100 == 0) {
std::cout << "Number of function calls:" << global.numberOfFunctionCalls
<< std::endl;
}
// if (++global.numberOfFunctionCalls %100== 0) {
//std::cout << "Number of function calls:" << global.numberOfFunctionCalls
// << std::endl;
//}
// compute error and return it
global.gObjectiveValueHistory.push_back(error);
@ -1047,7 +1051,7 @@ void ReducedModelOptimizer::visualizeResults(
SimulationResults fullModelResults =
simulator.executeSimulation(pFullPatternSimulationJob);
fullModelResults.registerForDrawing();
fullModelResults.saveDeformedModel();
//fullModelResults.saveDeformedModel();
const std::shared_ptr<SimulationJob> &pReducedPatternSimulationJob =
reducedPatternSimulationJobs[simulationScenarioIndex];
SimulationResults reducedModelResults =
@ -1085,7 +1089,7 @@ void ReducedModelOptimizer::visualizeResults(
}
ReducedModelOptimizer::Results ReducedModelOptimizer::optimize(
const Settings &xRanges,
const Settings &optimizationSettings,
const std::vector<SimulationScenario> &simulationScenarios) {
auto &global = tls[omp_get_thread_num()];
@ -1107,6 +1111,7 @@ ReducedModelOptimizer::Results ReducedModelOptimizer::optimize(
global.minY = std::numeric_limits<double>::max();
global.numOfSimulationCrashes = 0;
global.numberOfFunctionCalls = 0;
global.optimizationSettings = optimizationSettings;
// polyscope::removeAllStructures();
FormFinder::Settings settings;
@ -1138,7 +1143,7 @@ ReducedModelOptimizer::Results ReducedModelOptimizer::optimize(
global.reducedPatternSimulationJobs[simulationScenarioIndex] =
std::make_shared<SimulationJob>(reducedPatternSimulationJob);
}
Results optResults = runOptimization(xRanges);
Results optResults = runOptimization(optimizationSettings);
for (int simulationScenarioIndex : global.simulationScenarioIndices) {
optResults.fullPatternSimulationJobs.push_back(
simulationJobs[simulationScenarioIndex]);
@ -1149,7 +1154,7 @@ ReducedModelOptimizer::Results ReducedModelOptimizer::optimize(
// optResults.draw();
// visualizeResults(simulationJobs, global.simulationScenarioIndices);
visualizeResults(simulationJobs, global.reducedPatternSimulationJobs,
global.simulationScenarioIndices,global.reducedToFullInterfaceViMap);
//visualizeResults(simulationJobs, global.reducedPatternSimulationJobs,
// global.simulationScenarioIndices,global.reducedToFullInterfaceViMap);
return optResults;
}

View File

@ -59,7 +59,7 @@ public:
settingsString += xRangesString;
}
settingsString += "FuncCalls=" + std::to_string(numberOfFunctionCalls) +
" Accuracy=" + std::to_string(solutionAccuracy);
" Accuracy=" + std::to_string(solutionAccuracy)+" Norm="+(normalizeObjectiveValue ? "yes" : "no");
return settingsString;
}
@ -74,6 +74,7 @@ public:
}
csv << "Function Calls";
csv << "Solution Accuracy";
csv << "Normalize obj value";
csv << endrow;
if (!xRanges.empty()) {
@ -84,6 +85,7 @@ public:
}
csv << numberOfFunctionCalls;
csv << solutionAccuracy;
csv <<(normalizeObjectiveValue ? "yes" : "no");
csv << endrow;
}
};