-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Utils::ExportBranchAndBoundTreeToCSV
- Loading branch information
Showing
9 changed files
with
167 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
lib/include/idol/optimizers/branch-and-bound/watchers/ExportBranchAndBoundTreeToCSV.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// | ||
// Created by henri on 27.10.23. | ||
// | ||
|
||
#ifndef IDOL_EXPORTBRANCHANDBOUNDTREETOCSV_H | ||
#define IDOL_EXPORTBRANCHANDBOUNDTREETOCSV_H | ||
|
||
|
||
#include "idol/optimizers/branch-and-bound/nodes/NodeVarInfo.h" | ||
#include "idol/optimizers/branch-and-bound/callbacks/BranchAndBoundCallbackFactory.h" | ||
#include "idol/optimizers/branch-and-bound/callbacks/BranchAndBoundCallback.h" | ||
|
||
namespace idol::Utils { | ||
class ExportBranchAndBoundTreeToCSV; | ||
} | ||
|
||
class idol::Utils::ExportBranchAndBoundTreeToCSV : public BranchAndBoundCallbackFactory<NodeVarInfo> { | ||
const std::string m_filename; | ||
public: | ||
explicit ExportBranchAndBoundTreeToCSV(std::string t_filename); | ||
|
||
BranchAndBoundCallback<NodeVarInfo> *operator()() override; | ||
|
||
BranchAndBoundCallbackFactory<NodeVarInfo> *clone() const override; | ||
|
||
class Strategy : public BranchAndBoundCallback<NodeVarInfo> { | ||
const std::string m_filename; | ||
protected: | ||
void operator()(CallbackEvent t_event) override; | ||
public: | ||
explicit Strategy(std::string t_filename); | ||
}; | ||
}; | ||
|
||
#endif //IDOL_EXPORTBRANCHANDBOUNDTREETOCSV_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
lib/src/optimizers/branch-and-bound/watchers/BranchAndBoundTree.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// | ||
// Created by henri on 27.10.23. | ||
// | ||
#include "idol/optimizers/branch-and-bound/watchers/ExportBranchAndBoundTreeToCSV.h" | ||
#include <fstream> | ||
|
||
idol::Utils::ExportBranchAndBoundTreeToCSV::ExportBranchAndBoundTreeToCSV(std::string t_filename) : m_filename(std::move(t_filename)) { | ||
|
||
} | ||
|
||
idol::BranchAndBoundCallbackFactory<idol::NodeVarInfo> *idol::Utils::ExportBranchAndBoundTreeToCSV::clone() const { | ||
return new ExportBranchAndBoundTreeToCSV(*this); | ||
} | ||
|
||
idol::BranchAndBoundCallback<idol::NodeVarInfo> *idol::Utils::ExportBranchAndBoundTreeToCSV::operator()() { | ||
return new Strategy(m_filename); | ||
} | ||
|
||
idol::Utils::ExportBranchAndBoundTreeToCSV::Strategy::Strategy(std::string t_filename) : m_filename(std::move(t_filename)) { | ||
|
||
std::ofstream file(m_filename); | ||
|
||
if (!file.is_open()) { | ||
throw Exception("Could not open destination file."); | ||
} | ||
|
||
file.close(); | ||
|
||
} | ||
|
||
void idol::Utils::ExportBranchAndBoundTreeToCSV::Strategy::operator()(idol::CallbackEvent t_event) { | ||
|
||
if (t_event != InvalidSolution && t_event != IncumbentSolution && t_event != PrunedSolution) { | ||
return; | ||
} | ||
|
||
const auto& side_effect_registry = this->side_effect_registry(); | ||
|
||
if (side_effect_registry.n_added_lazy_cuts > 0 || side_effect_registry.n_added_user_cuts > 0) { | ||
return; | ||
} | ||
|
||
std::ofstream file(m_filename, std::ios::app); | ||
|
||
if (!file.is_open()) { | ||
throw Exception("Could not open destination file."); | ||
} | ||
|
||
const auto& node = this->node(); | ||
|
||
const unsigned int id = node.id(); | ||
const auto status = node.info().status(); | ||
unsigned int parent_id; | ||
std::stringstream branch_label; | ||
double sum_of_infeasibilities = Inf; | ||
|
||
if (id == 0) { | ||
parent_id = 0; | ||
} else { | ||
parent_id = node.parent().id(); | ||
|
||
const auto& branching_decision = node.info().branching_decision(); | ||
branch_label << branching_decision.variable.name() << " "; | ||
if (branching_decision.type == LessOrEqual) { | ||
branch_label << "≤"; | ||
} else { | ||
branch_label << "≥"; | ||
} | ||
branch_label << " " << branching_decision.bound; | ||
} | ||
|
||
if (status == Optimal || status == Feasible) { | ||
sum_of_infeasibilities = node.info().sum_of_infeasibilities(); | ||
} | ||
|
||
file << id << "," | ||
<< parent_id << "," | ||
<< status << "," | ||
<< node.info().objective_value() << "," | ||
<< branch_label.str() << "," | ||
<< t_event << "," | ||
<< sum_of_infeasibilities | ||
<< std::endl; | ||
|
||
file.close(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters