Skip to content

Commit 0cd7448

Browse files
author
Sancar Adali
committed
Successfully building and running version. Returns wrong permutation matrices
1 parent 05d7f3d commit 0cd7448

File tree

10 files changed

+122
-64
lines changed

10 files changed

+122
-64
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
*.o
2+
.Rproj.user

data/A_G.RData

174 Bytes
Binary file not shown.

data/A_H.RData

169 Bytes
Binary file not shown.

inst/include/tmp.txt

Whitespace-only changes.

man/rcpp_hello.Rd

+22-4
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,26 @@ A named list containing
1717
Runs the graph matching algorithm for graphs represented by \code{A} and \code{B}. Returns an \R \code{list} containing the permutation matrix \code{Pmat} and other computed variables.
1818
}
1919
\examples{
20-
A<-matrix( sample(c(0,1),125, replace = TRUE,prob=c(0.75,0.25)),15,15)
20+
B<-matrix( sample(c(0,1),225, replace = TRUE,prob=c(0.75,0.25)),15,15)
21+
diag(A)<- 1
2122
perm <- sample.int(15)
22-
B<-A [perm,perm]
23+
A<-B [perm,perm]
2324

2425
# parameters copied from test_simple example or graphm
2526
algorithm_params<-list(
2627
# Already provided as A and B matrices
2728
# *******************ALGORITHMS********************************
2829
# used algorithms and what should be used as
2930
#initial solution in corresponding algorithms
30-
algo="PATH",
31-
algo_init_sol="unif",
31+
algo="I QCV PATH",
32+
algo_init_sol="unif unif unif",
3233
solution_file="solution_im.txt",
3334
# coeficient of linear combination between
3435
# (1-alpha_ldh)*||graph_1-P*graph_2*P^T||^2_F +alpha_ldh*C_matrix
3536
alpha_ldh=0 ,
3637
cdesc_matrix="A" ,
3738
cscore_matrix="A" ,
39+
#C_matrix = "none",
3840
# **************PARAMETERS SECTION*****************************
3941
hungarian_max=10000 ,
4042
algo_fw_xeps=0.01 ,
@@ -73,4 +75,20 @@ graph_dot_print = 1
7375
print( algorithm_params)
7476
result <- run_graph_match(A, B, algorithm_params)
7577
print(result$Pmat)
78+
79+
80+
81+
82+
algorithm_params["algo"]<- "I U RANK QCV rand PATH"
83+
algorithm_params["algo_init_sol"] <- "unif unif unif unif unif unif"
84+
algorithm_params["alpha_ldh"] <- 0.44
85+
anotherresult <- run_graph_match(a,b, algorithm_params)
86+
87+
C<-matrix( runif( min = 0, max = 1,n= 225),15,15)
88+
diag(C)<- 1
89+
B <- C*B
90+
perm <- sample.int(15)
91+
A<-B [perm,perm]
92+
weightedmatrixresult <- run_graph_match(A,B, algorithm_params)
93+
7694
}

src/Makevars.win

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# combine with standard arguments for R
55
RCPPGSL_LDFLAGS = `$(R_HOME)/bin/Rscript -e "RcppGSL:::LdFlags()"`
66
#RCPPGSL_CFLAGS
7-
PKG_CPPFLAGS = $(GSL_CFLAGS) -I$(LIB_GSL)/include -I../inst/include -I../inst -Wall -Wno-unused-variable -Wno-unused-but-set-variable -Wno-long-long -Wno-sign-compare -pedantic -pthread -fPIC -O2 -g
7+
PKG_CPPFLAGS = $(GSL_CFLAGS) -I$(LIB_GSL)/include -I../inst/include -I../inst -Wall -Wno-unused-but-set-variable -Wno-long-long -Wno-sign-compare -pedantic -pthread -fPIC -O2 -g
88

99

1010
GRAPHM_OBJ = ./algorithm_ca.o ./algorithm_ext.o ./algorithm_fsol.o ./algorithm_iden.o ./algorithm_lp.o ./algorithm.o ./algorithm_path.o ./algorithm_qcv.o ./algorithm_rand.o ./algorithm_rank.o ./algorithm_umeyama.o ./algorithm_unif.o ./experiment.o ./graph.o ./hungarian.o ./rpc.o

src/graphm/experiment.cpp

+33-15
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void experiment::run_experiment(graph &g, graph &h)
3737
parameter pdebug_f = get_param("debugprint_file");
3838
pdebug.strvalue=pdebug_f.strvalue;
3939
std::string sverbfile=get_param_s("verbose_file");
40-
bool bverbose=(get_param_i("verbose_mode")==1);
40+
bool bverbose= 1; //(get_param_i("verbose_mode")==1);
4141
std::ofstream fverbose;
4242
if (sverbfile.compare("cout")==0){
4343
fverbose.open("Rconsoleout.txt");
@@ -53,14 +53,14 @@ void experiment::run_experiment(graph &g, graph &h)
5353
std::string sexp_type=get_param_s("exp_type");
5454
std::string salgo_match=get_param_s("algo");
5555
std::istrstream istr(salgo_match.c_str());
56-
std::vector<std::string>v_salgo_match;
56+
std::vector<std::string> v_salgo_match;
5757
std::string stemp;
5858
while (istr>>stemp)
5959
v_salgo_match.push_back(stemp);
6060
//used initialization algorithms
6161
std::string salgo_init=get_param_s("algo_init_sol");
6262
std::istrstream istrinit(salgo_init.c_str());
63-
std::vector<std::string>v_salgo_init;
63+
std::vector<std::string> v_salgo_init;
6464
while (istrinit>>stemp)
6565
v_salgo_init.push_back(stemp);
6666
if (v_salgo_init.size()!=v_salgo_match.size())
@@ -78,11 +78,20 @@ void experiment::run_experiment(graph &g, graph &h)
7878

7979

8080
if (get_param_i("graph_dot_print")==1){ g.printdot("g.dot");h.printdot("h.dot");};
81+
82+
83+
84+
int N1 =0;
85+
N1 = g.get_adjmatrix()->size1;
86+
87+
int N2 =0;
88+
N2 = h.get_adjmatrix()->size1;
89+
char fname[4];
8190
// Matrix of local similarities between graph vertices
82-
std::string strfile=get_param_s("C_matrix");
83-
int N1 =g.get_adjmatrix()->size1;
84-
int N2 =h.get_adjmatrix()->size1;
8591
gsl_matrix* gm_ldh=gsl_matrix_alloc(N1,N2);
92+
93+
94+
std::string strfile=get_param_s("C_matrix");
8695
FILE *f=fopen(strfile.c_str(),"r");
8796
if (f!=NULL){
8897
gsl_set_error_handler_off();
@@ -102,7 +111,7 @@ void experiment::run_experiment(graph &g, graph &h)
102111
if (get_param_i("C_matrix_dist")==1)
103112
gsl_matrix_scale(gm_ldh,-1);
104113

105-
if (bverbose) *gout<<"Graph synhronization"<<std::endl;
114+
if (bverbose) *gout<<"Graph synchronization"<<std::endl;
106115
synchronize(g,h,&gm_ldh);
107116

108117
//Cycle over all algoirthms
@@ -114,18 +123,24 @@ void experiment::run_experiment(graph &g, graph &h)
114123
algo_i->set_ldhmatrix(gm_ldh);
115124
match_result mres_i=algo_i->gmatch(g,h,NULL,NULL,dalpha_ldh);
116125
delete algo_i;
126+
char fname[5];
127+
printout(itoa(a,fname,10));
117128
//main algorithm
118129
algorithm* algo=get_algorithm(v_salgo_match[a]);
119130
algo->read_config(get_config_string());
120131
algo->set_ldhmatrix(gm_ldh);
121132
match_result mres_a=algo->gmatch(g,h,(mres_i.gm_P_exact!=NULL)?mres_i.gm_P_exact:mres_i.gm_P, NULL,dalpha_ldh);
133+
if (bverbose) {
134+
*gout<<"Finished matching with " <<itoa(a,fname,10) << "th algorithm of experiment" <<std::endl;
135+
}
122136
mres_a.vd_trace.clear();
123137
mres_a.salgo=v_salgo_match[a];
124138
v_mres.push_back(mres_a);
125139
delete algo;
140+
printout(itoa(a,fname,10));
126141
};
127142
gsl_matrix_free(gm_ldh);
128-
printout();
143+
//printout("after_experiment");
129144
}
130145
experiment::~experiment()
131146
{
@@ -190,14 +205,15 @@ int experiment::get_algo_len(){
190205

191206

192207
gsl_matrix* experiment::get_P_result(int algo_index){
193-
if ( algo_index<this->v_mres.size() && algo_index>=0 ){
194-
// if ( this->v_mres[algo_index].gm_P_exact != NULL){
195-
// return this->v_mres[algo_index].gm_P_exact;
196-
// } else{
197-
return this->v_mres[algo_index].gm_P;
198-
// }
208+
if ((algo_index >= 0 ) && ( algo_index < this->v_mres.size()) ) {
209+
if ( this->v_mres[algo_index].gm_P_exact != NULL){
210+
return this->v_mres[algo_index].gm_P_exact;
211+
} else{
212+
return this->v_mres.at(algo_index).gm_P;
213+
}
199214
} else {
200-
return NULL;
215+
throw std::runtime_error("Out-of-bounds error for experiment results vector\n ");
216+
201217
}
202218
}
203219

@@ -248,6 +264,7 @@ void experiment::printout(std::string fname_out,std::string sformat)
248264
fout<<v_mres.at(a).salgo<<" ";
249265
fout.precision(2);
250266
fout<<std::endl;
267+
if (v_mres.size() >0 ){
251268
for (int i=0;i<v_mres.at(0).gm_P->size1;i++)
252269
{
253270
for (int a=0;a<v_mres.size();a++)
@@ -261,6 +278,7 @@ void experiment::printout(std::string fname_out,std::string sformat)
261278
};
262279
fout<<std::endl;
263280
};
281+
}
264282

265283
};
266284
}

src/graphm/experiment.h

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
#include <strstream>
4141
#include <stdexcept>
4242
#include <exception>
43+
#include <cstdlib>
44+
#include <algorithm>
4345
/**
4446
Experiment class. It implements all extern routines for graph matching experiments
4547

src/graphm/graph.h

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class graph : public rpc
5050
int load_graph(std::string fgraph,char ftype='A',char cformat='D',std::string fvertexlst_name="");
5151
//adjacency matrix control
5252
const gsl_matrix* get_adjmatrix(){return gm_A;};
53+
5354
int set_adjmatrix(const gsl_matrix* _gm_A);
5455
gsl_matrix* get_descmatrix(char dmt);
5556
int printout(std::string fname_out);

src/graphmatch_rcpp.cpp

+62-44
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ Rcpp::List run_graph_match(const RcppGSL::Matrix& A, const RcppGSL::Matrix& B, c
6363
}
6464
}
6565
}
66+
int expected_result_count=0;
6667
if (!Rf_isNull(algorithm_params["algo"]) ) {
6768
string str(as<string>(algorithm_params["algo"]));
6869
string buf; // Have a buffer string
@@ -72,64 +73,81 @@ Rcpp::List run_graph_match(const RcppGSL::Matrix& A, const RcppGSL::Matrix& B, c
7273
while (ss >> buf)
7374
algo_list.push_back(buf);
7475

76+
expected_result_count = algo_list.size();
77+
7578
} else{
76-
algorithm_params["algo"] = "PATH";
77-
algorithm_params["algo_init_sol"] = "unif";
79+
//algorithm_params["algo"] = "PATH";
80+
//algorithm_params["algo_init_sol"] = "unif";
7881

7982
exp.set_param("algo",std::string("PATH"));
8083
exp.set_param("algo_init_sol",std::string("unif"));
81-
84+
// expected_result_count = 1;
8285
}
8386

8487
//exp.read_config(conc_params_string);
88+
exp.printout("before_Exp.txt","Parameters");
8589
int P_nr = max(A.nrow(),B.ncol());
8690
Rcpp::NumericMatrix P(P_nr);
91+
std::vector<Rcpp::NumericMatrix> P_matrix_list ;
8792
try {
93+
8894
exp.run_experiment(graphm_obj_A, graphm_obj_B);
8995

9096
int exp_count = exp.get_algo_len();
9197

92-
exp.printout("test.txt");
93-
94-
std::vector<Rcpp::NumericMatrix> P_matrix_list ;
95-
96-
for (int exp_i = 0; exp_i < exp_count; exp_i++) {
97-
RcppGSL::Matrix P_tmp ( exp.get_P_result(exp_i) ) ;
98-
if ( P_tmp != NULL ) {
99-
int mat_size = P_tmp.nrow()*P_tmp.ncol();
100-
try{
101-
for (int j =0 ; j< P_tmp.nrow(); j++){
102-
for (int k =0 ; k< P_tmp.ncol(); k++){
103-
P(j,k) = P_tmp(j,k);
104-
}
105-
106-
107-
}
108-
}
109-
catch (...){
110-
Rf_error("could not copy graphm result to Rcpp matrix");
111-
return Rcpp::List();
112-
113-
}
114-
}
115-
else{
116-
Rf_error("graphm returned NULL as matrix pointer");
117-
return Rcpp::List();
118-
}
119-
120-
121-
P_matrix_list.push_back(P);
122-
123-
P_tmp.free();
124-
125-
}
126-
Rcpp::List res = Rcpp::List::create(
127-
Rcpp::Named("debugprint_file") = algorithm_params["debugprint_file"],
128-
Rcpp::Named("Pmat") = P_matrix_list,
129-
Rcpp::Named("exp_count") = exp_count
130-
);
131-
132-
return res;
98+
exp.printout("experiment ran.txt");
99+
100+
Rf_warning("exp count is %d",exp_count);
101+
102+
for (int exp_i = 0; exp_i < exp_count; exp_i++) {
103+
try{
104+
105+
exp.get_P_result(exp_i);
106+
}
107+
catch (...){
108+
Rf_warning("Unable to get perm mat results from graphm experiment object ");
109+
}
110+
//RcppGSL::Matrix P_tmp (A.nrow(),B.ncol()) ;
111+
112+
RcppGSL::Matrix P_tmp ( exp.get_P_result(exp_i) ) ;
113+
if ( P_tmp != NULL ) {
114+
int mat_size = P_tmp.nrow();
115+
mat_size *= P_tmp.ncol();
116+
if (P_tmp.nrow()==0 || P_tmp.ncol()==0 ) {
117+
Rf_error("0 sized P_tmp: Permutation matrix");
118+
return Rcpp::List();
119+
}
120+
try{
121+
for (int j =0 ; j< P_tmp.nrow(); j++){
122+
for (int k =0 ; k< P_tmp.ncol(); k++){
123+
P(j,k) = P_tmp(j,k);
124+
}
125+
126+
}
127+
}
128+
catch (...){
129+
Rf_error("could not copy graphm result to Rcpp matrix");
130+
return Rcpp::List();
131+
//
132+
}
133+
} else {
134+
Rf_error("graphm returned NULL as matrix pointer");
135+
return Rcpp::List();
136+
}
137+
//
138+
//
139+
P_matrix_list.push_back(P);
140+
141+
P_tmp.free();
142+
143+
}
144+
Rcpp::List res = Rcpp::List::create(
145+
Rcpp::Named("debugprint_file") = algorithm_params["debugprint_file"],
146+
Rcpp::Named("Pmat") = P_matrix_list,
147+
Rcpp::Named("exp_count") = exp_count
148+
);
149+
150+
return res;
133151

134152
} catch( std::exception &ex) {
135153
Rf_error(ex.what());

0 commit comments

Comments
 (0)