예제 #1
0
void dump_vertex_attribute_numeric(const char* name, const igraph_t* g) {
  long int i, n = igraph_vcount(g);

  printf("Vertex attribute '%s':", name);
  for (i = 0; i < n; i++) {
    printf(" %g", (float)VAN(g, name, i));
  }
  printf("\n");
}
예제 #2
0
파일: pajek2.c 프로젝트: CansenJIANG/igraph
int main() {
  igraph_t g;
  FILE *ifile;
  int i, n;

  /* turn on attribute handling */
  igraph_i_set_attribute_table(&igraph_cattribute_table);  

  ifile=fopen("bipartite.net", "r");
  if (!ifile) { return 5; }
  igraph_read_graph_pajek(&g, ifile);
  fclose(ifile);
  if (igraph_vcount(&g) != 13 || igraph_ecount(&g) != 11 ||
      igraph_is_directed(&g)) { return 6; }

  for (i=0, n=igraph_vcount(&g); i<n; i++) {
    printf("%i ", (int) VAN(&g, "type", i));
  }

  igraph_destroy(&g);  


  return 0;
}
예제 #3
0
파일: utils.c 프로젝트: lccanon/ggen
int ggen_write_graph(igraph_t *g, FILE *output)
{
	Agraph_t *cg;
	Agnode_t *f,*t;
	Agedge_t *edge;
	igraph_vector_ptr_t vertices;
	igraph_eit_t eit;
	int err;
	unsigned long i,j;
	unsigned long vcount = igraph_vcount(g);
	igraph_integer_t from,to;
	char name[GGEN_DEFAULT_NAME_SIZE];
	char *str = NULL;
	igraph_strvector_t gnames,vnames,enames;
	igraph_vector_t gtypes,vtypes,etypes;
	Agsym_t *attr;
	/* see warning below */
	igraph_error_handler_t *error_handler;

	err = igraph_vector_ptr_init(&vertices,vcount);
	if(err) return 1;

	/* WARNING: this should be changed if igraph-0.6 gets
	 * stable.
	 * We need to ignore some igraph_cattribute errors
	 * because we try to retrieve special attributes (ggen specifics).
	 * igraph version 0.6 include a cattribute_has_attr that should be
	 * used instead of ignoring errors.
	 */
	error_handler = igraph_set_error_handler(igraph_error_handler_ignore);

	/* open graph
	 * its name is saved in __ggen_graph_name if it exists
	 */
	str =(char *) GAS(g,GGEN_GRAPH_NAME_ATTR);
	if(!str)
		cg = agopen(GGEN_DEFAULT_GRAPH_NAME,Agdirected,NULL);
	else
		cg = agopen(str,Agdirected,NULL);

	if(!cg)
	{
		err = 1;
		goto d_v;
	}

	/* save a pointer to each vertex */
	for(i = 0; i < vcount; i++)
	{
		/* find a vertex name */
		str = vid2vname_unsafe(name,g,i);
		if(!str)
			f = agnode(cg,name,1);
		else
			f = agnode(cg,str,1);
		VECTOR(vertices)[i] = (void *)f;
	}

	/* We have finished with dangerous attributes accesses */
	igraph_set_error_handler(error_handler);

	/* now loop through edges in the igraph */
	err = igraph_eit_create(g,igraph_ess_all(IGRAPH_EDGEORDER_ID),&eit);
	if(err) goto c_ag;

	for(IGRAPH_EIT_RESET(eit); !IGRAPH_EIT_END(eit); IGRAPH_EIT_NEXT(eit))
	{
		err = igraph_edge(g,IGRAPH_EIT_GET(eit),&from,&to);
		if(err) goto d_eit;

		f = (Agnode_t *) VECTOR(vertices)[(unsigned long)from];
		t = (Agnode_t *) VECTOR(vertices)[(unsigned long)to];
		agedge(cg,f,t,NULL,1);
	}

	/* find all properties */
	igraph_strvector_init(&gnames,1);
	igraph_strvector_init(&vnames,vcount);
	igraph_strvector_init(&enames,igraph_ecount(g));
	igraph_vector_init(&gtypes,1);
	igraph_vector_init(&vtypes,vcount);
	igraph_vector_init(&etypes,igraph_ecount(g));

	err = igraph_cattribute_list(g,&gnames,&gtypes,&vnames,&vtypes,&enames,&etypes);
	if(err) goto d_eit;

	/* add graph properties */
	for(i = 0; i < igraph_strvector_size(&gnames); i++)
	{
		if(strcmp(GGEN_GRAPH_NAME_ATTR,STR(gnames,i)))
		{
			if(VECTOR(gtypes)[i]==IGRAPH_ATTRIBUTE_NUMERIC) {
				snprintf(name,GGEN_DEFAULT_NAME_SIZE,"%f",
						(double)GAN(g,STR(gnames,i)));
				agattr(cg,AGRAPH,(char *)STR(gnames,i),name);
			}
			else
				agattr(cg,AGRAPH,(char *)STR(gnames,i),
						(char *)GAS(g,STR(gnames,i)));
		}
	}

	/* add vertex properties */
	for(i = 0; i < igraph_strvector_size(&vnames); i++)
	{
		if(strcmp(GGEN_VERTEX_NAME_ATTR,STR(vnames,i)))
		{
			/* creates the attribute but we still need to set it for each vertex */
			attr = agattr(cg,AGNODE,(char *)STR(vnames,i),GGEN_CGRAPH_DEFAULT_VALUE);
			for(j = 0; j < vcount; j++)
			{
				f = (Agnode_t *) VECTOR(vertices)[j];
				if(VECTOR(vtypes)[i]==IGRAPH_ATTRIBUTE_NUMERIC) {
					snprintf(name,GGEN_DEFAULT_NAME_SIZE,"%f",
							(double)VAN(g,STR(vnames,i),j));
					agxset(f,attr,name);
				}
				else
					agxset(f,attr,(char *)VAS(g,STR(vnames,i),j));
			}
		}
	}

	/* add edges properties */
	for(i = 0; i < igraph_strvector_size(&enames); i++)
	{
		/* creates the attribute but we still need to set it for each edge */
		attr = agattr(cg,AGEDGE,(char *)STR(enames,i),GGEN_CGRAPH_DEFAULT_VALUE);
		for(j = 0; j < igraph_ecount(g); j++)
		{
			igraph_edge(g,j,&from,&to);
			f = (Agnode_t *) VECTOR(vertices)[(unsigned long)from];
			t = (Agnode_t *) VECTOR(vertices)[(unsigned long)to];
			edge = agedge(cg,f,t,NULL,0);
			if(VECTOR(etypes)[i]==IGRAPH_ATTRIBUTE_NUMERIC) {
				snprintf(name,GGEN_DEFAULT_NAME_SIZE,"%f",
						(double)EAN(g,STR(enames,i),j));
				agxset(edge,attr,name);
			}
			else
				agxset(edge,attr,(char *)EAS(g,STR(enames,i),j));
		}
	}

	/* write the graph */
	err = agwrite(cg,(void *)output);
d_eit:
	igraph_eit_destroy(&eit);
c_ag:
	agclose(cg);
d_v:
	igraph_vector_ptr_destroy(&vertices);
	return err;
}
예제 #4
0
/*****************************************************************************
 * Main
 ****************************************************************************/
