Esempio n. 1
0
    glmerResp::glmerResp(Rcpp::S4 xp) throw (std::runtime_error)
	: merResp(xp),
	  d_fam(SEXP(xp.slot("family"))),
	  d_n(       xp.slot("n")),
	  d_eta(     xp.slot("eta")) {
	updateWts();
    }
Esempio n. 2
0
static route
convertSPtoRoute (sgraph* g, snode* fst, snode* lst)
{
    route rte;
    snode* ptr;
    snode* next;
    snode* prev;  /* node in shortest path just previous to next */
    int i, sz = 0;
    cell* cp;
    cell* ncp;
    segment seg;
    double fix, b1, b2;
    int l1, l2;
    pointf bp1, bp2, prevbp;  /* bend points */

	/* count no. of nodes in shortest path */
    for (ptr = fst; ptr; ptr = N_DAD(ptr)) sz++;
    rte.n = 0;
    rte.segs = N_NEW(sz-2, segment);  /* at most sz-2 segments */

    seg.prev = seg.next = 0;
    ptr = prev = N_DAD(fst);
    next = N_DAD(ptr);
    if (IsNode(ptr->cells[0]))
	cp = ptr->cells[1];
    else
	cp = ptr->cells[0];
    bp1 = sidePt (ptr, cp);
    while (N_DAD(next)!=NULL) {
	ncp = cellOf (prev, next);
	updateWts (g, ncp, N_EDGE(ptr));

        /* add seg if path bends or at end */
	if ((ptr->isVert != next->isVert) || (N_DAD(next) == lst)) {
	    if (ptr->isVert != next->isVert)
		bp2 = midPt (ncp);
	    else
		bp2 = sidePt(next, ncp);
	    if (ptr->isVert) {   /* horizontal segment */
		if (ptr == N_DAD(fst)) l1 = B_NODE;
		else if (prevbp.y > bp1.y) l1 = B_UP;
		else l1 = B_DOWN; 
		if (ptr->isVert != next->isVert) {
		    if (next->cells[0] == ncp) l2 = B_UP;
		    else l2 = B_DOWN;
		}
		else l2 = B_NODE;
		fix = cp->bb.LL.y;
		b1 = cp->bb.LL.x;
		b2 = ncp->bb.LL.x;
	    }
	    else {   /* vertical segment */
		if (ptr == N_DAD(fst)) l1 = B_NODE;
		else if (prevbp.x > bp1.x) l1 = B_RIGHT;
		else l1 = B_LEFT; 
		if (ptr->isVert != next->isVert) {
		    if (next->cells[0] == ncp) l2 = B_RIGHT;
		    else l2 = B_LEFT;
		}
		else l2 = B_NODE;
		fix = cp->bb.LL.x;
		b1 = cp->bb.LL.y;
		b2 = ncp->bb.LL.y;
	    }
	    setSeg (&seg, !ptr->isVert, fix, b1, b2, l1, l2);
	    rte.segs[rte.n++] = seg;
	    cp = ncp;
	    prevbp = bp1;
	    bp1 = bp2;
	    if ((ptr->isVert != next->isVert) && (N_DAD(next) == lst)) {
		bp2 = sidePt(next, ncp);
		l2 = B_NODE;
		if (next->isVert) {   /* horizontal segment */
		    if (prevbp.y > bp1.y) l1 = B_UP;
		    else l1 = B_DOWN; 
		    fix = cp->bb.LL.y;
		    b1 = cp->bb.LL.x;
		    b2 = ncp->bb.LL.x;
		}
		else {
		    if (prevbp.x > bp1.x) l1 = B_RIGHT;
		    else l1 = B_LEFT; 
		    fix = cp->bb.LL.x;
		    b1 = cp->bb.LL.y;
		    b2 = ncp->bb.LL.y;
		}
		setSeg (&seg, !next->isVert, fix, b1, b2, l1, l2);
		rte.segs[rte.n++] = seg;
	    }
	    ptr = next;
	}
	prev = next;
	next = N_DAD(next);
    }

    rte.segs = realloc (rte.segs, rte.n*sizeof(segment));
    for (i=0; i<rte.n; i++) {
	if (i > 0)
	    rte.segs[i].prev = rte.segs + (i-1);
	if (i < rte.n-1)
	    rte.segs[i].next = rte.segs + (i+1);
    }

    return rte;
}
Esempio n. 3
0
void GradFragment::initRnd(InstInfo& t) {
	const vector<int>& fTypes = PointRep::_brInstance->_featureTypes;

	int numWts = _RandOf(PointRep::_brInstance->_atomSymbols) + 1;
	_indexes.resize(numWts);
	_values.resize(numWts);
	_operations.resize(numWts);
	_weightVal.resize(numWts);
	_biasParam._val = _gRand(SMRNDSD);

	const vector<FValue>& vec = static_cast<PtInstInfo&>(t)._features;
	vector<double> fts(numWts);
	for (int n=0; n<numWts; ++n) {
		int idx = _RandOf(fTypes.size());
		_indexes[n] = idx;
		if (fTypes[idx] == iClass) {
			_operations[n] = eq;
			if (! vec.empty()) _values[n] = vec.at(idx);
			else {
				const vector<int>& fClasses = PointRep::_brInstance->_featureClasses;
				_values[n].iVal = _RandOf(fClasses[idx]);
			}
			continue;
		} else {
			// grab from state to match feature
			// define sigmoid activation
			_operations[n] = other;
			double val;
			if (! vec.empty()) {
				if (fTypes[idx] == iSeq) val = vec.at(idx).iVal;
				else if (fTypes[idx] == fSeq) val = vec.at(idx).fVal;
			} else {
				vector<pair<FValue, FValue> >& fRanges = PointRep::_brInstance->_featureRanges;
				double min, max;
				if (fTypes[idx] == iSeq) {
					min = fRanges[idx].first.iVal;
					max = fRanges[idx].second.iVal;
				} else {
					min = fRanges[idx].first.fVal;
					max = fRanges[idx].second.fVal;
				}

				val = _fRand() * (max - min) + min;
			}
			fts[n] = val;

			// initialise random
			_weightVal[n]._val = _gRand(SMRNDSD);
		}
	}

	// adjust so gives positive response
	int ttl = MAXADJITER;
	double act, sum = 0.0;
	for (int n=0; n<numWts; ++n) {
		if (_operations[n] == eq) sum += 1.0;
		sum += _weightVal[n]._val * fts[n];
	}
	act = NeurFn(sum + _biasParam._val);

	//double margin = 0.1 * (Params::_MAXTARGET - Params::_MINTARGET);
	double target = (Params::_MAXTARGET - PointRep::_brInstance->_matchThreshold) / 2.0;
	double margin = (target - PointRep::_brInstance->_matchThreshold) / 2.0;
	double delta = act - target;
	while (fabs(delta) > margin && ttl--) {
		//double delta = act - Params::_MAXTARGET;
		double grad = delta * NeurDelta(act);
		updateWts(grad, _biasParam, FASTLR);
		for (int n=0; n<numWts; ++n) {
			if (_operations[n] != other) continue;
			updateWts(grad * fts[n], _weightVal[n], FASTLR);
		}

		// find output again
		sum = 0.0;
		for (int n=0; n<numWts; ++n) {
			if (_operations[n] == eq) sum += 1.0;
			sum += _weightVal[n]._val * fts[n];
		}
		act = NeurFn(sum + _biasParam._val);
		delta = act - target;
	}

}