Skip to content

Commit

Permalink
[iss-61]
Browse files Browse the repository at this point in the history
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
joaquinffernandez committed Mar 14, 2022
1 parent c76b365 commit 6086577
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 15 deletions.
3 changes: 2 additions & 1 deletion causalize/sbg_implementation/Makefile.include
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ CAUSALIZE_SBG_SRC := \
$(CAUSALIZE_SBG_DIR)/main.cpp\
$(CAUSALIZE_SBG_DIR)/matching_graph_builder.cpp\
$(UTIL_DIR)/ast_visitors/matching_exps.cpp \
$(UTIL_DIR)/ast_visitors/pwl_map_values.cpp
$(UTIL_DIR)/ast_visitors/pwl_map_values.cpp \
$(UTIL_DIR)/logger.cpp

# Objects
CAUSALIZE_SBG_OBJ=$(addprefix $(BUILD_DIR)/, $(CAUSALIZE_SBG_SRC:.cpp=.o))
Expand Down
11 changes: 6 additions & 5 deletions causalize/sbg_implementation/matching_graph_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <util/ast_visitors/eval_expression.h>
#include <util/ast_visitors/matching_exps.h>
#include <util/ast_visitors/pwl_map_values.h>
#include <util/logger.h>

using namespace Modelica;
using namespace SBG;
Expand Down Expand Up @@ -283,16 +284,16 @@ SBGraph MatchingGraphBuilder::makeGraph()
Apply(matching_exps, left);
Apply(matching_exps, right);
std::set<Expression> matched_exps = matching_exps.matchedExps();
std::cout << "Matched exps for: " << unknown_vertex.name() << " in " << eq_desc.second << std::endl;
std::cout << "Equation dom: " << dom << std::endl;
LOG << "Matched exps for: " << unknown_vertex.name() << " in " << eq_desc.second << std::endl;
LOG << "Equation dom: " << dom << std::endl;
foreach_(Expression exp, matched_exps)
{
std::cout << "Expression: " << exp << std::endl;
LOG << "Expression: " << exp << std::endl;
MatchingMaps maps = generatePWLMaps(exp, dom, unk_dom, set_edge_offset, eq_vertex.name(), max_dim);
set_edge_offset += dom.card();
std::string edge_name = "E_" + std::to_string(edge_id++);
std::cout << "MapF: " << maps.first << std::endl;
std::cout << "MapU: " << maps.second << std::endl;
LOG << "MapF: " << maps.first << std::endl;
LOG << "MapU: " << maps.second << std::endl;
SetEdge edge(edge_name, num_edges(graph) + 1, maps.first, maps.second, 0);
SetEdgeDesc e;
bool b;
Expand Down
15 changes: 6 additions & 9 deletions causalize/sbg_implementation/test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ GOOGLE_TEST_INSTALL = $(ROOT_DIR)/3rd-party/gtest
GOOGLE_TEST_LIB = gtest
GOOGLE_MOCK_LIB = gmock
GOOGLE_TEST_INCLUDE = $(GOOGLE_TEST_INSTALL)/usr/include
LIB += -lginac -lcln -lgmp
G++ = g++
G++_FLAGS = -c -Wall -I $(GOOGLE_TEST_INCLUDE) -std=c++11
G++_FLAGS = -c -Wall -I $(GOOGLE_TEST_INCLUDE) -std=c++17
LD_FLAGS = -L $(GOOGLE_TEST_INSTALL)/usr/lib -l $(GOOGLE_TEST_LIB) -l $(GOOGLE_MOCK_LIB) -l pthread
RM = rm -rf

Expand All @@ -27,15 +26,13 @@ MAIN_SRC = $(SRC_DIR)/main.cpp
TEST_SRC = $(TEST_DIR)/models_test.cpp

# Objects
TEST_OBJ=$(addprefix $(BUILD_DIR)/test_, $(notdir $(TEST_SRC:.cpp=.o)))
TEST_OBJ=$(addprefix $(BUILD_DIR)/, $(TEST_SRC:.cpp=.o))

MAIN_OBJ=$(addprefix $(BUILD_DIR)/, $(notdir $(MAIN_SRC:.cpp=.o)))
MAIN_OBJ=$(addprefix $(BUILD_DIR)/, $(MAIN_SRC:.cpp=.o))

$(BUILD_DIR)/test_%.o : $(TEST_DIR)/%.cpp
$(G++) $(G++_FLAGS) $< -o $@

$(BUILD_DIR)/%.o : $(SRC_DIR)/%.cpp
$(G++) $(G++_FLAGS) $< -o $@
$(BUILD_DIR)/%.o : %.cpp
$(G++) $(INCLUDE) $(CXXFLAGS) -MM -MT $@ -MF $(patsubst %.o,%.d,$@) $<
$(G++) $(G++_FLAGS) $(INCLUDE) $(LIB) $< -o $@

all: $(TARGET) $(RUN_TESTS)

Expand Down
2 changes: 2 additions & 0 deletions test/causalize/Makefile.include
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
include test/causalize/boost/Makefile.include

OBJS_CAUSALIZATION_COMMON := $(OBJS_COMMON) \
util/debug.o \
util/table.o \
Expand Down
38 changes: 38 additions & 0 deletions test/causalize/boost/Makefile.include
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)
187 changes: 187 additions & 0 deletions test/causalize/boost/boost_matching.cpp
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;
}

0 comments on commit 6086577

Please sign in to comment.