Word PresentationParser::parseRelator( const VectorOf<Chars>& names, Chars& errMesg ) { genNames = names; if ( curToken == INIT ) getToken(); if ( !atStartOfWord() ) { parseError("Expected a word here"); errMesg = parseErrorMessage; return Word(); } else { Word u = parseWord( names, errMesg ); if ( errMesg.length() > 0 ) { return Word(); } if ( curToken == EQUALS ) { getToken(); Word v = parseWord( names, errMesg ); if ( errMesg.length() > 0 ) { return Word(); } return ((Word)(u * v.inverse())).cyclicallyReduce(); } else return u.cyclicallyReduce(); } }
// @brief Send post request // @param string uri the request uri // @return If no error occurs returns the number of receive data in bytes, // Otherwise -1 is returns int HTTPClient::post( string uri, Chars &body ) { string str; m_method = "POST"; str += "POST " + uri + " " + m_version; str += "\r\n"; str += "Host: " + m_host; str += "\r\n"; map<string, string>::iterator iter = m_requestHeader.begin(); while (iter != m_requestHeader.end()) { str += iter->first + ": "; str += iter->second; str += "\r\n"; ++iter; } // Set 'Content-length' str += "Content-Length: "; char buf[25]; snprintf(buf, 25, "%ld", body.length()); str += buf; str += "\r\n"; // Headers end str += "\r\n"; Chars data; data.assign(str.c_str(), str.length()); data.append(body.c_str(), body.length()); if (!isBlocking()) { setBlocking(); } // Set receive and send timeout to 5 seconds int nTimeout = 5000; if (m_host == "127.0.0.1") { nTimeout = 100; } setRcvTimeout(nTimeout); setSndTimeout(nTimeout); int n = this->send(data.c_str(), data.length()); if (n == -1) { return n; } //this->shutdown(SHUT_WD); /** * Receive response data */ // Receive all response data to socket buffer including HTTP headers while ( this->recv(1024) > 0 ) ; return decodeHttpData(); }
SetOf<Word> PresentationParser::parseWordSet( const VectorOf<Chars>& names, Chars& errMesg ) { genNames = names; if ( curToken == INIT ) getToken(); SetOf<Word> result; if ( curToken == INIT ) getToken(); while ( atStartOfWord() ) { Word w = parseWord( names, errMesg ); if ( errMesg.length() > 0 ) { return result; } result.adjoinElement( w ); if ( curToken == COMMA ) { getToken(); if ( !atStartOfWord() ) { parseError("Expected a word here"); errMesg = parseErrorMessage; return result; } } } return result; }
SetOf<Word> PresentationParser::parseRelatorList(const VectorOf<Chars>& names,Chars& errMesg) { genNames = names; SetOf<Word> result; if ( curToken == INIT ) getToken(); while ( atStartOfWord() ) { Word u = parseWord( names, errMesg ); if ( errMesg.length() > 0 ) { return result; } if ( curToken != EQUALS ) result.adjoinElement( u.cyclicallyReduce() ); else { while ( curToken == EQUALS ) { getToken(); Word v = parseWord( names, errMesg ); if ( errMesg.length() > 0 ) { return result; } result.adjoinElement( ((Word)(u * v.inverse())).cyclicallyReduce() ); } } if ( curToken == COMMA ) { getToken(); if ( !atStartOfWord() ) { parseError("Expected a word here"); errMesg = parseErrorMessage; return result; } } } return result; }
bool PresentationParser::getRangeOf( const Chars& name ,Chars& c, int& n){ char last = name[name.length()-1]; // If last char is not a number if ( !(last >= '0' && last <= '9') ) return false; bool endOfNum = false; Chars charPart; Chars numPart; for (int i = name.length()-1;i>=0;i--){ char ch = name[i]; if(!endOfNum && (ch >= '0' && ch <= '9')) numPart = Chars(ch)+numPart; else { endOfNum = true; charPart = Chars(ch)+charPart; } } // If name consists of numbers only if (!endOfNum) return false; c = charPart; n = atoi(numPart); return true; }
void IsEltCentral::takeControl( ) { Trichotomy theAnswer = dontknow; Chars explanation; if ( ! displayInFE() || freeARCs() > 0 ) if (nilpotentWP->state() == ComputationManager::TERMINATED){ explanation = ": this follows from an inspection of a basis." ; theAnswer = nilpotentWP->isTrivial(); } if (nilpotentWPInQuotients->state() == ComputationManager::TERMINATED) if (nilpotentWPInQuotients->isTrivial()==no){ int retClass; theAnswer = nilpotentWPInQuotients->isTrivial(retClass); //@njz // ostrstream msgTmp; std::ostrstream msgTmp; // msgTmp << ": because it's not central in lower central quotient of class " << retClass << ends; explanation = msgTmp.str(); } if (theAnswer != dontknow){ LogMessage msg( *this, theWord ); msg << Name( theWord ) << " is "; if ( theAnswer == no ) msg << "not "; msg << "central in " << Name( Parent( theWord ) ) << explanation; msg.send(); adminTerminate(); return; } if ( ( theAnswer = theChecker.isTrivial() ) != dontknow ) { LogMessage msg( *this, theWord ); msg << Name( theWord ) << " is "; if ( theAnswer == no ) msg << "not "; msg << "central in " << Name( Parent( theWord ) ); Chars explanation = theChecker.getExplanation(); if( explanation.length() > 0 ) msg << " : " << explanation; msg << "."; msg.send(); if( theChecker.haveDetails() ) { LogMessage msgLink( *this, theWord ); msgLink << Link( Chars("Click here to see details of centrality problem " "for ") + Text(Name(theWord)) + Text("."), "WordProblemDetails", theChecker.getDecompositionFileName() ); msgLink.send(); } adminTerminate(); } else if ( ( theAnswer = genetic->areTrivial() ) != dontknow ) { Chars explanation = "according to the genetic algorithm"; LogMessage msg( *this, theWord ); msg << Name( theWord ) << " is central"; msg << " : " << explanation << "."; msg.send(); adminTerminate(); } else if ( ( theAnswer = genetic->areTrivial() ) != dontknow ) { Chars explanation = "according to the genetic algorithm"; LogMessage msg( *this, theWord ); msg << Name( theWord ) << " is central"; msg << " : " << explanation << "."; msg.send(); adminTerminate(); } else usedOneARC(); }
int main ( ) { Chars errMsg; GHNConfig c; //cout << c << endl; ifstream in("../wp.in"); in >> c; cout << c << endl; int numOfVars; cout << "Enter the number of variables: "; cin >> numOfVars; cout << endl; FreeGroup G; cout << "Enter a free group: "; errMsg = cin >> G; if( errMsg.length() > 0 ) { cout << errMsg; exit(0); } cout << endl; VectorOf<Chars> v = G.namesOfGenerators(); int numOfConst = v.length(); int rLen = v.length() + numOfVars; VectorOf<Chars> r(rLen); int j; for( j = 0; j < v.length(); ++j ) if( v[j][0] != 'x' && v[j][0] != 'X' ) r[j] = v[j]; else error("x is reserved for variables\n"); char s[3] = {0,0,0}; s[0] = 'x'; for( int i = j; i < rLen; ++i ) { s[1] = i-j+1+48; r[i] = s; } FreeGroup F(r); cout << "Enter an equation (a word) with variables x1 ... x" << numOfVars << ": "; Word w; w = F.readWord(cin,errMsg); if( errMsg.length() > 0 ) { cout << errMsg; exit(0); } cout << endl; VectorOf<Word> im(rLen); for( int i = 0; i < rLen; ++i ) im[i] = Word(Generator(i+1)); Map M(F,F,im); int popSize = c.populationSize(); GAWord pop[numOfVars][popSize],newPop[numOfVars][popSize]; for( int k = 0; k < numOfVars; ++k ) for( int i = 0; i < popSize; ++i ) { pop[k][i] = GAWord(numOfConst,Word()); } int fit[popSize]; // the main loop int numOfGens = c.numOfGenerations(); //bool bHaveFitnessScaling = c.haveFitnessScaling(); float crossRate = c.chanceOfCrossover(); float mutRate = c.chanceOfMutation(); UniformRandom devgen; int max, min, minInd, g; // create the original random populations for( int k = 0; k < numOfVars; ++k ) for( int i = 0; i < popSize; ++i ) { pop[k][i] = pop[k][i].randomWord(); } for( g = 0; g < numOfGens || numOfGens == -1; ++g ) { min = MAXINT; max = 0; minInd = -1; // compute fitness values for( int i = 0; i < popSize; ++i ) { for( int k = 0; k < numOfVars; ++k ) M.setGeneratingImages(numOfConst+k,(pop[k][i]).getWord()); fit[i] = M.imageOf(w).freelyReduce().length(); if( fit[i] < min ) { min = fit[i]; minInd = i; } else if( fit[i] > max ) max = fit[i]; } // print current results cout << "Generation: " << g << " Fitness: " << min << endl; /* if( g % 100 == 0 ) { for( int i = 0; i < popSize; ++i ) { cout << "x" << i << " = "; F.printWord(cout, (pop[i]).getWord()); cout << endl; } cout << endl; } */ // exit if found a solution if( min == 0 ) { for( int k = 0; k < numOfVars; ++k ) { cout << "x" << k+1 << " = "; F.printWord(cout, (pop[k][minInd]).getWord()); cout << endl; } return 0; } // make fitness values suitable for Roulette wheel selection int base = max + 1; for( int i = 0; i < popSize; ++i ) fit[i] = base - fit[i]; // fitness scaling if( c.haveFitnessScaling() ) for( int i = 0; i < popSize; ++i ) fit[i] = fit[i] * fit[i]; // crossover RouletteWheel<int> wheel(popSize,fit); for( int k = 0; k < numOfVars; ++k ) for( int i = 0; i < popSize; ++i ) { if( devgen.rand() <= crossRate ) { int i1 = wheel.GetIndex(); int i2 = wheel.GetIndex(); newPop[k][i] = pop[k][i1].crossover(pop[k][i2]); } else { newPop[k][i] = pop[k][i]; } } // mutation for( int k = 0; k < numOfVars; ++k ) for( int i = 0; i < popSize; ++i ) { if( devgen.rand() <= mutRate ) { newPop[k][i] = newPop[k][i].mutate(); } } // elitist selection for( int k = 0; k < numOfVars; ++k ) if( c.haveElitistSelection() ) { newPop[k][0] = pop[k][minInd]; } // prepare for the next iteration for( int k = 0; k < numOfVars; ++k ) for( int i = 0; i < popSize; ++i ) { pop[k][i] = newPop[k][i]; } } }
// @brief Operator + // @return one copy of Chars Chars Chars::operator+ (const Chars& rhs) { this->append(rhs.m_data, rhs.length()); return *this; }
int main ( ) { Chars errMsg; FreeGroup F; cout << "Enter a free group: "; errMsg = cin >> F; if( errMsg.length() > 0 ) { cout << errMsg; exit(0); } cout << endl; VectorOf<Chars> empty, varNames; EquationParser p(cin); cout << "Enter a graphic equation: "; Word equation = p.parseEquation(empty, varNames, errMsg); if( errMsg.length() > 0 ) { cout << errMsg; exit(0); } cout << endl; cout << "Enter a word in F: "; WordParser P(cin); Word w = P.parseWordVerbatim( F.namesOfGenerators(), errMsg); if( errMsg.length() > 0 ) { cout << errMsg; exit(0); } cout << endl; w = w.cyclicallyReduce(); FreeGroup G(varNames); cout << "The group: " << F << endl; cout << "The variables: " << varNames << endl; cout << "Equation: "; G.printWord(cout, equation); cout << endl; cout << "Constant of length " << w.length() << ": "; F.printWord(cout, w); cout << endl; DGESolver solver(F,varNames,equation); Map solution; int wLen = w.length(); timeval t1,t2; gettimeofday(&t1, NULL); for( int i = 0; i < wLen; ++i ) { Word u = w.cyclicallyPermute(i); cout << i << ": " << endl; if( solver.getSolution(u, solution) ) { cout << "The solution is: " << endl; for( int j = 0; j < varNames.length(); ++j ) { cout << varNames[j] << " = "; F.printWord(cout, solution.generatingImages(j)); cout << endl; } cout << endl; break; } else cout << "The equation has no solutions" << endl; } gettimeofday(&t2, NULL); cout << "Time elapsed: " << diffTime(t2,t1) << endl << endl; }