int main(int argc, char **argv) {

    srand ( time(NULL) );
       
    /******************************
     * Command Line Parser
     ******************************/
         
    bool mono_lib(false), print_gate(false), print_solns(false), print_sat(false), print_blif(false), print_verilog(false);
    int num_threads, budget;
    float remove_percent(0.6);
    vector<string> test_args;
    
    po::options_description optional_args("Usage: [options]");
    optional_args.add_options()
        ("circuit",         po::value<string>(),                                    "input circuit")
        ("tech_lib",        po::value<string>(),                                    "tech library")
        ("continue_file,c", po::value<string>(),                                    "input report to continue from")
        ("mono_lib,m",      po::value(&mono_lib)->zero_tokens(),                    "treat each gate as a nand")
        ("budget,b",        po::value<int>(&budget)->default_value(100000000),      "minisat conflict budget (-1 = unlimited)")
        ("remove_edges,e",  po::value<float>(&remove_percent)->default_value(0.6),  "edge percent to remove")
        ("print_graph",     po::value(&print_gate)->zero_tokens(),                  "print H graph")
        ("print_solns",     po::value(&print_solns)->zero_tokens(),                 "print isos")
        ("print_blif",      po::value(&print_blif)->zero_tokens(),                  "print G blif circuit")
        ("print_verilog",   po::value(&print_verilog)->zero_tokens(),               "print verilog circuits")
        ("test,t",          po::value< vector<string> >(&test_args),                 "Run test suite #: \n"
                                                                                    "  -99: Beta Testing \n"
                                                                                    "  -1: Mem Testing \n"
                                                                                    "   0: L0 [count, remove_percent] \n"
                                                                                    "   1: L1 [count, remove_percent] \n"
                                                                                    "   2: S1 - Random [min S1] \n"
                                                                                    "   3: S1 - Greedy [min S1] \n")
        ("num_threads,n",   po::value<int>(&num_threads)->default_value(1),         "Number of threads")
        ("wdir,w",          po::value<string>()->default_value("./wdir"),           "Output directory")
        ("help,h",                                                                  "produce help message")
    ;

    po::positional_options_description positional_args;
    positional_args.add("circuit", 1);
    positional_args.add("tech_lib", 1);
    positional_args.add("test", -1);
    po::variables_map vm;

    try {
        po::store(po::command_line_parser(argc, argv).options(optional_args).positional(positional_args).run(), vm);
        po::notify(vm);
    } catch(exception& e) {
        cerr << "error: " << e.what() << "\n";
        return 1;
    } catch(...) {
        cerr << "Exception of unknown type!\n";
    }

    if (vm.count("help")) {
        cout << optional_args << "\n";
        return 0;
    }

    string circuit_filename, circuit_name, tech_filename, tech_name, working_dir, report_filename;
   
    // input circuit
    if (vm.count("circuit") == 1) {
        circuit_filename = vm["circuit"].as<string>();
        circuit_name = circuit_filename.substr(circuit_filename.rfind('/')+1);
        circuit_name = circuit_name.substr(0, circuit_name.find('.'));
    } else {
        cout << optional_args << "\n";
        return 0;
    }

    // input tech lib
    if (vm.count("tech_lib")) {
        tech_filename = vm["tech_lib"].as<string>();
        tech_name = tech_filename.substr(tech_filename.rfind('/')+1);
        tech_name = tech_name.substr(0, tech_name.find('.'));
    } else {
        cout << optional_args << "\n";
        return 0;
    }

    cout << "Circuit : " << circuit_filename << "\n";
    cout << "Tech Lib: " << tech_filename << "\n";

   
    /******************************
     * Setup working dir
     ******************************/
    working_dir = vm["wdir"].as<string>();
    mkdir(working_dir.c_str(), S_IRWXU);
    cout << "WDIR    : " << working_dir << "\n";


    /******************************
     * Convert circuit using tech_lib
     ******************************/
    string outfile = working_dir + circuit_filename.substr(circuit_filename.rfind('/'));
    cout << "Outfile : " << outfile << "\n";
   // sis_convert(circuit_filename, tech_filename, outfile);
cout << "I'm here!\n";
    circuit_filename = outfile;
    // copy tech_lib
    if ( tech_filename != string(working_dir + tech_filename.substr(tech_filename.rfind('/')) ) ) {
        ifstream src( tech_filename.c_str() );
        ofstream dst( string(working_dir + tech_filename.substr(tech_filename.rfind('/')) ).c_str() );
        dst << src.rdbuf();
        dst.close();
    }

   
    /******************************
     * Load circuit
     ******************************/
    Circuit circuit;
    load_circuit(&circuit, circuit_filename, mono_lib);
    
    Security *security;
    Circuit G, H;
    G.copy(&circuit);
    G.remove_io();

    circuit.save( working_dir + "/circuit.gml" );
    G.save( working_dir + "/G_circuit.gml" );


    /****************************************************************
     * print G
     ****************************************************************/
    if ( test_args.size() > 0 && -1 == atoi(test_args[0].c_str())) {
         G.print();
    }


    /****************************************************************
     * L0
     ****************************************************************/
    if ( test_args.size() > 0 && 0 == atoi(test_args[0].c_str())) {

        int max_count(2);
        if (test_args.size() == 2)
            max_count = atoi(test_args[1].c_str());

        H.copy(&G);
        H.rand_del_edges(remove_percent);
        H.save( working_dir + "/H_circuit.gml" );

        security = new Security(&G, &H);
        security->setConfBudget(budget);

        cout << "Rand L0: |V(G)| = "  << (int) igraph_vcount(&G);
        cout << ", |E(G)| = "         << (int) igraph_ecount(&G);
        cout << ", |V(H)| = "         << (int) igraph_vcount(&H);
        cout << ", |E(H)| = "         << (int) igraph_ecount(&H) << "\n";
        
        cout << " " << boolalpha << security->L0(max_count, false) << endl;
    }



    /****************************************************************
     * L1
     ****************************************************************/
    if ( test_args.size() > 0 && 1 == atoi(test_args[0].c_str())) {

        int max_L1(2);
        if (test_args.size() == 2)
            max_L1 = atoi(test_args[1].c_str());

        H.copy(&G);
        H.rand_del_edges(remove_percent);
        
        if (vm.count("continue_file")) {
            H.rand_del_edges((float) 1.0);
            string filename = vm["continue_file"].as<string>();
            ifstream file;
            try {
                file.open(filename.c_str());
                
                while (file.good()) {
                    string line;
                    int L0, L1;
                    Edge edge;

                    getline(file, line);
                    if (parse(line, &G, L1, L0, edge)) {
                        if (L1 >= max_L1) {
                            H.add_edge(edge);
                            cout << "L1 = " << max_L1 << ", +<" << edge.first << "," << edge.second << ">" << endl;
                        }
                    }
                }
            } catch(...) {}
        }

        H.save( working_dir + "/H_circuit.gml" );

        security = new Security(&G, &H);
        security->setConfBudget(budget);

        cout << "Rand L1: |V(G)| = "  << (int) igraph_vcount(&G);
        cout << ", |E(G)| = "         << (int) igraph_ecount(&G);
        cout << ", |V(H)| = "         << (int) igraph_vcount(&H);
        cout << ", |E(H)| = "         << (int) igraph_ecount(&H) << "\n";
        
        cout << " " << boolalpha << security->L1(max_L1, false) << endl;
    }



    /****************************************************************
     * #L1
     ****************************************************************/
    if ( test_args.size() == 1 && 2 == atoi(test_args[0].c_str())) {

        int max_L1(2);
        if (test_args.size() == 2)
            max_L1 = atoi(test_args[1].c_str());

        H.copy(&G);
        H.rand_del_edges(remove_percent);

        if (vm.count("continue_file")) {
            H.rand_del_edges((float) 1.0);
            string filename = vm["continue_file"].as<string>();
            ifstream file;
            try {
                file.open(filename.c_str());
                
                while (file.good()) {
                    string line;
                    int L0, L1;
                    Edge edge;

                    getline(file, line);
                    if (parse(line, &G, L1, L0, edge)) {
                        H.add_edge(edge);
                        max_L1 = L1;
                        cout << "L1 = " << max_L1 << ", +<" << edge.first << "," << edge.second << ">" << endl;
                    }
                }
            } catch(...) {}
        }

        H.save( working_dir + "/H_circuit.gml" );

        security = new Security(&G, &H);
        security->setConfBudget(budget);

        cout << "Rand L1: |V(G)| = "  << (int) igraph_vcount(&G);
        cout << ", |E(G)| = "         << (int) igraph_ecount(&G);
        cout << ", |V(H)| = "         << (int) igraph_vcount(&H);
        cout << ", |E(H)| = "         << (int) igraph_ecount(&H) << "\n";
        cout << " " << security->L1(false);
    }



    /****************************************************************
     * S1_rand
     ****************************************************************/
    if ( test_args.size() == 1 && 3 == atoi(test_args[0].c_str())) {

        int max_L1 = G.max_L1();
        H.copy(&G);
        H.rand_del_edges((float) 1.0);

        security = new Security(&G, &H);
        security->setConfBudget(budget);

        string output;
        output = "S1_rand ("  + G.get_name() + ")";
        output = report(output, &G, &H, max_L1);
        cout << output;

        security->S1_rand(num_threads);
    }



    /****************************************************************
     * S1_greedy
     ****************************************************************/
    if ( test_args.size() >= 1 && 4 == atoi(test_args[0].c_str())) {

        int min_L1(2), max_L1 = G.max_L1();
        H.copy(&G);
        H.rand_del_edges((float) 1.0);
        bool done(false);
        
        if ( test_args.size() == 3 ) {
            min_L1 = atoi(test_args[1].c_str());
            max_L1 = atoi(test_args[2].c_str());
        } else if ( test_args.size() == 2 )
            min_L1 = atoi(test_args[1].c_str());

        if (vm.count("continue_file")) {
            string filename = vm["continue_file"].as<string>();
            ifstream file;
		
		for (int eid = 0; eid < igraph_ecount(&G); eid++)
			SETEAS(&G, "style", eid, "invis");

            try {
                file.open(filename.c_str());
                
//		int last_sec;
//		Edge prev_edge;
//		bool first = true;
                while (file.good()) {
                    string line;
                    int L0, L1;
                    Edge edge;

                    getline(file, line);
		    
		
                    if (parse(line, &G, L1, L0, edge)) {
                        if (L1 < min_L1) {
                            done = true;
                            break;
                        }

//			if (first) { last_sec = L1; first = false; prev_edge = edge;}
//			else
//			{
//				if (last_sec != L1)
//				{

					
//					H.del_edge(prev_edge);
//					string gmlfilename;
//					char num[3];
//					sprintf(num, "%d", last_sec);		
//					gmlfilename = working_dir + "/H_circuit_" + num + ".gml";
//					string gvfilename = working_dir + "/H_circuit_" + num + ".gv";
//					string psfilename = working_dir + "/H_circuit_" + num + ".ps";					
					//H.save( gmlfilename );
//					G.save( gmlfilename );

//					H.add_edge(prev_edge);
//					string command = "gml2gv -o" + gvfilename + " " + gmlfilename;
//					system(command.c_str());
//					command = "dot -Tps -Nstyle=filled -Ncolorscheme=accent8 -Nshape=circle -Nlabel=\"\" -o " + psfilename + " " + gvfilename;
//					system(command.c_str());
//					last_sec = L1;
//				}
//				prev_edge = edge;
//				int eid;
//				igraph_get_eid(&G, &eid, edge.first, edge.second, true, false);
//				SETEAS(&G, "style", eid, "solid");

//			}
			
				
                        H.add_edge(edge);
                        max_L1 = L1;
                        cout << "L1 = " << max_L1 << ", +<" << edge.first << "," << edge.second << ">" << endl;
                    }
                }
//		return 0;
            } catch(...) {}
        }

        if ( test_args.size() == 3 ) {
            min_L1 = atoi(test_args[1].c_str());
            max_L1 = atoi(test_args[2].c_str());
        } else if ( test_args.size() == 2 )
            min_L1 = atoi(test_args[1].c_str());

        security = new Security(&G, &H);
        security->setConfBudget(budget);
	
        string output;
        output = "S1_greedy ("  + G.get_name() + ")";
        output = report(output, &G, &H, max_L1);
        cout << output;

        fstream report;
        if (!done)
	{
		clock_t tic = clock();
            security->S1_greedy(num_threads, min_L1, max_L1);
		clock_t toc = clock();
          cout << endl << "Heuristic took: ";
          cout << (double) (toc-tic)/CLOCKS_PER_SEC << endl;
	}

    }


    /****************************************************************
     * k-isomorphism
     ****************************************************************/
    if ( test_args.size() >= 1 && 10 == atoi(test_args[0].c_str())) {

        int min_L1(2), max_L1 = G.max_L1();

	if ( test_args.size() == 3 ) {
            min_L1 = atoi(test_args[1].c_str());
            max_L1 = atoi(test_args[2].c_str());
        } else if ( test_args.size() == 2 )
            min_L1 = atoi(test_args[1].c_str());

	igraph_vector_t match_vert;
	igraph_vector_init(&match_vert, 0);
	for (int i = 0; i < igraph_vcount(&G); i++)
	{
			int color = VAN(&G, "colour", i);
			if (igraph_vector_size(&match_vert) < color + 1)
				for (int j = igraph_vector_size(&match_vert); j <= color; j++)
				{
					igraph_vector_push_back(&match_vert, 0);
				}
			VECTOR(match_vert)[color]++;
	}

	igraph_t temp;
//	igraph_copy(&temp, &G);
//	igraph_destroy(&G);
//	for (min_L1 = max_L1; min_L1 >= 1; min_L1--) {
//		igraph_copy(&G, &temp);
	int count = 0;
	for (int i = 0; i < igraph_vector_size(&match_vert); i++)
	{
		int n = ((int) VECTOR(match_vert)[i]) % min_L1; if (n == 0) continue;
		for (int j = 0; j < min_L1 - n; j++)
		{ count++;
			igraph_add_vertices(&G, 1, 0);
			SETVAN(&G, "colour", igraph_vcount(&G) - 1, i);
		}
	}
	
cout << count << " "; cout.flush();
        H.copy(&G);
        H.rand_del_edges((float) 1.0);
        bool done(false);
        
        security = new Security(&G, &H);
        security->setConfBudget(budget);
	
        string output;
        output = "S1_greedy ("  + G.get_name() + ")";
        output = report(output, &G, &H, max_L1);
//        cout << output;

        fstream report;
        if (!done)
	{
		clock_t tic = clock();
            security->kiso(min_L1, max_L1);
		clock_t toc = clock();
  //        cout << endl << "Heuristic took: ";
   //       cout << (double) (toc-tic)/CLOCKS_PER_SEC << endl;
	} //igraph_destroy(&G);}

    }

/****************************************************************
     * Tree test
     ****************************************************************/
    if ( test_args.size() >= 1 && 7 == atoi(test_args[0].c_str())) {

        int min_L1(2), max_L1 = G.max_L1();

	//G.erase();
	igraph_vs_t vs;
	igraph_vs_all(&vs);

	igraph_delete_vertices(&G, vs);
	const int depth = 7;
	igraph_add_vertices(&G, pow(2,depth)-1, 0);
	for (int i=0; i < pow(2,depth-1); i++)
	{
		int level = floor(log(i+1)/log(2));
		igraph_add_edge(&G,i,pow(2,level+1) + (i-pow(2,level))*2 - 1);
		igraph_add_edge(&G,i,pow(2,level+1) + (i-pow(2,level))*2);
	}	

	for (int i=0; i < igraph_vcount(&G); i++)
	{
		SETVAN(&G, "colour", i, 0);
		SETVAS(&G, "type", i, "invf101");
		string label = "label";
		SETVAS(&G, "label", i, label.c_str());
	}

        H.copy(&G);
	for (int i=0; i < igraph_vcount(&H); i++)
	{
		SETVAN(&H, "colour", i, 0);
	}

        H.rand_del_edges((float) 1.0);
        
        if ( test_args.size() == 3 ) {
            min_L1 = atoi(test_args[1].c_str());
            max_L1 = atoi(test_args[2].c_str());
        } else if ( test_args.size() == 2 )
            min_L1 = atoi(test_args[1].c_str());


        if ( test_args.size() == 3 ) {
            min_L1 = atoi(test_args[1].c_str());
            max_L1 = atoi(test_args[2].c_str());
        } else if ( test_args.size() == 2 )
            min_L1 = atoi(test_args[1].c_str());

	cout << "I'm here!"; cout.flush();
        security = new Security(&G, &H);
        security->setConfBudget(budget);
	
        string output;
	cout << "I'm here!";
        output = "S1_greedy ("  + G.get_name() + ")";
	cout << "I'm here!";
        output = report(output, &G, &H, max_L1);
        cout << output;

	cout << "I'm here!";
        security->S1_greedy(num_threads, min_L1, max_L1);

    }

/****************************************************************
     * Compute security level G if no wires are lifted
     ****************************************************************/
    if ( test_args.size() >= 1 && 5 == atoi(test_args[0].c_str())) {

        H.copy(&G);
        security = new Security(&G, &H);
        security->setConfBudget(budget);
   H.rand_del_edges((float) 0.0);

//	security->clean_solutions();

        string output;
        output = "Security of circuit ("  + G.get_name() + ") if no wires are lifted: ";
        cout << output;
        
        security->S1_self();

    }



/****************************************************************
     * Solve LIFT(G, k, eta)
     ****************************************************************/

if ( test_args.size() >= 1 && 6 == atoi(test_args[0].c_str())) {

        int min_L1(2), max_L1 = G.max_L1(), eta = igraph_ecount(&G);
	H.copy(&G);
//        H.rand_del_edges((float) 1.0);
        
        if ( test_args.size() == 3 ) {
            min_L1 = atoi(test_args[1].c_str());
            eta = atoi(test_args[2].c_str());
        } else if ( test_args.size() == 2 )
            min_L1 = atoi(test_args[1].c_str());

        security = new Security(&G, &H);
        security->setConfBudget(budget);
	
    
	clock_t tic = clock();
	security->rSAT(min_L1, max_L1, eta);
	clock_t toc = clock();
	cout << endl << "SAT took: ";
	cout << (double) (toc-tic)/CLOCKS_PER_SEC << endl;

    }

/****************************************************************
     * Solve LIFT(G, k, eta, u)
     ****************************************************************/

if ( test_args.size() >= 1 && 8 == atoi(test_args[0].c_str())) {

        int min_L1(2), max_L1 = G.max_L1(), eta = igraph_ecount(&G), u;
	H.copy(&G);
//        H.rand_del_edges((float) 1.0);
        
        if ( test_args.size() == 4 ) {
		u = atoi(test_args[1].c_str());
            min_L1 = atoi(test_args[2].c_str());
            eta = atoi(test_args[3].c_str());
        } else if ( test_args.size() == 3 )
	{
		u = atoi(test_args[1].c_str());
            min_L1 = atoi(test_args[2].c_str());
} 
else if ( test_args.size() == 2 )
		u = atoi(test_args[1].c_str());

        security = new Security(&G, &H);
        security->setConfBudget(budget);
	
    
	clock_t tic = clock();
	security->rSAT(min_L1, max_L1, eta, u, true);
	clock_t toc = clock();
	cout << endl << "SAT took: ";
	cout << (double) (toc-tic)/CLOCKS_PER_SEC << endl;

    }

/****************************************************************
     * Solve LIFT(G, k, eta, u)
     ****************************************************************/

if ( test_args.size() >= 1 && 9 == atoi(test_args[0].c_str())) {

        int min_L1(2), max_L1 = G.max_L1(), eta = igraph_ecount(&G), u;
	H.copy(&G);
//        H.rand_del_edges((float) 1.0);
        
        if ( test_args.size() == 4 ) {
		u = atoi(test_args[1].c_str());
            min_L1 = atoi(test_args[2].c_str());
            eta = atoi(test_args[3].c_str());
        } else if ( test_args.size() == 3 )
	{
		u = atoi(test_args[1].c_str());
            min_L1 = atoi(test_args[2].c_str());
} 
else if ( test_args.size() == 2 )
		u = atoi(test_args[1].c_str());

        security = new Security(&G, &H);
        security->setConfBudget(budget);
	
    
	clock_t tic = clock();
	security->rSAT(min_L1,max_L1, eta, u);
	clock_t toc = clock();
	cout << endl << "SAT took: ";
	cout << (double) (toc-tic)/CLOCKS_PER_SEC << endl;

    }

/****************************************************************
     * simulated annealing
     ****************************************************************/


if ( test_args.size() >= 1 && 10 == atoi(test_args[0].c_str())) {
    const double MAX_TEMP = 100000.0;
    const int MAX_ITERATIONS = 2000;
    const double TEMP_CHANGE = 0.98;
    int no_of_edges = 20;
    int min_L1(2), max_L1 = G.max_L1();
    H.copy(&G);
    H.rand_del_edges(igraph_ecount(&G) - no_of_edges);
    security = new Security(&G, &H);
    security->setConfBudget(budget);
    int current_k_security = security->L1(); 
    int best_k_security = current_k_security;
    delete security;
    double temperature = MAX_TEMP;
    srand( time(NULL));

    for (int iter = 0; iter < MAX_ITERATIONS; iter++) {
        bool done(false);
        
        // H.rand_del_edges(1);
        vector<Edge> unlifted_edge_list;
        for (int eid = 0; eid < igraph_ecount(&H); eid++) {
            Edge edge = H.get_edge(eid);
            unlifted_edge_list.push_back(edge);
        }
        random_shuffle(unlifted_edge_list.begin(), unlifted_edge_list.end());
        H.del_edge(unlifted_edge_list[0]);

 
        vector<Edge> edge_list;
        for (int eid = 0; eid < igraph_ecount(&G); eid++) {
            Edge edge = G.get_edge(eid);
            if (!H.test_edge(edge)) edge_list.push_back(edge);
        }
        random_shuffle(edge_list.begin(), edge_list.end());
        H.add_edge(edge_list[0]);

        security = new Security(&G, &H);
        security->setConfBudget(budget);
	
        int new_k_security = security->L1();
        if (new_k_security >= current_k_security) { 
            current_k_security = new_k_security;
            if (current_k_security >= best_k_security) best_k_security = current_k_security;
        }
        else {
            if (exp((new_k_security-current_k_security)/temperature) >= ((double) rand())/ RAND_MAX);
            else {
                H.add_edge(unlifted_edge_list[0]);
                H.add_edge(edge_list[0]);
            }
        }
        temperature *= TEMP_CHANGE;
        if ((iter + 1 )% 10 == 0) cout << " > iteration " << iter + 1 << ", temp=" << temperature << ", best=" << best_k_security << endl;             
    }
}



    /****************************************************************
     * L1(label)
     ****************************************************************/
    if ( test_args.size() >= 1 && 5 == atoi(test_args[0].c_str())) {

        string label = "";
        if (test_args.size() == 2)
            label = test_args[1];

        int max_L1(2);
        H.copy(&G);
        H.rand_del_edges(remove_percent);
        
        if (vm.count("continue_file")) {
            H.rand_del_edges((float) 1.0);
            string filename = vm["continue_file"].as<string>();
            ifstream file;
            try {
                file.open(filename.c_str());
                
                while (file.good()) {
                    string line;
                    int L0, L1;
                    Edge edge;

                    getline(file, line);
                    if (parse(line, &G, L1, L0, edge)) {
                        H.add_edge(edge);
                        max_L1 = L1;
                        cout << "L1 = " << max_L1 << ", +<" << edge.first << "," << edge.second << ">" << endl;
                    }
                }
            } catch(...) {}
        }

        H.save( working_dir + "/H_circuit.gml" );

        security = new Security(&G, &H);
        security->setConfBudget(budget);

        security->L1(label);       
    }


    if ( test_args.size() >= 1 && atoi(test_args[0].c_str()) >= 0) {
        H.save( working_dir + "/H_circuit.gml" );
        delete security;
    }

    if (print_gate)
        G.print();

    if (print_blif)
        print_file(circuit_filename);
        
    if (print_solns)
        security->print_solutions();

    if (print_verilog)
        security->print_solutions();


    printf("\n\ndone 0 \n");
    return 0;
}
예제 #5
0
int print_attributes(const igraph_t *g) {

  igraph_vector_t gtypes, vtypes, etypes;
  igraph_strvector_t gnames, vnames, enames;
  long int i;

  igraph_vector_t vec;
  igraph_strvector_t svec;
  long int j;

  igraph_vector_init(&gtypes, 0);
  igraph_vector_init(&vtypes, 0);
  igraph_vector_init(&etypes, 0);
  igraph_strvector_init(&gnames, 0);
  igraph_strvector_init(&vnames, 0);
  igraph_strvector_init(&enames, 0);

  igraph_cattribute_list(g, &gnames, &gtypes, &vnames, &vtypes, 
			 &enames, &etypes);

  /* Graph attributes */
  for (i=0; i<igraph_strvector_size(&gnames); i++) {
    printf("%s=", STR(gnames, i));
    if (VECTOR(gtypes)[i]==IGRAPH_ATTRIBUTE_NUMERIC) {
      igraph_real_printf(GAN(g, STR(gnames,i)));
      putchar(' ');
    } else {
      printf("\"%s\" ", GAS(g, STR(gnames,i)));
    }
  }
  printf("\n");

  for (i=0; i<igraph_vcount(g); i++) {
    long int j;
    printf("Vertex %li: ", i);
    for (j=0; j<igraph_strvector_size(&vnames); j++) {
      printf("%s=", STR(vnames, j));
      if (VECTOR(vtypes)[j]==IGRAPH_ATTRIBUTE_NUMERIC) {
	igraph_real_printf(VAN(g, STR(vnames,j), i));
	putchar(' ');
      } else {
	printf("\"%s\" ", VAS(g, STR(vnames,j), i));
      }
    }
    printf("\n");
  }

  for (i=0; i<igraph_ecount(g); i++) {
    long int j;
    printf("Edge %li (%i-%i): ", i, (int)IGRAPH_FROM(g,i), (int)IGRAPH_TO(g,i));
    for (j=0; j<igraph_strvector_size(&enames); j++) {
      printf("%s=", STR(enames, j));
      if (VECTOR(etypes)[j]==IGRAPH_ATTRIBUTE_NUMERIC) {
	igraph_real_printf(EAN(g, STR(enames, j), i));
	putchar(' ');
      } else {
	printf("\"%s\" ", EAS(g, STR(enames, j), i));
      }
    }
    printf("\n");
  }

  /* Check vector-based query functions */
  igraph_vector_init(&vec, 0);
  igraph_strvector_init(&svec, 0);
  
  for (j=0; j<igraph_strvector_size(&vnames); j++) {
    if (VECTOR(vtypes)[j]==IGRAPH_ATTRIBUTE_NUMERIC) {
      igraph_cattribute_VANV(g, STR(vnames, j), igraph_vss_all(), &vec);
      for (i=0; i<igraph_vcount(g); i++) {
	igraph_real_t num=VAN(g, STR(vnames, j), i);
	if (num != VECTOR(vec)[i] &&
	    (!isnan(num) || !isnan(VECTOR(vec)[i]))) {
	  exit(51);
	}
      }
    } else {
      igraph_cattribute_VASV(g, STR(vnames, j), igraph_vss_all(), &svec);
      for (i=0; i<igraph_vcount(g); i++) {
	const char *str=VAS(g, STR(vnames, j), i);
	if (strcmp(str,STR(svec, i))) {
	  exit(52);
	}
      }
    }
  }

  for (j=0; j<igraph_strvector_size(&enames); j++) {
    if (VECTOR(etypes)[j]==IGRAPH_ATTRIBUTE_NUMERIC) {
      igraph_cattribute_EANV(g, STR(enames, j), 
			     igraph_ess_all(IGRAPH_EDGEORDER_ID), &vec);
      for (i=0; i<igraph_ecount(g); i++) {
	igraph_real_t num=EAN(g, STR(enames, j), i);
	if (num != VECTOR(vec)[i] && 
	    (!isnan(num) || !isnan(VECTOR(vec)[i]))) {
	  exit(53);
	}
      }
    } else {
      igraph_cattribute_EASV(g, STR(enames, j), 
			     igraph_ess_all(IGRAPH_EDGEORDER_ID), &svec);
      for (i=0; i<igraph_ecount(g); i++) {
	const char *str=EAS(g, STR(enames, j), i);
	if (strcmp(str,STR(svec, i))) {
	  exit(54);
	}
      }
    }
  }

  igraph_strvector_destroy(&svec);
  igraph_vector_destroy(&vec);

  igraph_strvector_destroy(&enames);
  igraph_strvector_destroy(&vnames);
  igraph_strvector_destroy(&gnames);
  igraph_vector_destroy(&etypes);
  igraph_vector_destroy(&vtypes);
  igraph_vector_destroy(&gtypes);

  return 0;
}
예제 #6
0
int main() {
  
  igraph_t g, g2;
  FILE *ifile;
  igraph_vector_t gtypes, vtypes, etypes;
  igraph_strvector_t gnames, vnames, enames;
  long int i;
  igraph_vector_t y;
  igraph_strvector_t id;
  char str[20];

  /* turn on attribute handling */
  igraph_i_set_attribute_table(&igraph_cattribute_table);
  
  ifile=fopen("LINKS.NET", "r");
  if (ifile==0) {
    return 10;
  }
  igraph_read_graph_pajek(&g, ifile);
  fclose(ifile);

  igraph_vector_init(&gtypes, 0);
  igraph_vector_init(&vtypes, 0);
  igraph_vector_init(&etypes, 0);
  igraph_strvector_init(&gnames, 0);
  igraph_strvector_init(&vnames, 0);
  igraph_strvector_init(&enames, 0);
  
  igraph_cattribute_list(&g, &gnames, &gtypes, &vnames, &vtypes, 
			 &enames, &etypes);
  
  /* List attribute names and types */
  printf("Graph attributes: ");
  for (i=0; i<igraph_strvector_size(&gnames); i++) {
    printf("%s (%i) ", STR(gnames, i), (int)VECTOR(gtypes)[i]);
  }
  printf("\n");
  printf("Vertex attributes: ");
  for (i=0; i<igraph_strvector_size(&vnames); i++) {
    printf("%s (%i) ", STR(vnames, i), (int)VECTOR(vtypes)[i]);
  }
  printf("\n");
  printf("Edge attributes: ");
  for (i=0; i<igraph_strvector_size(&enames); i++) {
    printf("%s (%i) ", STR(enames, i), (int)VECTOR(etypes)[i]);
  }
  printf("\n");

  print_attributes(&g);

  /* Copying a graph */
  igraph_copy(&g2, &g);
  print_attributes(&g2);
  igraph_destroy(&g2);
  
  /* Adding vertices */
  igraph_add_vertices(&g, 3, 0);
  print_attributes(&g);

  /* Adding edges */
  igraph_add_edge(&g, 1, 1);
  igraph_add_edge(&g, 2, 5);
  igraph_add_edge(&g, 3, 6);
  print_attributes(&g);

  /* Deleting vertices */
  igraph_delete_vertices(&g, igraph_vss_1(1));
  igraph_delete_vertices(&g, igraph_vss_1(4));
  print_attributes(&g);

  /* Deleting edges */
  igraph_delete_edges(&g, igraph_ess_1(igraph_ecount(&g)-1));
  igraph_delete_edges(&g, igraph_ess_1(0));
  print_attributes(&g);

  /* Set graph attributes */
  SETGAN(&g, "id", 10);
  if (GAN(&g, "id") != 10) {
    return 11;
  }
  SETGAS(&g, "name", "toy");
  if (strcmp(GAS(&g, "name"), "toy")) {
    return 12;
  }
  
  /* Delete graph attributes */
  DELGA(&g, "id");
  DELGA(&g, "name");
  igraph_cattribute_list(&g, &gnames, 0,0,0,0,0);
  if (igraph_strvector_size(&gnames) != 0) {
    return 14;
  }  

  /* Delete vertex attributes */
  DELVA(&g, "x");
  DELVA(&g, "shape");
  DELVA(&g, "xfact");
  DELVA(&g, "yfact");
  igraph_cattribute_list(&g, 0,0, &vnames, 0,0,0);  
  if (igraph_strvector_size(&vnames) != 2) {
    return 15;
  }
  
  /* Delete edge attributes */
  igraph_cattribute_list(&g, 0,0,0,0,&enames,0);
  i=igraph_strvector_size(&enames);
  DELEA(&g, "hook1");
  DELEA(&g, "hook2"); 
  DELEA(&g, "label");
  igraph_cattribute_list(&g, 0,0,0,0,&enames,0);
  if (igraph_strvector_size(&enames) != i-3) {
    return 16;
  }
  
  /* Set vertex attributes */
  SETVAN(&g, "y", 0, -1);
  SETVAN(&g, "y", 1, 2.1);
  if (VAN(&g, "y", 0) != -1 || 
      VAN(&g, "y", 1) != 2.1) {
    return 17;
  }
  SETVAS(&g, "id", 0, "foo");
  SETVAS(&g, "id", 1, "bar");
  if (strcmp(VAS(&g, "id", 0), "foo") ||
      strcmp(VAS(&g, "id", 1), "bar")) {
    return 18;
  }

  /* Set edge attributes */
  SETEAN(&g, "weight", 2, 100.0);
  SETEAN(&g, "weight", 0, -100.1);
  if (EAN(&g, "weight", 2) != 100.0 ||
      EAN(&g, "weight", 0) != -100.1) {
    return 19;
  }
  SETEAS(&g, "color", 2, "RED");
  SETEAS(&g, "color", 0, "Blue");
  if (strcmp(EAS(&g, "color", 2), "RED") ||
      strcmp(EAS(&g, "color", 0), "Blue")) {
    return 20;
  }      

  /* Set vector attributes as vector */
  igraph_vector_init(&y, igraph_vcount(&g));
  igraph_vector_fill(&y, 1.23);
  SETVANV(&g, "y", &y);
  igraph_vector_destroy(&y);
  for (i=0; i<igraph_vcount(&g); i++) {    
    if (VAN(&g, "y", i) != 1.23) {
      return 21;
    }
  }
  igraph_vector_init_seq(&y, 0, igraph_vcount(&g)-1);
  SETVANV(&g, "foobar", &y);
  igraph_vector_destroy(&y);
  for (i=0; i<igraph_vcount(&g); i++) {
    if (VAN(&g, "foobar", i) != i) {
      return 22;
    }
  }  
  
  igraph_strvector_init(&id, igraph_vcount(&g));
  for (i=0; i<igraph_vcount(&g); i++) {
    snprintf(str, sizeof(str)-1, "%li", i);
    igraph_strvector_set(&id, i, str);
  }
  SETVASV(&g, "foo", &id);
  igraph_strvector_destroy(&id);
  for (i=0; i<igraph_vcount(&g); i++) {
    printf("%s ", VAS(&g, "foo", i));
  }
  printf("\n");
  igraph_strvector_init(&id, igraph_vcount(&g));
  for (i=0; i<igraph_vcount(&g); i++) {
    snprintf(str, sizeof(str)-1, "%li", i);
    igraph_strvector_set(&id, i, str);
  }
  SETVASV(&g, "id", &id);
  igraph_strvector_destroy(&id);
  for (i=0; i<igraph_vcount(&g); i++) {
    printf("%s ", VAS(&g, "id", i));
  }
  printf("\n");  
  
  /* Set edge attributes as vector */
  igraph_vector_init(&y, igraph_ecount(&g));
  igraph_vector_fill(&y, 12.3);
  SETEANV(&g, "weight", &y);
  igraph_vector_destroy(&y);
  for (i=0; i<igraph_ecount(&g); i++) {    
    if (EAN(&g, "weight", i) != 12.3) {
      return 23;
    }
  }
  igraph_vector_init_seq(&y, 0, igraph_ecount(&g)-1);
  SETEANV(&g, "foobar", &y);
  igraph_vector_destroy(&y);
  for (i=0; i<igraph_ecount(&g); i++) {
    if (VAN(&g, "foobar", i) != i) {
      return 24;
    }
  }  
  
  igraph_strvector_init(&id, igraph_ecount(&g));
  for (i=0; i<igraph_ecount(&g); i++) {
    snprintf(str, sizeof(str)-1, "%li", i);
    igraph_strvector_set(&id, i, str);
  }
  SETEASV(&g, "foo", &id);
  igraph_strvector_destroy(&id);
  for (i=0; i<igraph_ecount(&g); i++) {
    printf("%s ", EAS(&g, "foo", i));
  }
  printf("\n");
  igraph_strvector_init(&id, igraph_ecount(&g));
  for (i=0; i<igraph_ecount(&g); i++) {
    snprintf(str, sizeof(str)-1, "%li", i);
    igraph_strvector_set(&id, i, str);
  }
  SETEASV(&g, "color", &id);
  igraph_strvector_destroy(&id);
  for (i=0; i<igraph_ecount(&g); i++) {
    printf("%s ", EAS(&g, "color", i));
  }
  printf("\n");    

  /* Delete all remaining attributes */
  DELALL(&g);
  igraph_cattribute_list(&g, &gnames, &gtypes, &vnames, &vtypes, &enames, &etypes);
  if (igraph_strvector_size(&gnames) != 0 ||
      igraph_strvector_size(&vnames) != 0 ||
      igraph_strvector_size(&enames) != 0) {
    return 25;
  }

  /* Destroy */
  igraph_vector_destroy(&gtypes);
  igraph_vector_destroy(&vtypes);
  igraph_vector_destroy(&etypes);  
  igraph_strvector_destroy(&gnames);
  igraph_strvector_destroy(&vnames);
  igraph_strvector_destroy(&enames);

  igraph_destroy(&g);

  return 0;
}