-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Squashed commit of the following: commit e82fdfd Author: Joaquin <joaquin.f.fernandez@gmail.com> Date: Mon Mar 14 14:55:09 2022 -0300 Setup logger in boost-cmp bijnary. commit 4a28dd5 Author: Joaquin <joaquin.f.fernandez@gmail.com> Date: Wed Mar 9 11:31:19 2022 -0300 Update version and usage messages for boost-cmp. commit 8356041 Author: Joaquin <joaquin.f.fernandez@gmail.com> Date: Wed Mar 9 11:18:33 2022 -0300 Added boost comparison binary for matching. commit c6f2a7b Author: Joaquin <joaquin.f.fernandez@gmail.com> Date: Wed Mar 9 11:17:55 2022 -0300 Update SBG library. commit 3390a47 Author: Joaquin <joaquin.f.fernandez@gmail.com> Date: Wed Mar 9 11:17:37 2022 -0300 Update stest object rule generation in Makefile. commit 5ad87b8 Author: Joaquin <joaquin.f.fernandez@gmail.com> Date: Wed Mar 9 11:16:57 2022 -0300 Use Logger in matching graph builder. commit fb5dddc Author: Joaquin <joaquin.f.fernandez@gmail.com> Date: Wed Feb 23 14:51:11 2022 -0300 Updated header files. commit 3725013 Author: Joaquin <joaquin.f.fernandez@gmail.com> Date: Wed Feb 23 14:50:56 2022 -0300 Updated makefiles. commit d5b8260 Author: Joaquin <joaquin.f.fernandez@gmail.com> Date: Wed Feb 23 14:50:36 2022 -0300 Added SBG library as 3rd party lib. commit 0255d7c Author: Joaquin <joaquin.f.fernandez@gmail.com> Date: Wed Feb 23 14:49:53 2022 -0300 Updated gitignore. commit 099d80d Author: Joaquin <joaquin.f.fernandez@gmail.com> Date: Wed Feb 23 14:47:06 2022 -0300 Removed graph library files.
- Loading branch information
1 parent
c76b365
commit 6086577
Showing
6 changed files
with
241 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# The Directories, Source, Includes, Objects, Binary | ||
CAUSALIZE_TEST_DIR := ./test/causalize | ||
BOOST_CMP_DIR := $(CAUSALIZE_TEST_DIR)/boost | ||
BOOST_CMP := bin/boost-cmp | ||
BOOST_CMP_LIB += -L$(ROOT_DIR)/lib -L$(ROOT_DIR)/3rd-party/sbg/sb-graph-dev/lib -lsbgraph -lginac -lcln -lgmp | ||
BOOST_CMP_INCLUDE = -I$(ROOT_DIR) -I$(ROOT_DIR)/3rd-party/sbg/sb-graph-dev/ | ||
|
||
all: $(BOOST_CMP) | ||
|
||
# Sources | ||
BOOST_CMP_SRC := \ | ||
$(UTIL_DIR)/debug.cpp \ | ||
$(UTIL_DIR)/table.cpp \ | ||
$(UTIL_DIR)/type.cpp \ | ||
$(UTIL_DIR)/ast_visitors/all_expressions.cpp \ | ||
$(UTIL_DIR)/ast_visitors/replace_expression.cpp \ | ||
$(UTIL_DIR)/ast_visitors/eval_expression.cpp \ | ||
$(UTIL_DIR)/ast_visitors/partial_eval_expression.cpp \ | ||
$(UTIL_DIR)/ast_visitors/ginac_interface.cpp \ | ||
$(UTIL_DIR)/ast_visitors/contains_expression.cpp \ | ||
$(UTIL_DIR)/ast_visitors/splitfor_visitor.cpp \ | ||
$(UTIL_DIR)/ast_visitors/state_variables_finder.cpp \ | ||
$(UTIL_DIR)/ast_visitors/pwl_map_values.cpp \ | ||
$(UTIL_DIR)/ast_visitors/matching_exps.cpp \ | ||
$(UTIL_DIR)/logger.cpp \ | ||
$(MMO_DIR)/mmo_class.cpp \ | ||
$(BOOST_CMP_DIR)/boost_matching.cpp \ | ||
$(CAUSALIZE_SBG_DIR)/matching_graph_builder.cpp | ||
|
||
# Objects | ||
BOOST_CMP_OBJ=$(addprefix $(BUILD_DIR)/, $(BOOST_CMP_SRC:.cpp=.o)) | ||
|
||
create-folders:: | ||
@mkdir -p $(BUILD_DIR)/$(CAUSALIZE_TEST_DIR) | ||
@mkdir -p $(BUILD_DIR)/$(BOOST_CMP_DIR) | ||
|
||
$(BOOST_CMP): $(BOOST_CMP_OBJ) $(CAUSALIZE_COMMON_OBJ) $(LIBMODELICA) | ||
$(CXX) $(CXXFLAGS) $(INCLUDES) -o $(BOOST_CMP) $(BOOST_CMP_OBJ) $(LIBS) |
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,187 @@ | ||
/***************************************************************************** | ||
This file is part of Modelica C Compiler. | ||
Modelica C Compiler is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
Modelica C Compiler is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with Modelica C Compiler. If not, see <http://www.gnu.org/licenses/>. | ||
******************************************************************************/ | ||
|
||
#include <chrono> | ||
#include <string> | ||
#include <iostream> | ||
#include <boost/graph/adjacency_list.hpp> | ||
#include <boost/graph/max_cardinality_matching.hpp> | ||
#include <boost/variant/get.hpp> | ||
#include <cassert> | ||
#include <getopt.h> | ||
|
||
|
||
#include <causalize/sbg_implementation/matching_graph_builder.h> | ||
#include <mmo/mmo_class.h> | ||
#include <parser/parser.h> | ||
#include <util/ast_visitors/state_variables_finder.h> | ||
#include <util/debug.h> | ||
#include <sbg/sbg.hpp> | ||
#include <sbg/sbg_algorithms.hpp> | ||
#include <sbg/sbg_printer.hpp> | ||
#include <sbg/graph_builders/ordinary_graph_builder.hpp> | ||
#include <util/logger.h> | ||
|
||
using namespace boost; | ||
using namespace std; | ||
|
||
using namespace Modelica; | ||
using namespace Modelica::AST; | ||
using namespace Causalize; | ||
|
||
void usage() | ||
{ | ||
cout << "Usage: boost-cpm [options] file" << endl; | ||
cout << "Computes matching for a given Modelica model file using SBG and boost matching algorithms and shows the execution time of both." << endl; | ||
cout << endl; | ||
cout << "-d, --debug Print debug information about graph generation." << endl; | ||
cout << "-h, --help Display this information and exit" << endl; | ||
cout << "-o <path>, --output <path> Sets the output path for the generated graph dot file." << endl; | ||
cout << endl; | ||
cout << "Modelica C Compiler home page: https://github.com/CIFASIS/modelicacc " << endl; | ||
} | ||
|
||
void version() | ||
{ | ||
cout << "Modelica C Compiler 2.0" << endl; | ||
cout << "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>" << endl; | ||
cout << "This is free software: you are free to change and redistribute it." << endl; | ||
cout << "There is NO WARRANTY, to the extent permitted by law." << endl; | ||
} | ||
|
||
//=============================================================================== | ||
// Based on the example code from Aaron Windsor: | ||
// | ||
// https://www.boost.org/doc/libs/1_46_0/libs/graph/example/matching_example.cpp | ||
// | ||
//=============================================================================== | ||
|
||
void computeMaxCardinalityMatching(SBG::SBGraph sb_graph, std::string dot_file, bool debug) | ||
{ | ||
|
||
OG::OrdinaryGraphBuilder ordinary_graph_builder(sb_graph); | ||
|
||
OG::Graph graph = ordinary_graph_builder.build(); | ||
|
||
if (debug) { | ||
OG::GraphPrinter printer(graph); | ||
printer.printGraph(dot_file+"_boost.dot"); | ||
} | ||
|
||
int n_vertices = num_vertices(graph); | ||
|
||
std::vector<graph_traits<OG::Graph>::vertex_descriptor> mate(n_vertices); | ||
auto begin = std::chrono::high_resolution_clock::now(); | ||
checked_edmonds_maximum_cardinality_matching(graph, &mate[0]); | ||
auto end = std::chrono::high_resolution_clock::now(); | ||
auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin); | ||
|
||
if (debug) { | ||
std::cout << std::endl << "Boost: Found a matching of size " << matching_size(graph, &mate[0]) << std::endl; | ||
|
||
std::cout << "The matching is:" << std::endl; | ||
|
||
graph_traits<OG::Graph>::vertex_iterator vi, vi_end; | ||
for(tie(vi,vi_end) = vertices(graph); vi != vi_end; ++vi) | ||
if (mate[*vi] != graph_traits<OG::Graph>::null_vertex() && *vi < mate[*vi]) | ||
std::cout << "{" << *vi << ", " << mate[*vi] << "}" << std::endl; | ||
|
||
std::cout << std::endl; | ||
} | ||
|
||
std::cout << "Boost Edmonds Maximum cardinality matching time: " << milliseconds.count() << " " << std::endl; | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
int opt; | ||
extern char* optarg; | ||
string output_path = ""; | ||
bool debug = false; | ||
|
||
while (true) { | ||
static struct option long_options[] = {{"version", no_argument, 0, 'v'}, | ||
{"help", no_argument, 0, 'h'}, | ||
{"debug", no_argument, 0, 'd'}, | ||
{"output", required_argument, 0, 'o'}, | ||
{0, 0, 0, 0}}; | ||
int option_index = 0; | ||
opt = getopt_long(argc, argv, "vhdo:", long_options, &option_index); | ||
if (opt == EOF) { | ||
break; | ||
} | ||
switch (opt) { | ||
case 'v': | ||
version(); | ||
exit(0); | ||
case 'h': | ||
usage(); | ||
exit(0); | ||
case 'd': | ||
debug = true; | ||
break; | ||
case 'o': | ||
output_path = optarg; | ||
break; | ||
default: | ||
abort(); | ||
} | ||
} | ||
|
||
StoredDef stored_def; | ||
bool status = false; | ||
if (argv[optind] != nullptr) { | ||
stored_def = Parser::ParseFile(argv[optind], status); | ||
} | ||
|
||
if (!status) { | ||
return -1; | ||
} | ||
|
||
Class ast_c = boost::get<Class>(stored_def.classes().front()); | ||
MMO_Class mmo_class(ast_c); | ||
StateVariablesFinder setup_state_var(mmo_class); | ||
setup_state_var.findStateVariables(); | ||
|
||
std::string dot_file = output_path+mmo_class.name(); | ||
Logger::instance().setFile(dot_file); | ||
|
||
MatchingGraphBuilder matching_graph_builder(mmo_class); | ||
|
||
SBG::SBGraph matching_graph = matching_graph_builder.makeGraph(); | ||
SBG::GraphPrinter printer(matching_graph, 0); | ||
|
||
printer.printGraph(dot_file+".dot"); | ||
|
||
MatchingStruct match(matching_graph); | ||
auto begin = std::chrono::high_resolution_clock::now(); | ||
pair<SBG::Set, bool> res = match.SBGMatching(); | ||
auto end = std::chrono::high_resolution_clock::now(); | ||
auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin); | ||
|
||
cout << "Generated matching:\n"; | ||
cout << get<0>(res) << "\n\n"; | ||
if (get<1>(res)) | ||
cout << ">>> Matched all unknowns\n"; | ||
std::cout << "SBG Algorithn matching time: " << milliseconds.count() << std::endl; | ||
|
||
computeMaxCardinalityMatching(matching_graph, dot_file, debug); | ||
|
||
return 0; | ||
} |