Automate::~Automate(){ deleteTableTranstion(); deleteQ(); deleteI(); deleteT(); }
void main() { int queue[MAX]; int front,rear; int n,value; front=0; rear=0; do { do { printf("Enter the element to be inserted\n"); scanf("%d",&value); insert(queue,&rear,front,value); printf("Enter 1 to continue\n"); scanf("%d",&n); } while(n == 1); printf("Enter 1 to delete an element\n"); scanf("%d",&n); while( n == 1) { deleteQ(queue,&front,rear,&value); printf("The value deleted is %d\n",value); printf("Enter 1 to delete an element\n"); scanf("%d",&n); } printf("Enter 1 to continue\n"); scanf("%d",&n); } while(n == 1); }
int main(int argc, char const *argv[]) { Queue *q = createQueue(); for (int i = 0; i < MAX_SIZE; ++i) { addQ(q, i); } for (int i = 0; i < MAX_SIZE; ++i) { deleteQ(q); } return 0; }
main () { int i, M, N, nodeNumber, continueLoop = 1; int tsd, res, optVal,tsItems,tpnamsz, tplength, status; char tpname[128]; double L, ubb, lbb, profit, cap, globalOptimal; NODE *node; ELEM *PbyW; static int itemsCopied = 0; puts("Going to start Worker bfsKnap"); fflush(stdout); tsItems = cnf_open("Items", 0); tsd = cnf_open("Nodes", 0); res = cnf_open("Results", 0); optVal = cnf_open("OptVal", 0); while(1) { puts("WORKER TIME STARTED"); fflush(stdout); startTime = time((long *)0); maxLiveNodes = 1; atLevel = 0; qLive = NULL; endQLive = NULL; numOps = 0; /* get tuple put by master */ strcpy(tpname, "*\0"); tpnamsz = strlen(tpname); tplength = sizeof(struct ituple); status = cnf_tsget(tsd, tpname, &ituple, 0); if (status > 0) { //printItuple(ituple); if(ituple.numItems == 0) { #ifdef DEBUG puts("Worker found last tuple"); fflush(stdout); #endif status = cnf_tsput(tsd, tpname, &ituple,status); cnf_term(); } /* get the global optimal if not the first iteration of while loop*/ if(itemsCopied) { strcpy(tpname, "*"); tpnamsz = strlen(tpname); tplength = sizeof(double); status = cnf_tsread(optVal, tpname, &globalOptimal, 0); if (status > 0) { if (ituple.L < globalOptimal){ tplength = sizeof(struct otuple); otuple.nodeNumber = ituple.nodeNumber; otuple.L = 0; otuple.numOps = numOps; sprintf(tpname, "node%d\0",ituple.nodeNumber); #ifdef DEBUG printf("Worker going to put otuple without processing\n"); fflush(stdout); #endif cnf_tsput(res, tpname, &otuple, tplength); printf("worker fake tp(%s) returned\n",tpname); fflush(stdout); continue; /* to the start of WHILE LOOP */ } } else{ printf("Error in reading global optimal\n"); fflush(stdout); } } /* fill in L etc */ i = ituple.level; L = ituple.L; ubb = ituple.ubb; M = ituple.capacity; N = ituple.numItems; nodeNumber = ituple.nodeNumber; } else { printf("Error in getting node in worker "); fflush(stdout); exit(1); } if(!itemsCopied) { PbyW = (ELEM *) malloc( N * sizeof(ELEM) ); tplength = N * sizeof(ELEM); #ifdef DEBUG puts("Going to get the items"); fflush(stdout); #endif strcpy(tpname,"Items"); status = cnf_tsread(tsItems, tpname, PbyW, 0); /* Just copy */ if (status <= 0) { printf("Error in getting items back in worker "); fflush(stdout); exit(2); } //else printf("Items copied\n"); itemsCopied = 1; } //luBound(PbyW, M, 0, N, 0, &L, &ubb); //printf("L is %g and ubb is %g\n", L, ubb); //newNode(NULL, 0, M, 0, ubb); newNode(NULL, ituple.tag, ituple.cu, ituple.profit, ituple.ub); addQLevelMarker(); while(i < N) { for(;continueLoop;) { node = deleteQ(); //printNode(node); switch(node->tag) { case -1: continueLoop = 0; break; default: //printf("Node->ub is %g and L is %g\n", node->ub, L); if(node->ub >= L){ cap = node->cu; profit = node->profit; if(cap >= PbyW[i].wt) { newNode(node, 1, (cap - PbyW[i].wt), (profit + PbyW[i].profit), node->ub); numOps += 2; } luBound(PbyW, cap, profit, N, (i+1), &lbb, &ubb); ++numOps; //printf("Cap is %g, PbyW[%d].wt is %g, ubb is %g\n", cap,i ,PbyW[i].wt, ubb); if(ubb >= L) { newNode(node, 0, cap, profit, ubb); L = (L > lbb)? L: lbb; } } free(node); break; } } addQLevelMarker(); continueLoop = 1; //printf("Live nodes at level(root at 0) %d are %ld\n", i, numLiveNodes); if(numLiveNodes == 0){ printf("Going to discard this node %d\n", nodeNumber); qLive = NULL; endQLive = NULL; i = N; /* break from while loop */ } if(numLiveNodes > maxLiveNodes){ maxLiveNodes = numLiveNodes; atLevel = 1; } i++; } if(qLive == NULL) packResults(0, qLive, N, M, nodeNumber, res, optVal); else for(; qLive; qLive = qLive->next) { if(qLive->profit == L) { packResults(L, qLive, N, M, nodeNumber, res, optVal); break; } } endTime = time((long *)0) - startTime; puts("WORKER TIME ENDED"); fflush(stdout); /* for(; qLive; qLive = qLive->next) { if(qLive->profit == L) packResults(L, qLive, N, M, nodeNumber, res, optVal); } */ } }
void bfsKnap(ELEM *PbyW, int M, int N) { int i, continueLoop = 1, knapCapacity; int tsd, res, tsItems, optVal, tpnamsz; double L, ubb, lbb, profit, cap; NODE *node; int CONS; CONS = atoi(getenv("NUMT")); // CONS = 1; knapCapacity = M; puts("Going to start bfsKnap"); fflush(stdout); maxLiveNodes = 1; atLevel = 0; tsd = cnf_open("Nodes", 0); tsItems = cnf_open("Items", 0); res = cnf_open("Results", 0); optVal = cnf_open("OptVal", 0); cnf_tsput(tsItems, "Items", PbyW, (N * sizeof(ELEM))); numProcessors = cnf_getP(); puts("TIME STARTED"); fflush(stdout); startTime = time((long *)0); i = 0; luBound(PbyW, M, 0, N, 0, &L, &ubb); //printf("L is %g and ubb is %g\n", L, ubb); newNode(NULL, 0, M, 0, ubb); addQLevelMarker(); while(i < N) { for(;continueLoop;) { node = deleteQ(); //printNode(node); switch(node->tag) { case -1: continueLoop = 0; break; default: //printf("Node->ub is %g and L is %g\n", node->ub, L); if(node->ub >= L){ cap = node->cu; profit = node->profit; if(cap >= PbyW[i].wt) { newNode(node, 1, (cap - PbyW[i].wt), (profit + PbyW[i].profit), node->ub); numOps += 2; } luBound(PbyW, cap, profit, N, (i+1), &lbb, &ubb); ++numOps; //printf("Cap is %g, PbyW[%d].wt is %g, ubb is %g\n", cap,i ,PbyW[i].wt, ubb); if(ubb >= L) { newNode(node, 0, cap, profit, ubb); L = (L > lbb)? L: lbb; } } free(node); break; } } addQLevelMarker(); continueLoop = 1; //printf("Live nodes at level(root at 0) %d are %ld\n", i, numLiveNodes); if(numLiveNodes == 0){ puts("Num Live Nodes becoms Zero. ERROR"); printf("Value of optimal filling %g\n", L); break; } if(numLiveNodes >= (CONS * numProcessors)) { /* Going to parallelize */ /* Distribute the live Nodes */ /* exit from the while loop by setting i = N */ int status, nodeNumber = 0; char tpname[128]; int tplength = sizeof(struct ituple); NODE *liveNode; #ifdef DEBUG printf("Num Live nodes to distribute %ld\n", numLiveNodes); #endif for(liveNode = qLive; liveNode->tag != -1; liveNode = liveNode->next, ++nodeNumber) { ituple.level = i; ituple.cu = liveNode->cu; ituple.profit = liveNode->profit; ituple.tag = liveNode->tag; ituple.ub = liveNode->ub; ituple.capacity = M; ituple.ubb = ubb; ituple.L = L; ituple.numItems = N; ituple.nodeNumber = nodeNumber; sprintf(tpname, "node%d\0", nodeNumber); #ifdef DEBUG printf("Master putting %s\n", tpname); #endif status = cnf_tsput(tsd, tpname, &ituple, tplength); } /* now put the terminating tuple */ ituple.numItems = 0; ituple.nodeNumber = nodeNumber; sprintf(tpname, "node%d\0", nodeNumber); #ifdef DEBUG printf("Master putting the termination tuple %s\n", tpname); #endif status = cnf_tsput(tsd, tpname, &ituple, tplength); strcpy(tpname, "OPTVAL\0"); #ifdef DEBUG printf("Master putting the optimal value tuple called %s with %g\n",tpname, L); #endif tplength = sizeof(double); status = cnf_tsput(optVal, tpname, &L, tplength); i = N; /* to exit from while loop */ } i++; } printResults(numLiveNodes, res, knapCapacity); cnf_close(tsd); cnf_close(tsItems); cnf_close(res); cnf_close(optVal); cnf_term(); }
bool Automate::ouvrir(const string& nomFichier){ deleteTableTranstion(); deleteA(); deleteQ(); deleteI(); deleteT(); ifstream fichier(nomFichier.c_str(), ios::in); if (fichier.is_open()){ string charA; string charQ; string charI; string charT; string charTDT; string temp; getline(fichier,charA); for(unsigned int i = 0; i < charA.size(); i++) { temp.clear(); while ((i < charA.size())&&(charA[i] == ' ')) i++; while((i < charA.size())&&(charA[i] != ' ')){ temp += charA.substr(i,1); i++; } _A.push_back(temp); } getline(fichier,charQ); for(unsigned int i = 0; i < charQ.size(); i++) { temp.clear(); while ((i < charQ.size())&&(charQ[i] == ' ')) i++; while((i < charQ.size())&&(charQ[i] != ' ')){ temp += charQ.substr(i,1); i++; } _Q.push_back(new list<string>); (*--_Q.end())->push_back(temp); } getline(fichier,charI); for(unsigned int i = 0; i < charI.size(); i++) { temp.clear(); while ((i < charI.size())&&(charI[i] == ' ')) i++; while((i < charI.size())&&(charI[i] != ' ')){ temp += charI.substr(i,1); i++; } _I.push_back(new list<string>); (*--_I.end())->push_back(temp); } getline(fichier,charT); for(unsigned int i = 0; i < charT.size(); i++) { temp.clear(); while ((i < charT.size())&&(charT[i] == ' ')) i++; while((i < charT.size())&&(charT[i] != ' ')){ temp += charT.substr(i,1); i++; } _T.push_back(new list<string>); (*--_T.end())->push_back(temp); } list<list<string>*>::iterator iQ; for(iQ=_Q.begin(); iQ!=_Q.end(); iQ++){ _tab[*iQ]=new list<list<string>*>[_A.size()]; } getline(fichier,charTDT); iQ = _Q.begin(); unsigned int col; unsigned int start; while ((!fichier.eof())&&(iQ != _Q.end())){ getline(fichier,charTDT); col = 0; start = 0; while ((start < charTDT.size())&&(charTDT[start] == ' ')) start++; while(charTDT[start] != ' ') start++; for(unsigned int i=start; i < charTDT.size(); i++){ temp.clear(); while ((i < charTDT.size())&&(charTDT[i] == ' ')) i++; while((i < charTDT.size())&&(charTDT[i] != ',')&&(charTDT[i] != ' ')){ temp += charTDT.substr(i,1); i++; } if (temp != "-"){ _tab[*iQ][col].push_back(new list<string>); (*--_tab[*iQ][col].end())->push_back(temp); } if (charTDT[i] == ','){ i++; } else{ col++; } } iQ++; } return true; } return false; }
bool Automate::minimiser(){ /* Minimise l'automate * Retourne true si l'automate etait deja minimal <=> Le nombre de groupe cree lors de la minimisation est egal au nombre d'etat de l'automate * Retourne false si l'automate n'etait pas minimal * */ list<string> groupes; // La liste des groupes Ex : {NT, T} list<string> tempgroupes; // Une liste des groupes temporaires string groupeString; // Le groupe auquel appartient un etat map<string, list<list<string>*>*> tabG; // Un tableau indexe par les groupes contenant la liste des etats dans un groupe //Initialisation de tempGroupes et de tabG tempgroupes.push_back("NT"); tempgroupes.push_back("T"); tabG["NT"] = new list<list<string>*>; tabG["T"] = new list<list<string>*>; for(list<list<string>*>::iterator iQ=_T.begin(); iQ!=_T.end(); iQ++) tabG["T"]->push_back(*iQ); for(list<list<string>*>::iterator iQ=_Q.begin(); iQ!=_Q.end(); iQ++){ int temp = 0; for(list<list<string>*>::iterator iT=_T.begin(); iT!=_T.end(); iT++){ if( ((*iT)->size() == (*iQ)->size()) && (equal( (*iQ)->begin(), (*iQ)->end(), (*iT)->begin()) )){ temp = 1; } } if(temp == 0) tabG["NT"]->push_back(*iQ); } list<string>::iterator iG; QStringList liste; QString tempS; int cptG; // Tant que le nombre de groupe n'est pas egal a celui de la derniere iteration while (groupes.size() != tempgroupes.size()) { // Initialisation de groupes et reinitialisation de tempgroupes groupes = tempgroupes; tempgroupes.clear(); cptG = 1; for(list<string>::iterator i = groupes.begin(); i != groupes.end(); i++){ tempS.clear(); tempS += "Group " + QString::number(cptG) + " : "; for(list<list<string>*>::iterator ii = tabG[*i]->begin(); ii != tabG[*i]->end(); ii++){ if (ii != tabG[*i]->begin()) tempS += ", "; tempS += "{"; for(list<string>::iterator iii = (*ii)->begin(); iii != (*ii)->end(); iii++){ if(iii != (*ii)->begin()) tempS += ", "; tempS += QString::fromStdString(*iii); } tempS += "}"; } liste << tempS; cptG++; } liste << ""; // Pour chaque etat for(list<list<string>*>::iterator iQ=_Q.begin(); iQ!=_Q.end(); iQ++){ // Reinitialisation de groupeString groupeString.clear(); iG = groupes.begin(); int temp; // Faire tant que nous ne trouvons pas dans quel groupe est cet etat temp = 1; do { for(list<list<string>*>::iterator iiG = tabG[*iG]->begin(); iiG != tabG[*iG]->end(); iiG++){ // Si les etats sont egaux if (((*iiG)->size() == (*iQ)->size())&&(equal((*iiG)->begin(), (*iiG)->end(), (*iQ)->begin()))) temp = 0; } iG++; }while(temp == 1); iG--; // Insertion de ce groupe dans groupeString groupeString = groupeString + *iG; for (unsigned int cpt = 0; cpt != _A.size(); cpt++){ int temp; iG = groupes.begin(); temp = 1; do{ list<list<string>*>::iterator iTab = _tab[*iQ][cpt].begin(); for(list<list<string>*>::iterator iiG = tabG[*iG]->begin(); iiG != tabG[*iG]->end(); iiG++){ if (((*iiG)->size() == (*iTab)->size())&&(equal((*iiG)->begin(), (*iiG)->end(), (*iTab)->begin()))) // Si les etats ne sont pas egaux temp = 0; } iG++; }while(temp == 1); iG--; // Insertion de ce groupe dans groupeString groupeString = groupeString + *iG; } if(tabG[groupeString] == NULL){ tabG[groupeString] = new list<list<string>*>; } // Insertion de cet etat dans son groupe tabG[groupeString]->push_back(*iQ); tempgroupes.push_back(groupeString); tempgroupes.sort(); tempgroupes.unique(); } // Suppression des anciens groupes for (list<string>::iterator it=groupes.begin(); it != groupes.end(); it++) tabG[*it]->clear(); } Interface::messageMinimiser(&liste); groupes = tempgroupes; tempgroupes.clear(); if(groupes.size() == _Q.size()) return true; list<list<string>*> Qmin, Tmin, Imin; list<string>::iterator iGG; list<list<string>*>::iterator iTabG; list<list<string>*>::iterator iTab; int temp; list<list<string>*>::iterator iI; list<list<string>*>::iterator iIm; iI = _I.begin(); iG=groupes.begin(); temp = 1; do { for(list<list<string>*>::iterator iiG = tabG[*iG]->begin(); iiG != tabG[*iG]->end(); iiG++){ if (((*iiG)->size() == (*iI)->size())&&(equal((*iiG)->begin(), (*iiG)->end(), (*iI)->begin()))) // Si les etats ne sont pas egaux temp = 0; } iG++; }while(temp == 1); iG--; Imin.push_back(new list<string>); iIm =-- Imin.end(); for(list<list<string>*>::iterator iTabGG = tabG[*iG]->begin(); iTabGG != tabG[*iG]->end(); iTabGG++) (*iIm)->insert(--(*iIm)->end(), (*iTabGG)->begin(), (*iTabGG)->end()); deleteI(); _I = Imin; list<list<string>*>::iterator iTmin; for(list<string>::iterator iG = groupes.begin(); iG != groupes.end(); iG++){ if ((*iG).at(0) == 'T'){ Tmin.push_back(new list<string>); iTmin =-- Tmin.end(); for(list<list<string>*>::iterator iTabG = tabG[*iG]->begin(); iTabG != tabG[*iG]->end(); iTabG++){ (*iTmin)->insert(--(*iTmin)->end(), (*iTabG)->begin(), (*iTabG)->end()); } } } deleteT(); _T = Tmin; list<list<string>*>::iterator iQmin; list<list<string>*>::iterator iTabmin; for(list<string>::iterator iG=groupes.begin(); iG != groupes.end(); iG++){ // Creation des états Qmin.push_back(new list<string>); iQmin = --Qmin.end(); for(iTabG = tabG[*iG]->begin(); iTabG != tabG[*iG]->end(); iTabG++){ (*iQmin)->insert(--(*iQmin)->end(), (*iTabG)->begin(), (*iTabG)->end()); } // Creation des transitions associes a chaque etat _tab[*(--Qmin.end())] = new list<list<string>*>[_A.size()]; // Pour chaque transition for(unsigned int cpt = 0; cpt < _A.size(); cpt++){ // Pour un etat dans le groupe iTabG = tabG[*iG]->begin(); // Pour une transition de de cet etat dans le groupe iTab = _tab[*iTabG][cpt].begin(); int temp; iGG = groupes.begin(); temp = 1; // Faire tant que nous ne trouvons pas dans quel groupe est cet etat do { for(list<list<string>*>::iterator iiG = tabG[*iGG]->begin(); iiG != tabG[*iGG]->end(); iiG++){ if (((*iiG)->size() == (*iTab)->size())&&(equal((*iiG)->begin(), (*iiG)->end(), (*iTab)->begin()))) temp = 0; } iGG++; }while(temp == 1); iGG--; _tab[*(--Qmin.end())][cpt].push_back(new list<string>); iTabmin =-- _tab[*(--Qmin.end())][cpt].end(); for(list<list<string>*>::iterator iTabGG = tabG[*iGG]->begin(); iTabGG != tabG[*iGG]->end(); iTabGG++) (*iTabmin)->insert(--(*iTabmin)->end(), (*iTabGG)->begin(), (*iTabGG)->end()); } } deleteQ(); _Q = Qmin; return false; }
void Automate::determinisation(){ /* Determinise l'automate * */ list<list<string>*> Qdet; list<list<string>*> Tdet; // Creation de l'etat initial (= premier etat de la liste des etats) Qdet.push_back(new list<string>); list<list<string>*>::iterator iQdet = Qdet.begin(); _tab[*iQdet] = new list<list<string>*>[_A.size()]; for(list<list<string>*>::iterator iI=_I.begin(); iI!=_I.end(); iI++) (*iQdet)->insert((*iQdet)->begin(),(*iI)->begin(),(*iI)->end()); (*iQdet)->sort(); (*iQdet)->unique(); // Remplace les etats initiaux pour le nouvel etat initial _I.clear(); _I.push_back(new list<string>); (*_I.begin())->assign((*iQdet)->begin(),(*iQdet)->end()); // Creer la nouvelle table de transition et la nouvelle liste des etats list<list<string>*>::iterator iQ; list<string>::iterator iiQdet; unsigned int cpt; // Tant qu'il y a des etats dans la nouvelle liste des etats while(iQdet != Qdet.end()){ for(cpt=0; cpt<_A.size(); cpt++){ _tab[*iQdet][cpt].push_back(new list<string>); } // Creation des nouvelles transitions pour l'etat concerne for(iQ=_Q.begin(); iQ!=_Q.end(); iQ++){ for(iiQdet=(*iQdet)->begin(); iiQdet!=(*iQdet)->end(); iiQdet++){ if (find((*iQ)->begin(),(*iQ)->end(),*iiQdet) != (*iQ)->end()){ for(cpt=0; cpt<_A.size(); cpt++){ list<list<string>*>::iterator iiTab; iiTab = _tab[*iQdet][cpt].begin(); for(list<list<string>*>::iterator iTab=_tab[*iQ][cpt].begin(); iTab!=_tab[*iQ][cpt].end(); iTab++) (*iiTab)->insert(--(*iiTab)->end(),(*iTab)->begin(),(*iTab)->end()); } } } } int temp; list<list<string>*>::iterator iTab; // Tri des nouvelles transitions pour l'etat concerne, et creation d'un nouvel etat a partir des nouvelles transition (s'il n'existait pas deja) for(cpt=0; cpt<_A.size(); cpt++){ iTab=_tab[*iQdet][cpt].begin(); while((!_tab[*iQdet][cpt].empty())&&(iTab!=_tab[*iQdet][cpt].end())){ if((*iTab)->empty()){ _tab[*iQdet][cpt].erase(iTab); iTab--; } else{ (*iTab)->sort(); (*iTab)->unique(); } iTab++; } temp = 0; iTab = _tab[*iQdet][cpt].begin(); // Creation d'un nouvel etat (s'il n'existait pas deja) for(iQ=Qdet.begin(); iQ!=Qdet.end(); iQ++){ if((_tab[*iQdet][cpt].empty())||(((*iTab)->size() == (*iQ)->size())&&(equal((*iTab)->begin(),(*iTab)->end(),(*iQ)->begin())))) temp = 1; } if (temp == 0){ Qdet.push_back(new list<string>); _tab[*(--Qdet.end())] = new list<list<string>*>[_A.size()]; (*(--Qdet.end()))->assign((*iTab)->begin(),(*iTab)->end()); } } iQdet++; } // Suppression de l'ancienne table de transition deleteTableTranstion(); // Remplace l'ancienne liste des etats par la nouvelle deleteQ(); _Q = Qdet; // Creation de la nouvelle liste des etats teminaux (tout etat contenant un ancien etat terminal est terminal) for(list<list<string>*>::iterator iT=_T.begin(); iT!=_T.end(); iT++){ for(iQ=_Q.begin(); iQ!=_Q.end(); iQ++){ if(search((*iQ)->begin(),(*iQ)->end(),(*iT)->begin(),(*iT)->end()) != (*iQ)->end()){ int temp = 0; list<list<string>*>::iterator iTdet; iTdet = Tdet.begin(); while ((iTdet != Tdet.end())&&(temp == 0)){ if (((*iQ)->size() == (*iTdet)->size())&&(equal((*iQ)->begin(),(*iQ)->end(),(*iTdet)->begin()))) temp = 1; iTdet++; } if (temp == 0){ Tdet.push_back(new list<string>); (*(--Tdet.end()))->assign((*iQ)->begin(),(*iQ)->end()); } } } } // Remplace l'ancienne liste des etats terminaux par la nouvelle deleteT(); _T = Tdet; }