GPPtr<GPParameter> GPParticleSwarmOpt::vFindBest(int n, IGPOptimizor::OPTFUNC computer, GPFLOAT* target) const { GPASSERT(n>0); GPASSERT(mSize>=1); GPASSERT(mTimes>=1); std::vector<GPPtr<Particle> > group; group.reserve(mSize); GPRandom::init(); for (int i=0; i<mSize; ++i) { GPPtr<Particle> p = (new Particle(n, mVmax, mThe1, mThe2)); p->randomInit(); GPFLOAT fit = computer(p->expand()); p->setFit(fit); group.push_back(p); } GPPtr<Particle> bestP; for (int i=0; i<mTimes; ++i) { for (int j=0; j<mSize; ++j) { GPPtr<Particle> p = group[j]; GPFLOAT fit = computer(p->expand()); p->updateFit(fit); } /*Find Best One*/ bestP = group[0]; for (int j=1; j<mSize; ++j) { if (group[j]->fit() > bestP->fit()) { bestP = group[j]; } } if (NULL!=target) { if (*target <= bestP->fit()) { break; } } /*Get the max distance before, Determin if we can break*/ GPFLOAT maxDis_before = max_distance(group); /*Move all Particle*/ for (int j=0; j<mSize; ++j) { GPPtr<Particle> p = group[j]; p->move(bestP); } /*Get the max distance after, Determin if we can break*/ GPFLOAT maxDis_after = max_distance(group); if (maxDis_before < mMinDistance*n && maxDis_after < mMinDistance*n) { break; } } bestP->fix(); return bestP->expand(); }
bool GPStreamADF::CP::vReceive(GPContents* c, const Point* source) { for (int i=0; i<mInputs.size(); ++i) { if (mInputs[i] == source) { if (mPoint->receive(c, i)) { GPContents* c = mPoint->compute(); GPASSERT(c->size() == mOutputs.size()); for (int i=0; i<c->size(); ++i) { GPContents unit; unit.push((*c)[i]); mOutputs[i]->vReceive(&unit, this); } /*Should not clear c!*/ delete c; } return true; } } GPASSERT(0); return false; }
bool GPStreamADF::SP::vReceive(GPContents* con, const Point* source) { GPASSERT(NULL==source); GPASSERT(NULL!=con); GPASSERT(1 == con->size()); mOutputs[0]->vReceive(con, this); return true; }
GPCarryVaryGroup::GPCarryVaryGroup(unsigned int* dimesions, unsigned int number) { GPASSERT(NULL!=dimesions); GPASSERT(number>0); mDimesions = new unsigned int[number]; ::memcpy(mDimesions, dimesions, sizeof(unsigned int)*number); mNumber = number; }
GPFormulaTreePoint* GPFormulaTreePoint::_create(const std::vector<std::string>& words, int sta, int fin, GPFormulaTreePoint* father) { GPASSERT(sta<=fin); int depth = 0; bool multi = false; for (int i=sta; i<=fin; ++i) { auto w = words[i]; if (w == "(") { depth++; } else if (w == ")") { depth--; } else if (w == "," && depth == 0) { multi = true; break; } } if (multi) { GPFormulaTreePoint* p = new GPFormulaTreePoint(OPERATOR, "SUB", father); p->_createSubPoints(words, sta, fin); return p; } GPASSERT(words[sta]!="(" && words[sta]!=")"); if (sta == fin) { return new GPFormulaTreePoint(NUM, words[sta], father); } GPASSERT(words[fin]==")" && words[sta+1]=="("); GPFormulaTreePoint* root = NULL; if ("MAP" == words[sta] || "REDUCE" == words[sta]) { root = new GPFormulaTreePoint(PARALLEL, words[sta], father); } else if ("ADF" == words[sta]) { root = new GPFormulaTreePoint(ADF, words[sta], father); } else if ("OUTPUT" == words[sta]) { root = new GPFormulaTreePoint(OUTPUT, words[sta], father); } else { root = new GPFormulaTreePoint(OPERATOR, words[sta], father); } sta = sta+2; fin = fin-1; root->_createSubPoints(words, sta, fin); return root; }
bool GPStreamADF::SP::vReceive(CONTENT con, const Point* source) { GPASSERT(NULL==source); GPASSERT(NULL!=con.get()); GPASSERT(NULL!=con->type()); GPASSERT(NULL!=con->content()); mOutputs[0]->vReceive(con, this); return true; }
static inline PFLOAT runOnePass(int k, PFLOAT v, GPPtr<GPParameter> current, IGPOptimizor::OPTFUNC& computer) { GPASSERT(NULL!=current.get()); GPASSERT(0<=k && k<current->size()); GPPtr<GPParameter> p = new GPParameter(current->size(), current->get()); PFLOAT* _p = p->attach(); _p[k] = v; return computer(p); }
bool GPStreamADF::DP::vReceive(CONTENT c, const Point* source) { GPASSERT(1 == mInputs.size()); if (NULL!=mContents.get()) { GPASSERT(mContents->type() == c->type()); } mContents = c; return false; }
GPStreamADF::GPStreamADF(const GPFunctionTree* tree) { auto root = tree->root(); auto rootfunc = root->data().pFunc; GPASSERT(NULL!=rootfunc); auto lists = root->display(); std::map<const GPAbstractPoint*, GPPtr<Point>> maplists; /*Create All Function*/ for (auto p : lists) { auto pp = (GPFunctionTreePoint*)p; if (GPFunctionTreePoint::INPUT == pp->type()) { GPPtr<Point> cp = new SP(NULL); mSources.push_back(cp); mInputPos.push_back(pp->data().iInput); maplists.insert(std::make_pair(p, cp)); } else { GPPtr<Point> cp = new CP(new GPComputePoint(pp->data().pFunc)); mFunctions.push_back((CP*)(cp.get())); maplists.insert(std::make_pair(p, cp)); } } /*Dest*/ auto rootcp = maplists.find(root)->second; for (int i=0; i<rootfunc->outputType.size(); ++i) { GPPtr<Point> dst = (new DP(rootfunc->outputType[i])); dst->connectInput(rootcp.get(), 0); rootcp->connectOutput(dst, i); mDest.push_back(dst); } /*Connect*/ for (auto p : lists) { auto PP = maplists.find(p)->second; auto func = ((GPFunctionTreePoint*)p)->data().pFunc; size_t n = p->getChildrenNumber(); GPASSERT(!(NULL!=func && n==0)); for (int i=0; i<n; ++i) { auto pc = p->getChild(i); auto PC = maplists.find(pc)->second; PP->connectInput(PC.get(), i); PC->connectOutput(PP, 0); if (pc->getChildrenNumber() == 0) { SP* s = (SP*)PC.get(); s->setType(func->inputType[i]); } } } }
size_t _computePos(unsigned int* key, int keynum) { GPASSERT(keynum <= mKeyDimesions.size() || (keynum==1 && mKeyDimesions.size()==0)); size_t sum = 0; for (int i=0; i<keynum; ++i) { sum = sum*mKeyDimesions[i] + key[i]; } GPASSERT(sum < mMaxSize); return sum; }
GPFileStream::GPFileStream(const char* filename) { GPASSERT(NULL!=filename); FILE* f = fopen(filename, "rb"); if (NULL == f) { FUNC_PRINT_ALL(filename, s); } GPASSERT(NULL!=f);//TODO mF = f; }
virtual void vSave(unsigned int* pKey, unsigned int keynum, GPContents* c) { GPASSERT(NULL!=c); GPASSERT(keynum>0); for (int i=0; i<c->size(); ++i) { auto type = c->getType(i); std::string path = generatePath(pKey, keynum, type->name()); GPPtr<GPWStream> writeStream = GPStreamFactory::NewWStream(path.c_str()); type->vSave(c->get(i), writeStream.get()); } }
virtual GPContents* vLoad(unsigned int* pKey, unsigned int keynum) { GPASSERT(!pTypes.empty()); GPContents* c = new GPContents; GPASSERT(keynum>0); for (int i=0; i<pTypes.size(); ++i) { std::string path = generatePath(pKey, keynum, pTypes[i]->name()); GPPtr<GPStream> readStream = GPStreamFactory::NewStream(path.c_str()); GPASSERT(NULL!=readStream.get()); c->push(pTypes[i]->vLoad(readStream.get()), pTypes[i]); } return c; }
virtual GPPieces* vPrepare(GPPieces** inputs, int inputNumber) const override { GPASSERT(NULL!=inputs); GPASSERT(inputNumber>0); std::vector<unsigned int> keyDms; for (auto& p : mOutputKey) { GPASSERT(p.first < inputNumber);//TODO GPPieces* inp = inputs[p.first]; GPASSERT(inp->nKeyNumber > p.second);//TODO keyDms.push_back(inp->pKeySize[p.second]); } return GPPieceFactory::createMemoryPiece(keyDms); }
GPBlock* GPBlock::merge(GPBlock* b) { GPASSERT(NULL!=b); /*Compute size*/ size_t sum = 0; const GPBlock* iter = b; while (NULL!=iter) { sum+=iter->size(); iter = iter->getNext(); } auto res = new GPBlock(sum+1); iter = b; auto rescontent = res->contents(); size_t cur = 0; while (NULL!=iter) { auto content = iter->contents(); ::memcpy(rescontent+cur, content, iter->size()); cur += iter->size(); iter = iter->getNext(); } rescontent[sum] = 0; return res; }
void GPFormulaTreePoint::render(std::ostream& output) const { std::string type; switch (mT) { case NUM: type = "Num"; break; case OPERATOR: type = "Func"; break; case ADF: type = "ADF"; break; case PARALLEL: type = "PARALLEL"; break; default: GPASSERT(0); break; } output << "<"<<type << ">" << mName; if (!mChildren.empty()) { output <<"\n"; } for (auto p : mChildren) { GPCONVERT(GPFormulaTreePoint, p)->render(output); } output << "</"<<type << ">\n"; }
void GPFormulaTreePoint::_createSubPoints(const std::vector<std::string>& words, int sta, int fin) { _removeUnusefulBracket(words, sta, fin); int pos = sta; int part_sta = pos; int depth = 0; for (; pos<=fin; ++pos) { auto w = words[pos]; if (w == "(") { depth++; } else if (w == ")") { depth--; } else if (w == "," && depth == 0) { _createSubPoint(words, part_sta, pos-1); part_sta = pos+1; } } if (part_sta < pos) { _createSubPoint(words, part_sta, pos-1); } GPASSERT(depth == 0); }
virtual GPContents* vLoad(unsigned int* pKey, unsigned int keynum) { auto sum = _computePos(pKey, keynum); GPContents* res = mPieces[sum]; GPASSERT(NULL!=res); res->addRef(); return res; }
void GPFormulaTree::setFormula(const std::string& formula) { GP_SAFE_UNREF(mRoot); std::vector<std::string> words; divideFormula(words, formula); GPASSERT(!words.empty()); mRoot = GPFormulaTreePoint::create(words); }
bool GPComputePoint::receive(GPContents* inputs, int n) { GPASSERT(NULL!=inputs); GPASSERT(1 == inputs->size()); GPASSERT(0<=n && n<mCache.size()); if (NULL == mCache.get(n)) { mCache.contents[n] = inputs->contents[0]; } else { auto dst = mCache[n]; auto src = (*inputs)[0]; GPASSERT(src.type == dst.type); GPASSERT(NULL!=src.type); GPASSERT(NULL!=src.content); GPASSERT(NULL!=dst.content); if (mFlags[n]) { dst.type->vMerge(dst.content, src.content); } else { /*Refresh*/ dst.type->vFree(dst.content); mCache.contents[n].content = src.content; } } mComplte = _computeCompleteStatus(); inputs->releaseForFree(); return mComplte; }
GPFormulaTreePoint* GPFormulaTreePoint::_ParallelExpand(const std::vector<std::string>& words, int sta, int fin) { _removeUnusefulBracket(words, sta, fin); switch (mChildren.size()) { case 0://INPUT { GPFormulaTreePoint* p = new GPFormulaTreePoint(OPERATOR, "INPUT", this); p->_createSubPoints(words, sta, fin); return p; } case 1://Formula { return _create(words, sta, fin, this); } case 2://KeyMap { GPFormulaTreePoint* p= new GPFormulaTreePoint(OPERATOR, "KEYMAP", this); /*Find -> */ for (int i=sta+1; i<fin; ++i) { if (words[i] == "->") { GPFormulaTreePoint* input = new GPFormulaTreePoint(OPERATOR, "INPUTKEY", p); input->_createSubPoints(words, sta, i-1); GPFormulaTreePoint* output = new GPFormulaTreePoint(OPERATOR, "OUTPUTKEY", p); output->_createSubPoints(words, i+1, fin); p->addPoint(input); p->addPoint(output); break; } } GPASSERT(p->getChildrenNumber() > 0); return p; } case 3://Condition { return _mergeStringForPoint(words, sta, fin, this); } default: GPASSERT(0); break; } return NULL; }
adpatergp(IGPAutoDefFunction* base, const std::vector<size_t>& order, const std::vector<const IStatusType*> inputs) { GPASSERT(NULL!=base); base->addRef(); mBase = base; mInputOrder = order; mOutputTypes = base->getOutputTypes(); mInputTypes = inputs; }
bool GPStreamADF::TP::vReceive(CONTENT c, const Point* source) { GPASSERT(source == mInputs[0]); for (auto p : mOutputs) { p->vReceive(c, this); } return true; }
GPFileWStream::GPFileWStream(const char* name) { FILE* f = fopen(name, "wb"); if (NULL == f) { FUNC_PRINT_ALL(name, s); } GPASSERT(NULL!=f); mF = f; }
GPFLOAT GPParticleSwarmOpt::Particle::distance(GPPtr<Particle> mP) { GPASSERT(mN == mP->mN); GPFLOAT* x1 = mX; GPFLOAT* x2 = mP->mX; GPFLOAT dis = 0; for (int i=0; i<mN; ++i) { dis+= ((x1[i]-x2[i])*(x1[i]-x2[i])); } return dis; }
GPContents* GPComputePoint::compute() { GPASSERT(NULL!=mF); GPASSERT(mComplte); GPContents* dst = NULL; if (mComplte) { GPContents inputs; for (auto c : mCache.contents) { inputs.push(c); } for (auto s : mStatus) { inputs.push(s->content(), s->type()); } dst = mF->basic(&inputs); mCache.clearContents(); mComplte = _computeCompleteStatus(); } return dst; }
bool GPStreamADF::CP::vReceive(CONTENT c, const Point* source) { for (int i=0; i<mInputs.size(); ++i) { if (mInputs[i] == source) { if (mPoint->receive(c, i) && mOpen) { auto comp = mPoint->compute(); GPASSERT(comp->size() == mOutputs.size()); for (int j=0; j<mOutputs.size(); ++j) { mOutputs[j]->vReceive(comp->getContent(j), this); } delete comp; mOpen = false; } return true; } } GPASSERT(0); return false; }
bool GPCarryVaryGroup::next(unsigned int* enums, unsigned int number) { GPASSERT(number == mNumber); enums[0]++; for (int i=0; i<number-1; ++i) { if (enums[i] < mDimesions[i]) { return true; } enums[i] = 0; enums[i+1]++; } return enums[number-1] < mDimesions[number-1]; }
void GPParticleSwarmOpt::Particle::move(GPPtr<Particle> best) { GPASSERT(mN == best->mN); GPFLOAT* x = mX; GPFLOAT* p = mP; GPFLOAT* v = mV; GPFLOAT* pg = best->mP; for (int i=0; i<mN; ++i) { GPFLOAT u1 = GPRandom::rate()*the1; GPFLOAT u2 = GPRandom::rate()*the2; v[i] = (v[i] + u1*(p[i]-x[i]) + u2*(pg[i]-x[i]))*0.718; x[i] = v[i] + x[i]; if (x[i] > Max) x[i] = Max; if (x[i] < -Max) x[i] = -Max; } }
void GPFormulaTreePoint::replaceNameByPoint(const std::string& name, GPFormulaTreePoint* point) { GPASSERT(NUM!=mT); for (int i=0; i<mChildren.size(); ++i) { auto p = GPCONVERT(GPFormulaTreePoint, mChildren[i]); if (p->mT == NUM && p->mName == name) { point->addRef(); mChildren[i] = point; p->decRef(); } else if (p->mChildren.size() > 0) { p->replaceNameByPoint(name, point); } } }