Beispiel #1
0
void BE::initandstore(std::vector<Variable*> &variables, std::vector<Function*> &functions, std::vector<int> &order)
{
	//pe=Double(1.0);
	//vector<vector<Function*> > buckets (order.size());
	buckets.resize(order.size());
	hashtable.resize(order.size());
	dirtyBitArrays.resize(order.size());
	bucketfuncs.resize(order.size());
	margVarList.resize(buckets.size());
	this->order = order;
	changedindexes.resize(order.size());
	vector<int> var_in_pos(order.size());
	for(int i=0;i<var_in_pos.size();i++) {
		var_in_pos[order[i]]=i;
	}

	// First put the functions in the proper buckets
	for(int i=0;i<functions.size();i++)
	{
		int pos=order.size();
		//LogFunction* function=new LogFunction(*functions[i]);
		if(functions[i]->variables().empty())
		{
			pe*=functions[i]->table()[0];

			continue;
		}
		//Function* function=new Function();
		//createFunction(*functions[i],*function);
		Function* function = functions[i];
		for(int j=0;j<functions[i]->variables().size();j++)
		{
			if(var_in_pos[functions[i]->variables()[j]->id()] < pos)
				pos=var_in_pos[functions[i]->variables()[j]->id()];
		}
		assert(pos!=(int)order.size());
		//check if exceeds ibound
		if(buckets[pos].size()==0) {
			buckets[pos].resize(1);
		}
		vector<Variable*> bucket_variables;
		for(int j=0;j<buckets[pos][buckets[pos].size()-1].size();j++)
		{
			do_set_union(bucket_variables,buckets[pos][buckets[pos].size()-1][j]->variables(),bucket_variables,less_than_comparator_variable);
		}
		if(bucket_variables.size() > ibound) {
			vector<Function*> tmpfuncs;
			tmpfuncs.push_back(function);
			buckets[pos].push_back(tmpfuncs);
		} else {
			buckets[pos][buckets[pos].size()-1].push_back(function);
		}
	}

	int prval;
	currPRVal = 1;
	for(int i=0;i<buckets.size();i++)
	{
		if(buckets[i].empty())
			continue;
		for(int j=0;j<buckets[i].size();j++)
		{
			vector<Variable*> bucket_variables;
			for(int k=0;k<buckets[i][j].size();k++) {
				do_set_union(bucket_variables,buckets[i][j][k]->variables(),bucket_variables,less_than_comparator_variable);
			}
			vector<Variable*> bucket_variable;
			bucket_variable.push_back(variables[order[i]]);
			vector<Variable*> marg_variables;
			do_set_difference(bucket_variables,bucket_variable,marg_variables,less_than_comparator_variable);
			Function* function= new Function();
			bucketfuncs[i].push_back(function);
			vector<int> htable;
			hashtable[i].push_back(htable);
			//Function::multiplyAndMarginalize(marg_variables,buckets[i],*function,false);
			Function::multiplyAndMarginalizeNew(marg_variables,buckets[i][j],*function,hashtable[i][hashtable[i].size()-1]);
			if(i==buckets.size()-1) {
				currPRVal *= get_norm_const(*function);
			}
			//cout<<bucket_variables.size()<<" "<<flush;
			//cout<<"buck-vars.size ="<<bucket_variables.size()<<endl;
			//cerr<<"Processing bucket "<<i<<" out of "<<buckets.size()<<endl;
			if(function->variables().empty())
			{
				assert((int)function->table().size()==1);
				//pe*=function->table()[0];
				//currPRVal = function->table()[0];
				//delete(function);
				continue;
			}
		
			//prval = get_norm_const(*function);
			//pe*=prval;
			//Put the function in the appropriate bucket
			int pos=order.size();
			//function->print(cout);
			//assert(!function->log_table.empty());

			for(int k=0;k<function->variables().size();k++)
			{
				if(var_in_pos[function->variables()[k]->id()] < pos)
					pos=var_in_pos[function->variables()[k]->id()];
			}
			assert(pos!=(int)order.size());
			assert(pos > i);
			int indextoadd=-1;
			vector<Variable*> new_vars;
			for(int k=0;k<buckets[pos].size();k++)
			{
				vector<Variable*> bucket_variables;
				for(int m=0;m<buckets[pos][k].size();m++) {
					do_set_union(bucket_variables,buckets[pos][k][m]->variables(),bucket_variables,less_than_comparator_variable);
				}
				do_set_union(bucket_variables,function->variables(),bucket_variables,less_than_comparator_variable);
				if(bucket_variables.size() <= ibound) {
					indextoadd = k;
					new_vars = bucket_variables;
					break;
				}
			}
			if(indextoadd==-1) {
				vector<Function*> tmp_f;
				tmp_f.push_back(function);
				buckets[pos].push_back(tmp_f);
			} else {
				buckets[pos][indextoadd].push_back(function);
			}
		}
		
	}

	for(int i=0;i<buckets.size();i++)
	{
		changedindexes[i].resize(buckets[i].size());
		vector<Variable*> bucket_variable;
		bucket_variable.push_back(variables[order[i]]);
		for(int j=0;j<buckets[i].size();j++) {
			vector<Variable*> bucket_variables;
			for(int k=0;k<buckets[i][j].size();k++) {
				do_set_union(bucket_variables,buckets[i][j][k]->variables(),bucket_variables,less_than_comparator_variable);
			}
			vector<Variable*> marg_variables;
			do_set_difference(bucket_variables,bucket_variable,marg_variables,less_than_comparator_variable);
			margVarList[i].push_back(marg_variables);
		}
	}

	/*
	for(int i=0;i<buckets.size();i++){
		for(int j=0;j<buckets[i].size();j++){
			if (buckets[i][j]!=NULL){
				delete(buckets[i][j]);
			}
		}
	}
	buckets.clear();
	*/
	//BESample(variables,functions,order,buckets);
}
Beispiel #2
0
void BE::init(std::vector<Variable*> &variables, std::vector<Function*> &functions, std::vector<int> &order)
{
	//pe=Double(1.0);
	//vector<vector<Function*> > buckets (order.size());
	vector<vector<Function*> > tmp_buckets(order.size());
	this->order = order;
	vector<int> var_in_pos(order.size());
	for(int i=0;i<var_in_pos.size();i++) {
		var_in_pos[order[i]]=i;
	}

	// First put the functions in the proper buckets
	for(int i=0;i<functions.size();i++)
	{
		int pos=order.size();
		//LogFunction* function=new LogFunction(*functions[i]);
		if(functions[i]->variables().empty())
		{
			pe*=functions[i]->table()[0];

			continue;
		}
		//Function* function=new Function();
		//createFunction(*functions[i],*function);
		Function* function = functions[i];
		for(int j=0;j<functions[i]->variables().size();j++)
		{
			if(var_in_pos[functions[i]->variables()[j]->id()] < pos)
				pos=var_in_pos[functions[i]->variables()[j]->id()];
		}
		assert(pos!=(int)order.size());
		tmp_buckets[pos].push_back(function);
	}
	int prval;
	for(int i=0;i<tmp_buckets.size();i++)
	{
		if(tmp_buckets[i].empty())
			continue;

		vector<Variable*> bucket_variables;
		for(int j=0;j<tmp_buckets[i].size();j++)
		{
			do_set_union(bucket_variables,tmp_buckets[i][j]->variables(),bucket_variables,less_than_comparator_variable);
		}
		//cout<<bucket_variables.size()<<" "<<flush;
		//cout<<"buck-vars.size ="<<bucket_variables.size()<<endl;
		//cerr<<"Processing bucket "<<i<<" out of "<<buckets.size()<<endl;


		// Compute variables required for marginalization
		//cerr<<bucket_variables.size()<<endl;
		vector<Variable*> bucket_variable;
		bucket_variable.push_back(variables[order[i]]);
		vector<Variable*> marg_variables;
		do_set_difference(bucket_variables,bucket_variable,marg_variables,less_than_comparator_variable);
		
		Function* function= new Function();
		
		Function::multiplyAndMarginalize(marg_variables,tmp_buckets[i],*function,false);
		
		if(i==tmp_buckets.size()-1) {
			currPRVal = get_norm_const(*function);
		}
		if(function->variables().empty())
		{
			assert((int)function->table().size()==1);
			//pe*=function->table()[0];
			//currPRVal = function->table()[0];
			//delete(function);
			continue;
		}
		
		//prval = get_norm_const(*function);
		//pe*=prval;
		//Put the function in the appropriate bucket
		int pos=order.size();
		//function->print(cout);
		//assert(!function->log_table.empty());

		for(int j=0;j<function->variables().size();j++)
		{
			if(var_in_pos[function->variables()[j]->id()] < pos)
				pos=var_in_pos[function->variables()[j]->id()];
		}
		assert(pos!=(int)order.size());
		assert(pos > i);
		tmp_buckets[pos].push_back(function);
		
	}
	

	/*
	for(int i=0;i<buckets.size();i++){
		for(int j=0;j<buckets[i].size();j++){
			if (buckets[i][j]!=NULL){
				delete(buckets[i][j]);
			}
		}
	}
	buckets.clear();
	*/
	//BESample(variables,functions,order,buckets);
}
void AO_COSP::buildSF(int curr_variable)
{
	cout<<"BuildSF...."<<curr_variable<<" "<<flush;
	int pos_curr_variable=mapped_order[curr_variable];
	if(!run_ijgp[pos_curr_variable])
		return;
	jg->propagate();
	vector<Variable*> all_variables;
	for(int i=pos_curr_variable;i>-1;i--)
	{
		if(run_ijgp[i] && i!=pos_curr_variable)
			return;
		
		int var=order[i];
		cout<<"Var = "<<var<<endl;
		all_variables.push_back(gm->variables[var]);
		sort(all_variables.begin(),all_variables.end(),less_than_comparator_variable);
		int max=INVALID_VALUE;
		int max_cluster_id=INVALID_VALUE;
		for(int j=0;j<jg->nodes.size();j++)
		{
			bool found=false;
			for(int k=0;k<jg->nodes[j]->variables().size();k++)
			{
				if(jg->nodes[j]->variables()[k]->id()==var)
				{
					found=true;
					break;
				}
			}
			if(found)
			{
				vector<Variable*> temp;
				do_set_intersection(all_variables,jg->nodes[j]->variables(),temp,less_than_comparator_variable);
				if((int)temp.size() > max)
				{
					max=(int)temp.size();
					max_cluster_id=j;
				}
			}
		}
		if(max_cluster_id == INVALID_VALUE)
		{
			//jg->print(cout);
			cout<<"invalid value for var = "<<var<<endl;
			vector<Variable*> cond_vars;
			CPT& cpt=sampling_functions[var];
			cpt.variables().push_back(gm->variables[var]);
			cpt.cond_variables()=vector<Variable*>();
			cpt.setMargVariable(gm->variables[var]);
			cpt.table()=vector<Double> (cpt.marg_variable()->domain_size());
			for(int j=0;j<cpt.table().size();j++)
			{
				cpt.table()[j]=Double((double)1.0/(double)cpt.marg_variable()->domain_size());
			}
		
	
			continue;
		}
		
		assert(max_cluster_id > INVALID_VALUE);
		//Form the function
		vector<Variable*> temp;
		do_set_intersection(all_variables,jg->nodes[max_cluster_id]->variables(),temp,less_than_comparator_variable);
		vector<Variable*> marg_vars;
		marg_vars.push_back(gm->variables[var]);
		do_set_difference(temp,marg_vars,temp,less_than_comparator_variable);
		CPT& cpt=sampling_functions[var];
		jg->nodes[max_cluster_id]->getCF(temp,gm->variables[var],cpt);
	}
}
AO_COS::AO_COS(GM* gm, vector<int>& order, JG* jg,vector<vector<Variable*> >& ancestors)
{
	csp=new GM();
	gm->convertToCN(*csp);
	cp_algo=new DRC(*csp,jg->i_bound(),order);
	//vector<Variable*> all_variables;
	//cout<<"\t "<<gm->variables.size()<<endl;
	sampling_functions=vector<CPT>(gm->variables.size());
	for(int i=0;i<gm->variables.size();i++)
	{
		int var=order[order.size()-i-1];
		vector<Variable*>& all_variables=ancestors[var];
		//all_variables.push_back(gm->variables[var]);
		//sort(all_variables.begin(),all_variables.end(),less_than_comparator_variable);
		// Find the largest cluster in the join graph which has a non-zero intersection with all_variables
		int max_cluster_id=INVALID_VALUE;
		int max=INVALID_VALUE;
		for(int j=0;j<jg->nodes.size();j++)
		{
			bool found=false;
			for(int k=0;k<jg->nodes[j]->variables().size();k++)
			{
				if(jg->nodes[j]->variables()[k]->id()==var)
				{
					found=true;
					//max_cluster_id=j;
					
					break;
				}
			}
			if(found)
			{
				//break;
				//cout<<"Variable found "<<var<<"\n";
				vector<Variable*> temp;
				do_set_intersection(all_variables,jg->nodes[j]->variables(),temp,less_than_comparator_variable);
				if((int)temp.size() > max)
				{
					max=(int)temp.size();
					max_cluster_id=j;
				}
			}
		}
		
		//max_cluster_id=INVALID_VALUE;
		if(max_cluster_id == INVALID_VALUE)
		{
			//jg->print(cout);
			cout<<"invalid value for var = "<<var<<endl;
			vector<Variable*> cond_vars;
			CPT& cpt=sampling_functions[var];
			cpt.variables().push_back(gm->variables[var]);
			cpt.cond_variables()=vector<Variable*>();
			cpt.setMargVariable(gm->variables[var]);
			cpt.table()=vector<Double> (cpt.marg_variable()->domain_size());
			for(int j=0;j<cpt.table().size();j++)
			{
				cpt.table()[j]=Double((double)1.0/(double)cpt.marg_variable()->domain_size());
			}
			//cpt.epsilonCorrection();
	
			continue;
		}
		//cout<<i<<endl;
		assert(max_cluster_id > INVALID_VALUE);
		//Form the function
		vector<Variable*> temp;
		do_set_intersection(all_variables,jg->nodes[max_cluster_id]->variables(),temp,less_than_comparator_variable);
		vector<Variable*> marg_vars;
		marg_vars.push_back(gm->variables[var]);
		do_set_difference(temp,marg_vars,temp,less_than_comparator_variable);
		
		CPT& cpt=sampling_functions[var];
		
		jg->nodes[max_cluster_id]->getCF(temp,gm->variables[var],cpt);
		//Double epsilon(0.1);
		//cpt.epsilonCorrection(epsilon);
		
		
	
	}
}
Beispiel #5
0
JG::JG(GM& gm, int i_bound__, int num_iterations__, vector<int>& order,
		JG_TYPE type) :
	num_iterations_(num_iterations__), i_bound_(i_bound__), copy_of_gm(&gm) {
	//printStacktrace();
	vector<int> mapped_order(order.size());
	for (int i = 0; i < order.size(); i++)
		mapped_order[order[i]] = i;
	assert(order.size()==gm.variables.size());
	//cerr<<"Creating buckets\n";
	// Create buckets
	vector<vector<JGHelperFunction> > buckets(order.size());
	for (int i = 0; i < gm.functions.size(); i++) {
		int min_size = (int) gm.variables.size();
		for (int j = 0; j < gm.functions[i]->variables().size(); j++) {
			if (gm.functions[i]->variables()[j]->id() >= order.size()) {
				gm.functions[i]->print();
				exit(1);
			}
			if (mapped_order[gm.functions[i]->variables()[j]->id()] < min_size) {
				min_size = mapped_order[gm.functions[i]->variables()[j]->id()];
			}
		}
		if (min_size == (int) gm.variables.size()) {
			for (int j = 0; j < gm.functions[i]->variables().size(); j++) {
				cerr << gm.functions[i]->variables()[j]->id() << endl;
			}

		}
		//cout<<min_size<<endl;
		buckets[min_size].push_back(JGHelperFunction(
				gm.functions[i]->variables(), INVALID_VALUE, gm.functions[i]));
		//cout<<"\tAdded\n";
	}
	//cout<<"buckets created\n";

	//Update i_bound
	/*
	 int max_func_size=0;

	 for(int i=0;i<gm.functions.size();i++)
	 {
	 int domain_size=0;
	 domain_size=Variable::getDomainSize(gm.functions[i]->variables());
	 if(max_func_size < domain_size)
	 {
	 max_func_size=domain_size;
	 }
	 }
	 int avg_domain_size=0;
	 for(int i=0;i<gm.variables.size();i++)
	 {
	 avg_domain_size+=gm.variables[i]->domain_size();
	 }
	 avg_domain_size/=(int)gm.variables.size();
	 cerr<<avg_domain_size<<" "<<max_func_size<<" ";
	 int i_bound=1;
	 for(int i=0;i<(i_bound_-1);i++)
	 {
	 i_bound*=avg_domain_size;
	 }
	 i_bound*=(max_func_size);
	 */
	//max_cluster_size=i_bound;
	cerr << "Dom size i-bound = " << i_bound() << endl;
	int old_nodes = 0;
	int id = 0;
	// Run schematic mini-buckets
	for (int i = 0; i < order.size(); i++) {
		//cerr<<i<<endl;
		// Form mini-buckets by using the first fill rule
		for (int j = 0; j < buckets[i].size(); j++) {
			bool found = false;
			int loc = INVALID_VALUE;
			for (int k = old_nodes; k < nodes.size(); k++) {
				vector<Variable*> temp_variables;
				do_set_union(buckets[i][j].variables, nodes[k]->variables(),
						temp_variables, less_than_comparator_variable);
				//int domain_size=Variable::getDomainSize(temp_variables);
				//if((int)domain_size <= i_bound)
				if ((int) temp_variables.size() <= i_bound()) {
					found = true;
					loc = k;
					break;
				}
			}
			// If not found create a mini-bucket
			if (!found) {

				nodes.push_back(addNode(type));
				nodes[nodes.size() - 1]->id() = id;
				id++;
				nodes[nodes.size() - 1]->variables() = buckets[i][j].variables;
				if (buckets[i][j].parent_id != INVALID_VALUE) {
					//cout<<"F1\n";
					//nodes[nodes.size()-1]->neighbors().push_back(nodes[buckets[i][j].parent_id]);
					//nodes[buckets[i][j].parent_id]->neighbors().push_back(nodes[nodes.size()-1]);
					JGEdge* edge = addEdge(nodes[nodes.size() - 1],
							nodes[buckets[i][j].parent_id], type);
					do_set_intersection(nodes[nodes.size() - 1]->variables(),
							nodes[buckets[i][j].parent_id]->variables(),
							edge->variables(), less_than_comparator_variable);
					//edge->setNode1(nodes[nodes.size()-1]);
					//edge->setNode2(nodes[buckets[i][j].parent_id]);
					do_set_intersection(nodes[nodes.size() - 1]->variables(),
							nodes[buckets[i][j].parent_id]->variables(),
							edge->variables(), less_than_comparator_variable);
					nodes[nodes.size() - 1]->edges().push_back(edge);
					nodes[buckets[i][j].parent_id]->edges().push_back(edge);

				} else {
					//cout<<"F2\n";
					nodes[nodes.size() - 1]->addFunction(*buckets[i][j].f);
				}
			} else {

				do_set_union(buckets[i][j].variables, nodes[loc]->variables(),
						nodes[loc]->variables(), less_than_comparator_variable);
				if (buckets[i][j].parent_id != INVALID_VALUE) {
					//cout<<"F3\n";
					/*nodes[loc]->neighbors().push_back(nodes[buckets[i][j].parent_id]);
					 nodes[buckets[i][j].parent_id]->neighbors().push_back(nodes[loc]);*/
					JGEdge* edge = addEdge(nodes[loc],
							nodes[buckets[i][j].parent_id], type);
					do_set_intersection(nodes[loc]->variables(),
							nodes[buckets[i][j].parent_id]->variables(),
							edge->variables(), less_than_comparator_variable);
					//edge->setNode1(nodes[loc]);
					//edge->setNode2(nodes[buckets[i][j].parent_id]);
					do_set_intersection(nodes[loc]->variables(),
							nodes[buckets[i][j].parent_id]->variables(),
							edge->variables(), less_than_comparator_variable);
					nodes[loc]->edges().push_back(edge);
					nodes[buckets[i][j].parent_id]->edges().push_back(edge);

				} else {
					//cout<<"F4\n";
					nodes[loc]->addFunction(*buckets[i][j].f);
				}

			}
		}
		vector<Variable*> curr_variable;
		curr_variable.push_back(gm.variables[order[i]]);

		// Connect the newly created nodes to each other
		for (int j = old_nodes; j < (int) nodes.size() - 1; j++) {
			//nodes[j]->neighbors().push_back(nodes[j+1]);
			//nodes[j+1]->neighbors().push_back(nodes[j]);
			JGEdge* edge = addEdge(nodes[j], nodes[j + 1], type);
			//edge->setNode1(nodes[j]);
			//edge->setNode2(nodes[j+1]);
			edge->variables() = curr_variable;
			nodes[j]->edges().push_back(edge);
			nodes[j + 1]->edges().push_back(edge);
		}

		if (i < (int) order.size() - 1) {
			for (int j = old_nodes; j < nodes.size(); j++) {

				vector<Variable*> temp_variables;
				do_set_difference(nodes[j]->variables(), curr_variable,
						temp_variables, less_than_comparator_variable);
				if (temp_variables.empty())
					continue;
				// Put the node in the appropriate bucket
				int min_size = (int) gm.variables.size();
				for (int k = 0; k < temp_variables.size(); k++) {

					if (min_size > mapped_order[temp_variables[k]->id()]) {
						min_size = mapped_order[temp_variables[k]->id()];
					}
				}
				assert(min_size<(int)gm.variables.size());
				//if(min_size >=(int) gm.variables.size())
				//	continue;
				buckets[min_size].push_back(JGHelperFunction(temp_variables, j));
			}
		}
		old_nodes = (int) nodes.size();
	}

	//print();
	// Minimize edges
	//minimize();
	// update node ids
	/*for(int i=0;i<nodes.size();i++)
	 {
	 nodes[i]->id()=i;
	 }*/
	//print();
	//// Set node and edge sizes
	//for(int i=0;i<nodes.size();i++)
	//{
	//	// Find domain size
	//	int domain_size=Variable::getDomainSize(nodes[i]->variables());
	//	nodes[i]->table()=vector<Double>(domain_size);
	//	for(int j=0;j<domain_size;j++)
	//	{
	//		nodes[i]->table()[j]=Double(1.0);
	//	}
	//	for(int j=0;j<nodes[i]->edges().size();j++)
	//	{
	//		int domain_size=Variable::getDomainSize(nodes[i]->edges()[j]->variables());
	//		nodes[i]->edges()[j]->message1()=vector<Double> (domain_size);
	//		nodes[i]->edges()[j]->message2()=vector<Double> (domain_size);
	//		for(int k=0;k<domain_size;k++)
	//		{
	//			nodes[i]->edges()[j]->message1()[k]=Double(1.0);
	//			nodes[i]->edges()[j]->message2()[k]=Double(1.0);
	//		}
	//	}
	//}
	//cerr<<"Now putting functions\n";
	////reduce();
	cout << "Join graph done\n";

}