/* Make a guess in each valid position in the graph */ void Solver::solveDepth(int depth) { bool usingPrioQueue = true; if (usingPrioQueue) { int initSize = epq_.size(); int guesses = 0; while (!epq_.empty() && guesses++ < initSize && !multipleSolutions_) { PrioEdge pe = epq_.top(); if (pe.h) { makeHLineGuess(pe.coords.i, pe.coords.j, depth); if (grid_->getHLine(pe.coords.i, pe.coords.j) == EMPTY) { pe.priority = pe.priority - 1; epq_.push(pe); } if (grid_->getUpdated()) { break; } } else { makeVLineGuess(pe.coords.i, pe.coords.j, depth); if (grid_->getVLine(pe.coords.i, pe.coords.j) == EMPTY) { pe.priority = pe.priority - 1; epq_.push(pe); } if (grid_->getUpdated()) { break; } } epq_.pop(); } } else { for (int i = 0; i < grid_->getHeight()+1; i++) { for (int j = 0; j < grid_->getWidth(); j++) { applyRules(selectedRules_); makeHLineGuess(i, j, depth); } } for (int i = 0; i < grid_->getHeight(); i++) { for (int j = 0; j < grid_->getWidth()+1; j++) { applyRules(selectedRules_); makeVLineGuess(i, j, depth); } } } }
struct grid resolvGrid(struct grid Grid){ applyRules(&Grid); if(isComplete(Grid)){ return Grid; } else if(!isValid(Grid)){ Grid.line = -1; return Grid; } else{ struct grid g=copy(Grid); if(fixUncertainty(&g, 1)){ g = resolvGrid(g); if(g.line != -1){ return g; } else{ g=copy(Grid); fixUncertainty(&g, 0); g = resolvGrid(g); return g; } } else{ Grid.line = -1; return Grid; } } }
/* Constructor takes a grid as input to solve */ Solver::Solver(Grid & grid, Rule rules[NUM_RULES], Contradiction contradictions[NUM_CONTRADICTIONS], int selectedRules[], int selectLength, int depth) { grid_ = &grid; depth_ = depth; multipleSolutions_ = false; epq_.initEPQ(grid_->getHeight(), grid_->getWidth()); rules_ = rules; contradictions_ = contradictions; selectedRules_ = selectedRules; ruleCounts_ = 0; int selectedPlusBasic[selectLength+NUM_CONST_RULES]; for (int i = 0; i < selectLength; i++) { selectedPlusBasic[i] = selectedRules[i]; } for (int i = 1; i <= NUM_CONST_RULES; i++) { selectedPlusBasic[selectLength+ NUM_CONST_RULES - i] = (NUM_RULES - i); } selectLength_ = selectLength + NUM_CONST_RULES; applyRules(selectedPlusBasic); selectLength_ = selectLength; solve(); }
void LSystem::generate() { //generate Alphabet from Rules if (alphabet.empty()) for (Rule& rule : _rules) alphabet.push_back(rule.getTrigger()); _sentence = applyRules(_sentence, 1); }
string LSystem::applyRules(string& str, unsigned depth) { string newSentence = ""; for (const char& c : str) newSentence.append(_rules[alphabet.find(c, 0)].getRes()); //not Thread safe if (depth == _maxDepth) return newSentence; else return applyRules(newSentence, depth+1); }
/* Apply a combination of deterministic rules and * recursive guessing to find a solution to a puzzle */ void Solver::solve() { grid_->setUpdated(true); while (grid_->getUpdated() && !grid_->isSolved()) { applyRules(selectedRules_); for (int d = 0; d < depth_; d++) { if (!grid_->getUpdated() && !testContradictions() && !grid_->isSolved() && !multipleSolutions_) { solveDepth(d); } } } }
bool lemmatiseFile(const char * OneWordPerLineFile,const char * rulefile,const char * resultFile) { buffer Buffer; if(!readRules(rulefile,&Buffer)) return false; FILE * OWPLF = fopen(OneWordPerLineFile,"rb"); ++openfiles; if(!OWPLF) { deleteRules(); return false; } FILE * rf = fopen(resultFile,"wb"); ++openfiles; if(!rf) { --openfiles; fclose(OWPLF); deleteRules(); return false; } fseek(OWPLF, 0, SEEK_END); long wend = ftell(OWPLF); fseek(OWPLF, 0, SEEK_SET); char * wbuf = new char[(size_t)(wend + 1)]; if (wbuf && wend > 0) { if (fread(wbuf, 1, (size_t)wend, OWPLF) != (size_t)wend) return 0; wbuf[wend] = '\0'; } char * q; for(char * p = wbuf;(q = strchr(p,'\n')) != 0;p = q + 1) { *q = 0; for(char * r = q - 1;r >= p && (*r == '\r' || *r == ' ' || *r == '\t');--r) *r = 0; fprintf(rf,"%s\n",applyRules(p,&Buffer)); } delete [] wbuf; deleteRules(); --openfiles; fclose(rf); --openfiles; fclose(OWPLF); return true; }
void initBase( PlantRules* rules, GamePlantNode* curNode, FIVector4* baseVec ) { int i; float curLerp; float maxLength = 0.0f; //float curMult = 1.0f; int maxGen = rules->numGenerations; curNode->init(NULL,rules->numChildren[1],gv(rules->numChildren)); //curNode->tangent.setFXYZRef(baseVec); curNode->begPoint.setFXYZRef(&origin); curNode->endPoint.setFXYZRef(&origin); curNode->endPoint.addXYZRef(baseVec,rules->curLength[0]); curNode->updateTangent(gv(rules->divergenceAngleV)); for (i = 1; i <= maxGen; i++) { maxLength += rules->curLength[i]; // *curMult; //curMult *= rules->nodeLengthMultiplier; } // curLerp = rules->curLength[0]/maxLength; // curNode->endThickness = (1.0f-curLerp)*rules->begThickness + curLerp*rules->endThickness; // curLerp = 0.0f; // curNode->begThickness = curNode->endThickness;//(1.0f-curLerp)*rules->begThickness + curLerp*rules->endThickness; // curNode->midThickness = (curNode->begThickness+curNode->endThickness)*0.5f; curNode->begThickness = rules->begThickness; curNode->endThickness = curNode->begThickness; curNode->midThickness = curNode->begThickness; applyRules( rules, curNode, 0, maxGen, //rules->baseLength*(rules->nodeLengthMultiplier), 0.0f, maxLength ); }
int main(int argc, char **argv) { if ((!parseArgs(argc, argv)) || (!getDisplay(argc, argv))) exit(-1); settings.locale.value = setlocale(LC_ALL, settings.locale.value); settings.locale.src = FROM_SERVER; VMSG1(7, "locale is %s\n", settings.locale.value); if (dpy) getServerValues(); if (settings.config.value && (!applyConfig(settings.config.value))) exit(-3); if (!applyRules()) exit(-4); if (!applyComponentNames()) exit(-5); if (dpy) XCloseDisplay(dpy); exit(0); }
int main(int argc, char **argv) { if ((!parseArgs(argc, argv)) || (!getDisplay(argc, argv))) exit(-1); svValue[LOCALE_NDX] = setlocale(LC_ALL, svValue[LOCALE_NDX]); svSrc[LOCALE_NDX] = FROM_SERVER; VMSG1(7, "locale is %s\n", svValue[LOCALE_NDX]); if (dpy) getServerValues(); if (svValue[CONFIG_NDX] && (!applyConfig(svValue[CONFIG_NDX]))) exit(-3); if (!applyRules()) exit(-4); if (!applyComponentNames()) exit(-5); if (dpy) XCloseDisplay(dpy); exit(0); }
void Map::update() { // iterate over everything except for the border and mark // which cells should live, die and be born: for ( int x = 1 ; x < mapSize - 1 ; x++ ) { for (int y = 1 ; y < mapSize - 1 ; y++ ) { int n = countAliveNeighbours( x, y ); applyRules( x, y, n ); } } // now that we have counted all neighbours and determined the new states, // we can actually change the alive-states of the cells: for ( int x = 1 ; x < mapSize - 1 ; x++ ) { for (int y = 1 ; y < mapSize - 1 ; y++ ) { tiles[x][y].alive = tiles[x][y].newState; } } }
void applyRules( PlantRules* rules, GamePlantNode* curParent, int curGen, int maxGen, //float curLength, float totLength, float maxLength ) { //cout << "curLength: " << curLength << "\n"; int i; float fi; float fCurGen = (float)curGen; float fNumChildren; float curLerp; float twoPi = 6.283185307f; float curLength = rules->curLength[curGen]; GamePlantNode* curChild; fNumChildren = (float)(curParent->numChildren); for (i = 0; i < curParent->numChildren; i++) { fi = ((float)i)/fNumChildren; curChild = &(curParent->children[i]); if (curGen == maxGen) { curChild->init(curParent,0,0); } else { curChild->init(curParent,rules->numChildren[1], max( gv(rules->numChildren)-(float)curGen, 1.0f) ); } curChild->begPoint.setFXYZRef(&(curParent->endPoint)); curChild->endPoint.setFXYZRef(&(curParent->endPoint)); axisRotationInstance.doRotation( &tempv0, &(curParent->baseShoot), &(curParent->tangent), fi*twoPi + (fGenRand()-0.5f)*twoPi*(1.0f-rules->angleUniformityU)/fNumChildren ); curChild->endPoint.addXYZRef(&tempv0,curLength); curChild->updateTangent(gv(rules->divergenceAngleV)); curLerp = (totLength/maxLength); curChild->begThickness = (1.0f-curLerp)*rules->begThickness + curLerp*rules->endThickness; curLerp = ((totLength+curLength)/maxLength); curChild->endThickness = (1.0f-curLerp)*rules->begThickness + curLerp*rules->endThickness; curChild->midThickness = (curChild->begThickness+curChild->endThickness)*0.5f; if ( rules->sphereGen == fCurGen ) { curChild->sphereRad = rules->sphereRad*singleton->pixelsPerMeter; //(maxLength-totLength) + } if (curGen < maxGen) { applyRules(rules, curChild, curGen + 1, maxGen, totLength+curLength, maxLength); } } }