/* * vertex_cover.cpp : Source c++ code solving "Optimal Vertex Cover Problem" * Implemented by : Psallidas Fotis( using naxos project implemented by Nikos Pothitos) * std code : std06170 * Project Reference: Logic Programming Project 5.0 * */ #include #include #include #include #include extern "C"{ #include #include #include } using namespace std; using namespace naxos; template std::string to_string (const T& t) { /* convert numbers to strings */ std::stringstream ss; ss << t; return ss.str(); } int main (int argc, char *argv[]) { try { NsProblemManager pm; NsIntVarArray Var; int* optimal=NULL; bool firstSolItem; struct stat statbuf; ofstream file; // Checking input arguments. if ( argc != 3 && argc != 4 ) { cerr << "Correct syntax is:\t" << argv[0] << " []\n"; return 1; } // Passing the number of nodes. int N = atoi(argv[1]); // Passing the density. int density = atoi(argv[2]); // Setting the seed. if ( argc == 4 ) srand( atoi(argv[3]) ); // Creating for each node `i' the array `graph_edge[i-1]', which // contains the numbers (`j') of the nodes connected to `i'. NsDeque< NsDeque > graph_edge; int i, j; for (i=1; i <= N; ++i) { graph_edge.push_back( NsDeque() ); for (j=i+1; j <= N; ++j) { if ( 100 * (rand()/(RAND_MAX+1.0)) <= density ) graph_edge[ i-1 ].push_back( j ); } } // Printing the graph representation. bool firstEdge = true; cout << "G = ["; for (i=1; i <= N; ++i) { for (j=0; j < (signed int)graph_edge[i-1].size(); ++j) { if ( firstEdge ) firstEdge = false; else cout << ", "; cout << i << " - " << graph_edge[i-1][j]; } } cout << "]\n"; /* Create Variables List */ for (i=1; i <= N; ++i) { Var.push_back( NsIntVar(pm, 0, 1) ); } /* Add Constraints */ for (i=1; i <= N; ++i){ for (j=0; (unsigned)j < graph_edge[i-1].size(); ++j) pm.add( NsAbs(Var[i-1] + Var[graph_edge[i-1][j]-1]) >= 1); } pm.addGoal( new NsgLabeling(Var) ); pm.minimize(NsSum(Var)); optimal = new int[N]; while (pm.nextSolution() != false){ for(i = 0 ; i < N ; i++){ if(Var[i].value()==1) optimal[i]=1; else optimal[i]=0; } } firstSolItem = true; cout<<"Optimal Vetrex Cover: ["; for(i = 0 ; i < N; i++){ if(optimal[i]==1){ if ( firstSolItem ) firstSolItem = false; else cout << ", "; cout<c_str(),&statbuf) != -1 ){ *filename = "solution" + to_string(k) + ".txt"; k++; } file.open(filename->c_str()); firstEdge = true; file << "G = ["; for (i=1; i <= N; ++i) { for (j=0; j < (signed int)graph_edge[i-1].size(); ++j) { if ( firstEdge ) firstEdge = false; else file << ", "; file << i << " - " << graph_edge[i-1][j]; } } file << "]\n"; firstSolItem = true; file<<"C = ["; for(i = 0 ; i < N; i++){ if(optimal[i]==1){ if ( firstSolItem ) firstSolItem = false; else file << ", "; file<(N).c_str(),output_filename.c_str(),NULL); perror("Execlp error"); exit(EXIT_FAILURE); }else{ close(fd[0]); dup2(fd[1],1); close(fd[1]); execlp("/bin/cat","cat",filename->c_str(),NULL); perror("Execlp error"); exit(EXIT_FAILURE); } case -1: exit(EXIT_FAILURE); default: wait(&status); } pid3 = fork(); switch(pid3){ case 0: execlp("/usr/bin/X11/eog","eog",output_filename.c_str(),NULL); perror("Execlp error"); exit(EXIT_FAILURE); case -1: cerr<<"Internal error"<