Beispiel #1
0
void BackProp::predict(const std::vector<double>& features, std::vector<double>& labels)
{
	assert(labels.size() == 1);
	assert(_nLayers > 0);
	assert(features.size() == _layers[0].getNumUnits());

	if(_loggingOn)
	{
		std::cout << "Input features: " << vectorToString(features) << std::endl;
	}

	std::vector<double> finalLayerOutput;
	_layers[0].predict(features, finalLayerOutput);

	if(_loggingOn)
	{
		std::cout << "Prediction: " << vectorToString(finalLayerOutput) << std::endl;
	}

	size_t maxLabel = 0;
	double maxLabelOuput = -5.0;
	for(size_t labelIndex = 0; labelIndex < finalLayerOutput.size(); labelIndex++)
	{
		if(finalLayerOutput[labelIndex] > maxLabelOuput)
		{
			maxLabel = labelIndex;
			maxLabelOuput = finalLayerOutput[labelIndex];
		}
	}

	labels[0] = maxLabel;
}
Beispiel #2
0
void Functions::toString(Aurora::NWScript::FunctionContext &ctx) {
	ctx.getReturn() = "";

	switch (ctx.getParams()[0].getType()) {
		case Aurora::NWScript::kTypeInt:
			intToString(ctx);
			break;

		case Aurora::NWScript::kTypeFloat:
			floatToString(ctx);
			break;

		case Aurora::NWScript::kTypeString:
			ctx.getReturn() = ctx.getParams()[0];
			break;

		case Aurora::NWScript::kTypeObject:
			objectToString(ctx);
			break;

		case Aurora::NWScript::kTypeVector:
			vectorToString(ctx);
			break;

		default:
			break;
	}
}
	bool setOption(T &option, std::string flagStr,
			const std::string &shortDescription, bool required = false) {
		flag currentFlag = flag(option, flagStr, shortDescription, required);
		bool found = false;
		for (const auto &fTok : currentFlag.flags_) {
			if (commands_.lookForOption(option, fTok)) {
				currentFlag.setValue(option);
				found = true;
				break;
			} else {
				found = false;
			}
		}

		if (required && !found) {
			std::stringstream tempStream;
			tempStream << bashCT::bold + bashCT::black << "Need to have "
					<< bashCT::red << vectorToString(tokenizeString(flagStr, ","), " or ")
					<< bashCT::black << " see " << bashCT::red
					<< commands_["-program"] + " -help " << bashCT::black
					<< "for more details" << bashCT::reset;
			warnings_.emplace_back(tempStream.str());
			failed_ = true;
		}
		flags_.addFlag(currentFlag);
		return found;
	}
Beispiel #4
0
 string replaceWords(vector<string>& dict, string sentence) {
     if (sentence.empty()) return sentence;
     
     auto vec = stringToVector(sentence);
     replaceByRoot(vec, dict);
     return vectorToString(vec);
 }
Beispiel #5
0
void ExtractionStator::outStatsPerName(std::ostream & out, const std::string & delim){

	for(auto & mid : counts_){
		uint32_t totalReads = mid.second[true].getTotal() + mid.second[false].getTotal();
		uint32_t totalGoodReads = mid.second[true].good_ + mid.second[false].good_;
		uint32_t totalBadReads = mid.second[true].bad_ + mid.second[false].bad_;
		uint32_t totalContam = mid.second[true].contamination_ + mid.second[false].contamination_;
		uint32_t totalBadRev = mid.second[true].badReverse_ + mid.second[false].badReverse_;
		uint32_t totalConN = mid.second[true].containsNs_ + mid.second[false].containsNs_;
		uint32_t totalMinLen = mid.second[true].minLenBad_ + mid.second[false].minLenBad_;
		uint32_t totalMaxLen = mid.second[true].maxLenBad_ + mid.second[false].maxLenBad_;
		uint32_t totalQualFail = mid.second[true].qualityFailed_ + mid.second[false].qualityFailed_;
		out << vectorToString(toVecStr(mid.first, totalReads,
				getPercentageString(totalGoodReads, totalReads),
				getPercentageString(mid.second[false].good_, totalGoodReads),
				getPercentageString(mid.second[true].good_, totalGoodReads),
				getPercentageString(totalBadReads, totalReads),
				getPercentageString(totalBadRev, totalBadReads),
				getPercentageString(totalConN, totalBadReads),
				getPercentageString(totalMinLen, totalBadReads),
				getPercentageString(totalMaxLen, totalBadReads),
				getPercentageString(totalQualFail, totalBadReads),
				getPercentageString(totalContam, totalReads)), delim) << "\n";

	}
}
void HighScoreComponentTest::testFull()
{
	expectedScore.clear();
	expectedScore.push_back(1000);
	expectedScore.push_back(900);
	expectedScore.push_back(800);
	expectedScore.push_back(700);
	expectedScore.push_back(600);
	expectedScore.push_back(500);
	expectedScore.push_back(400);
	expectedScore.push_back(300);
	expectedScore.push_back(200);
	expectedScore.push_back(100);

	_highScoreComponentFull->submitScore(200);
	_highScoreComponentFull->submitScore(300);
	_highScoreComponentFull->submitScore(400);
	_highScoreComponentFull->submitScore(1000);
	_highScoreComponentFull->submitScore(600);
	_highScoreComponentFull->submitScore(100);
	_highScoreComponentFull->submitScore(500);
	_highScoreComponentFull->submitScore(700);
	_highScoreComponentFull->submitScore(900);
	_highScoreComponentFull->submitScore(800);
	_highScoreComponentFull->submitScore(50);
	CPPUNIT_ASSERT_MESSAGE(vectorToString(expectedScore, _highScoreComponentFull->scores()), expectedScore == _highScoreComponentFull->scores());
}
Beispiel #7
0
	void OperatorControl()
	{
		float xJoy, yJoy, gyroVal, angle = 0, turn = 0, angleDiff, turnPower;
		gyro.Reset();
		gyro.SetSensitivity(9.7);
		while (IsEnabled() && IsOperatorControl()) // loop as long as the robot is running
		{
			yJoy = xbox.getAxis(Xbox::L_STICK_V);
			xJoy = xbox.getAxis(Xbox::R_STICK_H);			
			gyroVal = gyro.GetAngle()/0.242*360;
			turn = 0.15;
			angle = angle - xJoy * xJoy * xJoy * turn;
			angleDiff = mod(angle - gyroVal, 360);
			turnPower = - mod(angleDiff / 180.0 + 1.0, 2) + 1.0;
			SmartDashboard::PutString("Joy1", vectorToString(xJoy, yJoy));
			SmartDashboard::PutNumber("Heading", mod(gyroVal, 360));
			SmartDashboard::PutNumber("Turn Power", turnPower);
			SmartDashboard::PutBoolean("Switch is ON:", dumperSwitch.Get());
			
			if (!xbox.isButtonPressed(Xbox::R))
			{
				drive.ArcadeDrive(yJoy * yJoy * yJoy, turnPower * fabs(turnPower), false);				
			}
		}
	}
void HighScoreComponentTest::testZero()
{
	expectedScore.clear();

	_highScoreComponentZero->submitScore(0);

	CPPUNIT_ASSERT_MESSAGE(vectorToString(expectedScore, _highScoreComponentZero->scores()), expectedScore == _highScoreComponentZero->scores());
}
Beispiel #9
0
std::string mismatch::outputInfoString() const {
  std::stringstream out;

  if (transition) {
    out << "transition\t";
  } else {
    out << "transversion\t";
  }
  out << refBasePos << "\t" << refBase << "\t" << refQual << "\t"
      << vectorToString(refLeadingQual, ",") << "\t"
      << vectorToString(refTrailingQual, ",") << "\t" << seqBasePos << "\t"
      << seqBase << "\t" << seqQual << "\t"
      << vectorToString(seqLeadingQual, ",") << "\t"
      << vectorToString(seqTrailingQual, ",") << "\t" << kMerFreqByPos << "\t"
      << kMerFreq;
  return out.str();
}
Beispiel #10
0
string 
SearchSpace::
getNthTranslation(int nth){
	if(nth<1)return "";
	string result="";
	if((int)_hypoHeaps.back().size()<nth)
		return "";
	return vectorToString(_hypoHeaps.back()[nth-1].translation);
}
Beispiel #11
0
string MistarParser::getHeaderNoDefline(string prefix){
    vector<string> fields=allTokens(headerNoDefline,'\n');
    vector<string> toreturn;
    for(unsigned int i=0;i<fields.size();i++){
	if(!fields[i].empty())
	    toreturn.push_back(prefix+fields[i]);
    }

    return vectorToString(toreturn,"\n");
}
Beispiel #12
0
string terShift::toString()
{
    stringstream s;
    s.str ( "" );
    s << "[" << start << ", " << end << ", " << moveto << "/" << newloc << "]";
    if ( ( int ) shifted.size() > 0 ) {
        s << " (" << vectorToString ( shifted ) << ")";
    }
    return s.str();
}
void VelocityTracker::addMovement(nsecs_t eventTime, BitSet32 idBits, const Position* positions) {
    while (idBits.count() > MAX_POINTERS) {
        idBits.clearLastMarkedBit();
    }

    if ((mCurrentPointerIdBits.value & idBits.value)
            && eventTime >= mLastEventTime + ASSUME_POINTER_STOPPED_TIME) {
#if DEBUG_VELOCITY
        ALOGD("VelocityTracker: stopped for %0.3f ms, clearing state.",
                (eventTime - mLastEventTime) * 0.000001f);
#endif
        // We have not received any movements for too long.  Assume that all pointers
        // have stopped.
        mStrategy->clear();
    }
    mLastEventTime = eventTime;

    mCurrentPointerIdBits = idBits;
    if (mActivePointerId < 0 || !idBits.hasBit(mActivePointerId)) {
        mActivePointerId = idBits.isEmpty() ? -1 : idBits.firstMarkedBit();
    }

    mStrategy->addMovement(eventTime, idBits, positions);

#if DEBUG_VELOCITY
    ALOGD("VelocityTracker: addMovement eventTime=%" PRId64 ", idBits=0x%08x, activePointerId=%d",
            eventTime, idBits.value, mActivePointerId);
    for (BitSet32 iterBits(idBits); !iterBits.isEmpty(); ) {
        uint32_t id = iterBits.firstMarkedBit();
        uint32_t index = idBits.getIndexOfBit(id);
        iterBits.clearBit(id);
        Estimator estimator;
        getEstimator(id, &estimator);
        ALOGD("  %d: position (%0.3f, %0.3f), "
                "estimator (degree=%d, xCoeff=%s, yCoeff=%s, confidence=%f)",
                id, positions[index].x, positions[index].y,
                int(estimator.degree),
                vectorToString(estimator.xCoeff, estimator.degree + 1).c_str(),
                vectorToString(estimator.yCoeff, estimator.degree + 1).c_str(),
                estimator.confidence);
    }
#endif
}
std::ostream& operator<< (std::ostream& os, const JointGoal& j) {
    os << "time_since_start(" << j.time_since_start << "), " <<
          "sub_goal_idx(" << j.sub_goal_idx << "), " <<
          "joint_index_mapping(" << vectorToString(j.joint_index_mapping) << "), " <<
          "goal_msg(" << j.goal_msg << "), " <<
          "num_goal_joints(" << j.num_goal_joints << "), " <<
          "use_cubic_interpolation(" << j.use_cubic_interpolation << "), " <<
          "status(" << j.status << ")";
    return os;
}
Beispiel #15
0
        std::string polylineToString(Polyline const & polyline, Layout const & layout) const
        {
            Polyline shifted_polyline = polyline;
            shifted_polyline.offset(Point(margin.width, margin.height));

            std::vector<Circle> vertices;
            for (unsigned i = 0; i < shifted_polyline.points.size(); ++i)
                vertices.push_back(Circle(shifted_polyline.points[i], getDimensions()->height / 30.0, Color::Black));

            return shifted_polyline.toString(layout) + vectorToString(vertices, layout);
        }
Beispiel #16
0
std::string Scene::toString() const {
    return tfm::format(
        "Scene[\n"
        "  settings = %s,\n"
        "  camera = %s,\n"
        "  world = %s,\n"
        "  boxes = %s,\n"
        "  spheres = %s,\n"
        "  meshes = %s\n,"
        "  cameraKeyframes = %s\n"
        "]",
        indent(settings.json().dump()),
        indent(camera.toString()),
        indent(world.toString()),
        indent(vectorToString(boxes)),
        indent(vectorToString(spheres)),
        indent(vectorToString(meshes)),
        indent(vectorToString(cameraKeyframes))
    );
}
Beispiel #17
0
void 
Hypothesis::
print(ostream& os)
{
	os<<represent<<" : "<<vectorToString(translation)<<" ||| "<<currentScore<<" "<<estimatedScore<<endl;
	os<<"\t";features.print(os);os<<endl;
	os<<"\t";
	if(trace.p_prev!=NULL)
		os<<trace.p_prev->represent;
	os<<" ||| ";
	if(trace.p_rule!=NULL)
		os<<(*trace.p_rule).srcPhrase<<" => "<<(*trace.p_rule).tarPhrase<<" ||| "<<(*trace.p_rule);
	os<<endl;
}
void HighScoreComponentTest::testSortDesc()
{
	expectedScore.clear();
	expectedScore.push_back(500);
	expectedScore.push_back(400);
	expectedScore.push_back(300);
	expectedScore.push_back(200);

	_highScoreComponentSortDesc->submitScore(500);
	_highScoreComponentSortDesc->submitScore(400);
	_highScoreComponentSortDesc->submitScore(300);
	_highScoreComponentSortDesc->submitScore(200);
	CPPUNIT_ASSERT_MESSAGE(vectorToString(expectedScore, _highScoreComponentSortDesc->scores()), expectedScore == _highScoreComponentSortDesc->scores());
}
Beispiel #19
0
CCArray* GlobalData::getAllCardProfile(CardQueryCriteria *query)
{
	arrayCards = CCArray::create();

	sqlite3 *pDB = NULL;
	char* errMsg = NULL;
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
	std::string dbPath = getWritablePath();
#else
	std::string dbPath = CCFileUtils::sharedFileUtils()->fullPathForFilename("card.s3db");
#endif
	int result = sqlite3_open_v2(dbPath.c_str(),&pDB,SQLITE_OPEN_READWRITE, NULL);
	if (result!=SQLITE_OK) {
		return NULL;
	}

	std::string szSql = "select c.cardHeadImg,c.cardBodyImg,c.cardProfileImg,c.game_group_id,r.roleName,r.starGrade,r.beginGrade,r.blood,r.attack,r.defence,r.crit,r.dodge,r.roleDescription from card c left join game_role r on c.cardProfileImg = r.roleImageId where r.starGrade is not null";
	if (query != NULL)
	{
		if (query->groupid >= 0)
		{

		}
		if (query->cardName.length()>0)
		{
			std::vector<std::string> slist = split(query->cardName,';');
			if (slist.size()<=1)
			{
				szSql.append(" AND c.cardProfileImg = '");
				szSql.append(slist[0]);
				szSql.append("'");
			} else {
				szSql.append(" AND c.cardProfileImg in (\'");
				std::string sresult = vectorToString(slist,"\',\'");
				szSql.append(sresult);
				szSql.append("\')");
			}
		}
	}

	const char *argc = QUERY_CARD_PROFILE_ALL;
	result = sqlite3_exec(pDB,szSql.c_str(), sqliteExecCallBack, (void *)argc, &errMsg);
	if (result != SQLITE_OK) {
		return NULL;
	}

	return arrayCards;
}
Beispiel #20
0
double BackPropUnit::getOutput(const std::vector<double>& features) const
{
	if(_loggingOn)
	{
		std::cout << "Unit weights: " << vectorToString(_weights) << std::endl;
	}

	double sum = 0.0;
	assert(features.size() == _weights.size() - 1);
	for(size_t i = 0; i < features.size(); i++)	
		sum += _weights[i] * features[i];

	sum += _weights[_weights.size() - 1];

	double output = 1 / (1 + exp(-sum));
	return output; 
}
Beispiel #21
0
////////////////////////////////////////////////////////////////////
// for debug
void CTree::dfs(CNode *x, std::ostream& out, int8_t depth) {
    if(x == NULL) {
        return;
    }

    char str[512];
    memset(str, 0, 512);
    string label(txt + x->id, x->len);
    for (int i = 0; i < kmerLen; ++i) {
        if(shape[i] == '1') {
            int id = i - depth;
            if(id > 0 && id < x->len) label[id] = '_';
        }
    }

    if(x->leaf) {
        string poss = vectorToString(((CLeaf *) x)->pos);
        sprintf(str, "\t%d [label=\"%s (%s)\"];\n", x, label.c_str(),
                poss.c_str());
        out << str;
        return;
    } else {
        sprintf(str, "\t%d [label=\"%s\"];\n", x, label.c_str());
        out << str;
    }

    CInnerNode* inner = (CInnerNode*) x;
    for (uint8_t i = 0; i < CNode::SIGMA; ++i) {
        if(inner->children[i]) {
            memset(str, 0, 512);
            // edges
            sprintf(str, "\t%d -- %d;\n", inner, inner->children[i]);
            out << str;
        }
    }

    for (uint8_t i = 0; i < 4; ++i) {
        if(inner->children[i]) {
            dfs(inner->children[i], out, depth + inner->len);
        }
    }
}
Beispiel #22
0
void ExtractionStator::outTotalStats(std::ostream & out, const std::string & delim) {
	uint32_t totalBadReads = 0;
	uint32_t totalGoodReads = 0;
	uint32_t totalContam = 0;
	uint32_t totalFailedForward = 0;
	for(auto & ff : failedForward_){
		totalFailedForward += ff.second[true] + ff.second[false];
	}
	for (auto & mid : counts_) {
		totalGoodReads += mid.second[true].good_ + mid.second[false].good_;
		totalBadReads += mid.second[true].bad_ + mid.second[false].bad_;
		totalContam += mid.second[true].contamination_
				+ mid.second[false].contamination_;
	}
	out << vectorToString(toVecStr(totalReadCount_,
			getPercentageString(readsUnrecBarcode_, totalReadCount_),
			getPercentageString(readsUnrecBarcodePosContamination_, totalReadCount_),
			getPercentageString(smallFrags_, totalReadCount_),
			getPercentageString(totalFailedForward, totalReadCount_),
			getPercentageString(totalBadReads, totalReadCount_),
			getPercentageString(totalGoodReads, totalReadCount_),
			getPercentageString(totalContam, totalReadCount_)), delim) << "\n";
}
    void multiTxtDocument::loadFile ( string fileName, bool caseOn,  bool noPunct, bool debugMode, bool noTxtIds, bool tercomLike )
    {
        if ( multiTxtDocumentParams.debugMode )
        {
            cerr << "DEBUG tercpp : multiTxtDocument::loadFile : loading files  " << endl << fileName << endl << "END DEBUG" << endl;
            cerr << "DEBUG tercpp : multiTxtDocument::loadFile : testing params  " << endl << Tools::printParams ( multiTxtDocumentParams ) << endl << "END DEBUG" << endl;
            cerr << "DEBUG tercpp : multiTxtDocument::loadFile : testing others params  " << endl << "caseOn : " << caseOn << endl << "noPunct : " << noPunct << endl << "debugMode : " << debugMode << endl << "noTxtIds : " << noTxtIds << endl << "tercomLike : " << tercomLike << endl << "END DEBUG" << endl;
        }

        ifstream fichierLoad ( fileName.c_str(), ios::in );
        string line="";
        documentStructure l_doc;
	stringstream l_stream;
	l_doc.setFileName(fileName);
	l_stream.str ( "" );
	l_stream << ( int ) documents.size();
	l_doc.setDocId ( l_stream.str() );
        if ( fichierLoad )
        {
            int l_ids = 1;
	    l_stream.str ( "" );
	    string l_key="";
	    string line_mod="";
            while ( getline ( fichierLoad, line ) )
            {
		l_key="";
		line_mod="";
                l_stream.str ( "" );

                if ( noTxtIds )
                {
                    l_stream << l_ids;
                    l_key = l_stream.str();
                    line_mod = line;
                    l_ids++;
                }
                else
                {
		    if ((int)line.rfind ( "(" )==-1)
		    {
			cerr << "ERROR : multiTxtDocument::loadFile : Id not found, maybe you should use the --noTxtIds Option ? " << endl;
			exit ( 0 );
		    }
                    l_key = line.substr ( line.rfind ( "(" ), line.size() - 1 );
                    line_mod = line.substr ( 0, line.rfind ( "(" ) - 1 );
                }
                if ( multiTxtDocumentParams.debugMode )
                {
                    cerr << "DEBUG multiTxtDocument::loadFile : line NOT tokenized |" << line_mod << "|" << endl << "END DEBUG" << endl;
                }
                if ( !tercomLike )
                {
                    if ( multiTxtDocumentParams.debugMode )
                    {
                        cerr << "DEBUG tercpp : multiTxtDocument::loadFile : " << endl << "TERCOM AT FALSE " << endl << "END DEBUG" << endl;
                    }

//                    line_mod = tokenizePunct ( line_mod );
                }
                if ( !caseOn )
                {
                    if ( multiTxtDocumentParams.debugMode )
                    {
                        cerr << "DEBUG tercpp : multiTxtDocument::loadFile : " << endl << "CASEON AT FALSE " << endl << "END DEBUG" << endl;
                    }
                    line_mod = lowerCase ( line_mod );
                }
                if ( noPunct )
                {
                    if ( multiTxtDocumentParams.debugMode )
                    {
                        cerr << "DEBUG tercpp : multiTxtDocument::loadFile : " << endl << "NOPUNCT AT TRUE " << endl << "END DEBUG" << endl;
                    }
                    if ( !tercomLike )
                    {
                        line_mod = removePunctTercom ( line_mod );
                    }
                    else
                    {
                        line_mod = removePunct ( line_mod );
                    }
                }
                if ( multiTxtDocumentParams.debugMode )
                {
                    cerr << "DEBUG multiTxtDocument::loadFile : line tokenized |" << line_mod << "|" << endl << "END DEBUG" << endl;
                }
                vector<string> vecDocLine = stringToVector ( line_mod, " " );
// 	  string l_key;
// 	  hashHypothesis.addValue(l_key,vecDocLine);
// 	  l_key=(string)vecDocLine.at((int)vecDocLine.size()-1);
// 	  vecDocLine.pop_back();
                if ( multiTxtDocumentParams.debugMode )
                {
                    cerr << "DEBUG tercpp multiTxtDocument::loadFile : " << l_key << "|" << vectorToString ( vecDocLine ) << "|" << endl << "Vector Size : " << vecDocLine.size() << endl << "Line length : " << ( int ) line_mod.length() << endl << "END DEBUG" << endl;
                }
//             hashHypothesis.addValue(l_key,vecDocLine);
                segmentStructure l_seg ( l_key, vecDocLine, l_doc.getDocId() );
                l_doc.addSegments ( l_seg );
		 
            }
	    
//         Ref=line;
//         getline ( fichierHyp, line );
//         Hyp=line;
            fichierLoad.close();  // on ferme le fichier
	    addDocument ( l_doc );
            if ( multiTxtDocumentParams.debugMode )
            {
                cerr << "DEBUG multiTxtDocument::loadFile : document " << l_doc.getDocId() << " added !!!" << endl << "END DEBUG" << endl;
            }
	    l_key.erase();
	    line_mod.erase();
            l_stream.str("");
        }
        else  // sinon
        {
            cerr << "ERROR : multiTxtDocument::loadFile : can't open file : " + fileName + " !" << endl;
            exit ( 0 );
        }
    }
/**
 * Solves a linear least squares problem to obtain a N degree polynomial that fits
 * the specified input data as nearly as possible.
 *
 * Returns true if a solution is found, false otherwise.
 *
 * The input consists of two vectors of data points X and Y with indices 0..m-1
 * along with a weight vector W of the same size.
 *
 * The output is a vector B with indices 0..n that describes a polynomial
 * that fits the data, such the sum of W[i] * W[i] * abs(Y[i] - (B[0] + B[1] X[i]
 * + B[2] X[i]^2 ... B[n] X[i]^n)) for all i between 0 and m-1 is minimized.
 *
 * Accordingly, the weight vector W should be initialized by the caller with the
 * reciprocal square root of the variance of the error in each input data point.
 * In other words, an ideal choice for W would be W[i] = 1 / var(Y[i]) = 1 / stddev(Y[i]).
 * The weights express the relative importance of each data point.  If the weights are
 * all 1, then the data points are considered to be of equal importance when fitting
 * the polynomial.  It is a good idea to choose weights that diminish the importance
 * of data points that may have higher than usual error margins.
 *
 * Errors among data points are assumed to be independent.  W is represented here
 * as a vector although in the literature it is typically taken to be a diagonal matrix.
 *
 * That is to say, the function that generated the input data can be approximated
 * by y(x) ~= B[0] + B[1] x + B[2] x^2 + ... + B[n] x^n.
 *
 * The coefficient of determination (R^2) is also returned to describe the goodness
 * of fit of the model for the given data.  It is a value between 0 and 1, where 1
 * indicates perfect correspondence.
 *
 * This function first expands the X vector to a m by n matrix A such that
 * A[i][0] = 1, A[i][1] = X[i], A[i][2] = X[i]^2, ..., A[i][n] = X[i]^n, then
 * multiplies it by w[i]./
 *
 * Then it calculates the QR decomposition of A yielding an m by m orthonormal matrix Q
 * and an m by n upper triangular matrix R.  Because R is upper triangular (lower
 * part is all zeroes), we can simplify the decomposition into an m by n matrix
 * Q1 and a n by n matrix R1 such that A = Q1 R1.
 *
 * Finally we solve the system of linear equations given by R1 B = (Qtranspose W Y)
 * to find B.
 *
 * For efficiency, we lay out A and Q column-wise in memory because we frequently
 * operate on the column vectors.  Conversely, we lay out R row-wise.
 *
 * http://en.wikipedia.org/wiki/Numerical_methods_for_linear_least_squares
 * http://en.wikipedia.org/wiki/Gram-Schmidt
 */
static bool solveLeastSquares(const float* x, const float* y,
        const float* w, uint32_t m, uint32_t n, float* outB, float* outDet) {
#if DEBUG_STRATEGY
    ALOGD("solveLeastSquares: m=%d, n=%d, x=%s, y=%s, w=%s", int(m), int(n),
            vectorToString(x, m).c_str(), vectorToString(y, m).c_str(),
            vectorToString(w, m).c_str());
#endif

    // Expand the X vector to a matrix A, pre-multiplied by the weights.
    float a[n][m]; // column-major order
    for (uint32_t h = 0; h < m; h++) {
        a[0][h] = w[h];
        for (uint32_t i = 1; i < n; i++) {
            a[i][h] = a[i - 1][h] * x[h];
        }
    }
#if DEBUG_STRATEGY
    ALOGD("  - a=%s", matrixToString(&a[0][0], m, n, false /*rowMajor*/).c_str());
#endif

    // Apply the Gram-Schmidt process to A to obtain its QR decomposition.
    float q[n][m]; // orthonormal basis, column-major order
    float r[n][n]; // upper triangular matrix, row-major order
    for (uint32_t j = 0; j < n; j++) {
        for (uint32_t h = 0; h < m; h++) {
            q[j][h] = a[j][h];
        }
        for (uint32_t i = 0; i < j; i++) {
            float dot = vectorDot(&q[j][0], &q[i][0], m);
            for (uint32_t h = 0; h < m; h++) {
                q[j][h] -= dot * q[i][h];
            }
        }

        float norm = vectorNorm(&q[j][0], m);
        if (norm < 0.000001f) {
            // vectors are linearly dependent or zero so no solution
#if DEBUG_STRATEGY
            ALOGD("  - no solution, norm=%f", norm);
#endif
            return false;
        }

        float invNorm = 1.0f / norm;
        for (uint32_t h = 0; h < m; h++) {
            q[j][h] *= invNorm;
        }
        for (uint32_t i = 0; i < n; i++) {
            r[j][i] = i < j ? 0 : vectorDot(&q[j][0], &a[i][0], m);
        }
    }
#if DEBUG_STRATEGY
    ALOGD("  - q=%s", matrixToString(&q[0][0], m, n, false /*rowMajor*/).c_str());
    ALOGD("  - r=%s", matrixToString(&r[0][0], n, n, true /*rowMajor*/).c_str());

    // calculate QR, if we factored A correctly then QR should equal A
    float qr[n][m];
    for (uint32_t h = 0; h < m; h++) {
        for (uint32_t i = 0; i < n; i++) {
            qr[i][h] = 0;
            for (uint32_t j = 0; j < n; j++) {
                qr[i][h] += q[j][h] * r[j][i];
            }
        }
    }
    ALOGD("  - qr=%s", matrixToString(&qr[0][0], m, n, false /*rowMajor*/).c_str());
#endif

    // Solve R B = Qt W Y to find B.  This is easy because R is upper triangular.
    // We just work from bottom-right to top-left calculating B's coefficients.
    float wy[m];
    for (uint32_t h = 0; h < m; h++) {
        wy[h] = y[h] * w[h];
    }
    for (uint32_t i = n; i != 0; ) {
        i--;
        outB[i] = vectorDot(&q[i][0], wy, m);
        for (uint32_t j = n - 1; j > i; j--) {
            outB[i] -= r[i][j] * outB[j];
        }
        outB[i] /= r[i][i];
    }
#if DEBUG_STRATEGY
    ALOGD("  - b=%s", vectorToString(outB, n).c_str());
#endif

    // Calculate the coefficient of determination as 1 - (SSerr / SStot) where
    // SSerr is the residual sum of squares (variance of the error),
    // and SStot is the total sum of squares (variance of the data) where each
    // has been weighted.
    float ymean = 0;
    for (uint32_t h = 0; h < m; h++) {
        ymean += y[h];
    }
    ymean /= m;

    float sserr = 0;
    float sstot = 0;
    for (uint32_t h = 0; h < m; h++) {
        float err = y[h] - outB[0];
        float term = 1;
        for (uint32_t i = 1; i < n; i++) {
            term *= x[h];
            err -= term * outB[i];
        }
        sserr += w[h] * w[h] * err * err;
        float var = y[h] - ymean;
        sstot += w[h] * w[h] * var * var;
    }
    *outDet = sstot > 0.000001f ? 1.0f - (sserr / sstot) : 1;
#if DEBUG_STRATEGY
    ALOGD("  - sserr=%f", sserr);
    ALOGD("  - sstot=%f", sstot);
    ALOGD("  - det=%f", *outDet);
#endif
    return true;
}
bool LeastSquaresVelocityTrackerStrategy::getEstimator(uint32_t id,
        VelocityTracker::Estimator* outEstimator) const {
    outEstimator->clear();

    // Iterate over movement samples in reverse time order and collect samples.
    float x[HISTORY_SIZE];
    float y[HISTORY_SIZE];
    float w[HISTORY_SIZE];
    float time[HISTORY_SIZE];
    uint32_t m = 0;
    uint32_t index = mIndex;
    const Movement& newestMovement = mMovements[mIndex];
    do {
        const Movement& movement = mMovements[index];
        if (!movement.idBits.hasBit(id)) {
            break;
        }

        nsecs_t age = newestMovement.eventTime - movement.eventTime;
        if (age > HORIZON) {
            break;
        }

        const VelocityTracker::Position& position = movement.getPosition(id);
        x[m] = position.x;
        y[m] = position.y;
        w[m] = chooseWeight(index);
        time[m] = -age * 0.000000001f;
        index = (index == 0 ? HISTORY_SIZE : index) - 1;
    } while (++m < HISTORY_SIZE);

    if (m == 0) {
        return false; // no data
    }

    // Calculate a least squares polynomial fit.
    uint32_t degree = mDegree;
    if (degree > m - 1) {
        degree = m - 1;
    }
    if (degree >= 1) {
        if (degree == 2 && mWeighting == WEIGHTING_NONE) { // optimize unweighted, degree=2 fit
            outEstimator->time = newestMovement.eventTime;
            outEstimator->degree = 2;
            outEstimator->confidence = 1;
            outEstimator->xCoeff[0] = 0; // only slope is calculated, set rest of coefficients = 0
            outEstimator->yCoeff[0] = 0;
            outEstimator->xCoeff[1] = solveUnweightedLeastSquaresDeg2(time, x, m);
            outEstimator->yCoeff[1] = solveUnweightedLeastSquaresDeg2(time, y, m);
            outEstimator->xCoeff[2] = 0;
            outEstimator->yCoeff[2] = 0;
            return true;
        }

        float xdet, ydet;
        uint32_t n = degree + 1;
        if (solveLeastSquares(time, x, w, m, n, outEstimator->xCoeff, &xdet)
                && solveLeastSquares(time, y, w, m, n, outEstimator->yCoeff, &ydet)) {
            outEstimator->time = newestMovement.eventTime;
            outEstimator->degree = degree;
            outEstimator->confidence = xdet * ydet;
#if DEBUG_STRATEGY
            ALOGD("estimate: degree=%d, xCoeff=%s, yCoeff=%s, confidence=%f",
                    int(outEstimator->degree),
                    vectorToString(outEstimator->xCoeff, n).c_str(),
                    vectorToString(outEstimator->yCoeff, n).c_str(),
                    outEstimator->confidence);
#endif
            return true;
        }
    }

    // No velocity data available for this pointer, but we do have its current position.
    outEstimator->xCoeff[0] = x[0];
    outEstimator->yCoeff[0] = y[0];
    outEstimator->time = newestMovement.eventTime;
    outEstimator->degree = 0;
    outEstimator->confidence = 1;
    return true;
}
Beispiel #26
0
void qsubMFT(JKArgs& args)
{
	//"jkparser -qsubMFT -curDir=dir -stepLength=int -nStep=int -dConfig=trainConfig -o=ruleFile [-walltime=time] [-pmem=mem] [-queue=node]"
	string curDir="";
	int stepLength=5000;
	int nStep=6;
	string dConfig="";
	int sleepTime=5;

	if(!args.is_set("curDir")||!args.is_set("stepLength")||
		!args.is_set("nStep")||!args.is_set("dConfig")||!args.is_set("o"))
		usage();
	curDir=args.value("curDir");
	stepLength=atoi(args.value("stepLength").c_str());
	nStep=atoi(args.value("nStep").c_str());
	dConfig=args.value("dConfig");
	string output=args.value("o");
	int dlevel=atoi(args.value("debug").c_str());
	if(args.is_set("sleepTime"))sleepTime=atoi(args.value("sleepTime").c_str());

	JKArgs dArgs;
	dArgs.init(dConfig.c_str());

	string walltime="20:00:00";
	string pmem="7g";
	string queue="isi";
	if(args.is_set("walltime"))
		walltime=args.value("walltime");
	if(args.is_set("pmem"))
		pmem=args.value("pmem");
	if(args.is_set("queue"))
		queue=args.value("queue");

	cerr<<"walltime="<<walltime<<endl;

	string signal="mftsignal";
	if(dArgs.is_set("signal"))signal=dArgs.value("signal");

	string logerror=output+"-logerror";
	vector<string> v_signal,v_logerror,v_log,v_sh;

	CHDIR(curDir.c_str());

	string cmd="";
	cmd="cd "+curDir;
	if(dlevel)cerr<<cmd<<endl;
	else system(cmd.c_str());

	for(int iter=0;iter<nStep;iter++)
	{
		int start=stepLength*iter+1;
		int stop=start+stepLength-1;
		string range=intToString(start)+"-"+intToString(stop);
		string n_signal=signal+"."+range;
		v_signal.push_back(n_signal);

		string n_logerror=logerror+"."+range;
		string n_log=output+"."+range;
		v_logerror.push_back(n_logerror);
		v_log.push_back(n_log);

		string fd_cmd="( ./pbmt -config="+dConfig+" -range="+range+" -signal="+n_signal;
		fd_cmd+=" > "+n_log+" ) >& "+n_logerror;

		string fd_sh=curDir+"/mft."+range+".sh";
		v_sh.push_back(fd_sh);
		cmd="echo cd "+curDir+" > "+fd_sh;
		if(dlevel)cerr<<cmd<<endl;
		else system(cmd.c_str());
		
		cmd="echo \""+fd_cmd+" \" >> "+fd_sh;
		if(dlevel)cerr<<cmd<<endl;
		else system(cmd.c_str());
		
		cmd="qsub -l walltime="+walltime+" -l pmem="+pmem+" -q "+queue+" "+fd_sh;
		if(dlevel)cerr<<cmd<<endl;
		else system(cmd.c_str());
	}

	while(true)
	{
		bool allDone=true;
		for(int iter=0;iter<nStep;iter++)
		{
			ifstream signalFile(v_signal[iter].c_str());
			if(!signalFile.good())
				allDone=false;
			signalFile.close();
		}
		if(allDone)
			break;
#ifndef WIN32
		sleep(sleepTime);
#else
		Sleep(sleepTime*100);
#endif
	}

	cmd="rm "+output+" "+logerror;
	if(dlevel)cerr<<cmd<<endl;
	else system(cmd.c_str());
	
	cmd="cat "+vectorToString(v_log)+" > "+output;
	if(dlevel)cerr<<cmd<<endl;
	else system(cmd.c_str());
	
	cmd="cat "+vectorToString(v_logerror)+" > "+logerror;
	if(dlevel)cerr<<cmd<<endl;
	else system(cmd.c_str());

	string debugInfo=output+"-debugInfo";
	cmd="mkdir "+debugInfo;
	if(dlevel)cerr<<cmd<<endl;
	else system(cmd.c_str());
	cmd="mv "+vectorToString(v_log)+" "+vectorToString(v_logerror)+" "+vectorToString(v_sh)+" "+vectorToString(v_signal)\
		+" *sh.e* *sh.o* "+debugInfo;
	if(dlevel)cerr<<cmd<<endl;
	else system(cmd.c_str());
}
Beispiel #27
0
void printVector(const std::vector<T>& vec, const std::string& delim = " ",
		std::ostream& out = std::cout) {
	out << vectorToString(vec, delim) << "\n";

}
Beispiel #28
0
void SimpleVCF::init(const vector<string> & fields, CoreVCF *  corevcf_){ //string line){


    unresolvedGT=false;
    homozygousREF=false;
    heterozygous=false;
    homozygousALT=false;

    indexGenotype=-1; 
    indexGenotypeQual=-1; 
    indexDepth=-1;    
    indexPL=-1;       

    typeOfData=1;


    // fields=allTokens(line,'\t');
    // corevcf = corevcf_;
    
    int fieldIndex  = corevcf->getFieldIndexAndIncrease();
    // cerr<<"fieldIndex "<<fieldIndex<<endl;

    //FORMAT FIELDS
    rawFormatNames  = fields[ corevcf->getFieldIndexINFO()+1 ];
    rawFormatValues = fields[fieldIndex];

    // cerr<<"rawFormatNames  "<<rawFormatNames<<endl;
    // cerr<<"rawFormatValues "<<rawFormatValues<<endl;

    formatFieldNames  = allTokens(rawFormatNames ,':');
    formatFieldValues = allTokens(rawFormatValues,':');
    
    if(rawFormatValues == "./."){
	unresolvedGT=true; 

	observedPL=false;
	observedGL=false;
	haploidCall=false;
    }else{

    if(formatFieldNames.size() != formatFieldValues.size()){
	cerr<<"SimpleVCF: for line "<<vectorToString(fields,"\t")<<" the format field does not have as many fields as the values"<<endl;
	exit(1);
    }

    observedPL=false;
    observedGL=false;
    haploidCall=false;
    for(unsigned int i=0;i<formatFieldNames.size();i++){
	 // cerr<<"formatFieldNames["<<i<<"] "<<formatFieldNames[i]<<" = "<<formatFieldValues[i]<<endl;
	if(formatFieldNames[i] == "GT"){ 
	    indexGenotype     =i; 
	    formatFieldGT=                   formatFieldValues[i]; 
	    bool determinedGenotype=false;
	    //Taken from http://www.broadinstitute.org/gatk/guide/topic?name=intro
	    if(formatFieldGT == "./."){ determinedGenotype=true; unresolvedGT=true;       }

	    if(formatFieldGT == "0"){   determinedGenotype=true; homozygousREF=true;   haploidCall=true;   }
	    if(formatFieldGT == "1"){   determinedGenotype=true; homozygousALT=true;   haploidCall=true;   }

	    if(formatFieldGT == "0/0"){ determinedGenotype=true; homozygousREF=true;      }
	    if(formatFieldGT == "0|0"){ determinedGenotype=true; homozygousREF=true;      }

	    if(formatFieldGT == "0/1"){ determinedGenotype=true; heterozygous=true;       }
	    if(formatFieldGT == "0|1"){ determinedGenotype=true; heterozygous=true;       }
	    if(formatFieldGT == "1|0"){ determinedGenotype=true; heterozygous=true;       }


	    if(formatFieldGT == "1/1"){ determinedGenotype=true; homozygousALT=true;      }
	    if(formatFieldGT == "1|1"){ determinedGenotype=true; homozygousALT=true;      }

	    if(formatFieldGT == "1/2"){ determinedGenotype=true; heterozygousALT=true;    } //has first alt and second alt
	    if(formatFieldGT == "1|2"){ determinedGenotype=true; heterozygousALT=true;    } //has first alt and second alt

	    if(formatFieldGT == "2/1"){ determinedGenotype=true; heterozygousALT=true;    } //has first alt and second alt
	    if(formatFieldGT == "2|1"){ determinedGenotype=true; heterozygousALT=true;    } //has first alt and second alt

	    if(formatFieldGT == "0|2"){ determinedGenotype=true; heterozygous2ndALT=true; } //has ref       and second alt
	    if(formatFieldGT == "0/2"){ determinedGenotype=true; heterozygous2ndALT=true; } //has ref       and second alt

	    if(formatFieldGT == "2/0"){ determinedGenotype=true; heterozygous2ndALT=true; } //has ref       and second alt
	    if(formatFieldGT == "2|0"){ determinedGenotype=true; heterozygous2ndALT=true; } //has ref       and second alt

	    if(formatFieldGT == "2/2"){ determinedGenotype=true; homozygous2ndALT=true; }   //twice the second alt
	    if(formatFieldGT == "2|2"){ determinedGenotype=true; homozygous2ndALT=true; }   //twice the second alt

	    //for more than 3

	    if(!determinedGenotype){

		vector<string> fieldsOfGT  = allTokens(formatFieldGT ,'/');
	    
		if(fieldsOfGT.size() == 2){
		    // int alleleCFirst = destringify<int>  (fieldsOfGT[0]);
		    // int alleleC2nd   = destringify<int>  (fieldsOfGT[1]);
		    if(isPositiveInt(fieldsOfGT[0]) &&
		       isPositiveInt(fieldsOfGT[1])    ){
			determinedGenotype=true; unresolvedGT=true; 
		    }		   
		}else{
		    vector<string> fieldsOfGT  = allTokens(formatFieldGT ,'|');
	    
		    if(fieldsOfGT.size() == 2){
			// int alleleCFirst = destringify<int>  (fieldsOfGT[0]);
			// int alleleC2nd   = destringify<int>  (fieldsOfGT[1]);
			if(isPositiveInt(fieldsOfGT[0]) &&
			   isPositiveInt(fieldsOfGT[1])   ){
			    determinedGenotype=true; unresolvedGT=true; 
			}		   
		    }else{

		    }

		}
	    }
	    // if(formatFieldGT == "0/3" ||
	    //    formatFieldGT == "3/3" ||
	    //    formatFieldGT == "3/3" ||
	       
	       

	    //    ){ determinedGenotype=true; unresolvedGT=true; 

	    if(!determinedGenotype){
		cerr<<"SimpleVCF: unable to determine genotype for line "<<vectorToString(fields,"\t")<<" field=#"<<formatFieldGT<<"#"<<endl;
		exit(1);
	    }
	    continue;
	}

	if(formatFieldNames[i] == "GQ"){ 
	    if(formatFieldValues[i] == "."){
		indexGenotypeQual =i; 
		formatFieldGQ=0.0;
	    }else{
		indexGenotypeQual =i; 
		formatFieldGQ=destringify<float>(formatFieldValues[i]);
	    } 
	    continue; }
	if(formatFieldNames[i] == "DP"){ indexDepth        =i; formatFieldDP=destringify<int>  (formatFieldValues[i]); continue;}

	if(formatFieldNames[i] == "GL"){ 
	    observedGL=true;
	    if(observedPL){
		cerr<<"SimpleVCF: cannot observed both GL and PL "<<vectorToString(fields,"\t")<<""<<endl;
		exit(1);
	    }

	    indexPL        = i; 
	    formatFieldGL  = formatFieldValues[i];
	    vector<string> glfields = allTokens(formatFieldGL,',');

	    if(glfields.size() == 2){ //haploid calls (e.g. X for a male)
		if(!haploidCall){
		    cerr<<"SimpleVCF: cannot observed 2 GL fields for a non-haploid record "<<vectorToString(fields,"\t")<<""<<endl;
		    exit(1);
		}
		formatFieldPLHomoRef =  int(-10.0*destringify<double>(glfields[0]));
		formatFieldPLHetero  =  -1000000; //very unlikely
		formatFieldPLHomoAlt =  int(-10.0*destringify<double>(glfields[1]));
		    
	    }else{
		if(glfields.size() == 3){ //biallelic

		    formatFieldPLHomoRef =  int(-10.0*destringify<double>(glfields[0]));
		    formatFieldPLHetero  =  int(-10.0*destringify<double>(glfields[1]));
		    formatFieldPLHomoAlt =  int(-10.0*destringify<double>(glfields[2]));

		}else{
		    if(glfields.size() == 6){ //triallelic
			//according to VCF docs it has the following order AA,AB,BB,AC,BC,CC
			formatFieldPLHomoRef  =  int(-10.0*destringify<double>(glfields[0])); //r-r

			formatFieldPLHetero1  =  int(-10.0*destringify<double>(glfields[1])); //r-a1
			formatFieldPLHomoAlt1 =  int(-10.0*destringify<double>(glfields[2])); //a1-a1

			formatFieldPLHetero2  =  int(-10.0*destringify<double>(glfields[3])); //r-a2

			formatFieldPLHetero12 =  int(-10.0*destringify<double>(glfields[4])); //a1-a2
			formatFieldPLHomoAlt2 =  int(-10.0*destringify<double>(glfields[5])); //a2-a2

		    }else{
			cerr<<"SimpleVCF: for line "<<vectorToString(fields,"\t")<<" the GL field does not have 3 or 6 fields"<<endl;
			exit(1);
		    }
		}
	    }
	}


	if(formatFieldNames[i] == "PL"){ 
	    observedPL=true;

	    if(observedGL){
		cerr<<"SimpleVCF: cannot observed both GL and PL "<<vectorToString(fields,"\t")<<""<<endl;
		exit(1);
	    }

	    indexPL        = i; 

	    if(formatFieldValues[i] == "."){
		formatFieldPL = formatFieldValues[i];
		unresolvedGT=true; 
		continue;
	    }

	    formatFieldPL  = formatFieldValues[i];
	    vector<string> plfields = allTokens(formatFieldPL,',');

	    if(plfields.size() == 3){ //biallelic
		formatFieldPLHomoRef =  destringify<int>(plfields[0]);
		formatFieldPLHetero  =  destringify<int>(plfields[1]);
		formatFieldPLHomoAlt =  destringify<int>(plfields[2]);

	    }else{
		if(plfields.size() == 6){ //triallelic
		    //according to VCF docs it has the following order AA,AB,BB,AC,BC,CC
		    formatFieldPLHomoRef  =  destringify<int>(plfields[0]); //r-r

		    formatFieldPLHetero1  =  destringify<int>(plfields[1]); //r-a1
		    formatFieldPLHomoAlt1 =  destringify<int>(plfields[2]); //a1-a1

		    formatFieldPLHetero2  =  destringify<int>(plfields[3]); //r-a2

		    formatFieldPLHetero12 =  destringify<int>(plfields[4]); //a1-a2
		    formatFieldPLHomoAlt2 =  destringify<int>(plfields[5]); //a2-a2

		}else{
		    cerr<<"SimpleVCF: for line "<<vectorToString(fields,"\t")<<" the PL field does not have 3 or 6 fields"<<endl;
		    exit(1);
		}
	    }
	    continue;
	}

	//To uncomment the fields to get these fields
	if(formatFieldNames[i] == "A"){   
	    vector<string> adfield = allTokens( formatFieldValues[i] ,',');
	    for(unsigned int j=0;j<adfield.size();j++){
		countA.push_back(   destringify<int>( adfield[j]) );
	    }
	    continue;
	}

	if(formatFieldNames[i] == "C"){   
	    vector<string> adfield = allTokens( formatFieldValues[i] ,',');
	    for(unsigned int j=0;j<adfield.size();j++){
		countC.push_back(   destringify<int>( adfield[j]) );
	    }
	    continue;
	}

	if(formatFieldNames[i] == "G"){   
	    vector<string> adfield = allTokens( formatFieldValues[i] ,',');
	    for(unsigned int j=0;j<adfield.size();j++){
		countG.push_back(   destringify<int>( adfield[j]) );
	    }
	    continue;
	}

	if(formatFieldNames[i] == "T"){   
	    vector<string> adfield = allTokens( formatFieldValues[i] ,',');
	    for(unsigned int j=0;j<adfield.size();j++){
		countT.push_back(   destringify<int>( adfield[j]) );
	    }
	    continue;
	}
	    


    }
    }
    // cout<<getADforA()<<endl;
    // cout<<getADforC()<<endl;
    // cout<<getADforG()<<endl;
    // cout<<getADforT()<<endl;

    // cerr<<"end"<<endl;

}
Beispiel #29
0
void slave(int rank, int comm_sz) {
  Message msg;
  int local_v[MAX_VECTOR];
  int local_l;
  MPI_Status status;
  
  int q; // disposable index
  char * strVector; // pointer for string representation of vector
  char label[3]; // storage for local event label 
  
  // initialize vector
  for (q = 0; q < comm_sz; q++) 
    local_v[q] = 0;
  // initialize lamport
  local_l = 0;
  // initialize label
  label[0] = '0' + rank - 1;
  label[1] = '@'; // == 'A' - 1
  label[2] = '\0';

   while (1) {
    MPI_Recv(&msg, 1, MESSAGE, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
    sleep(1);
    // On finalize, MPI_TAG == 1
    if (status.MPI_TAG == 1) {
      strVector = vectorToString(local_v, comm_sz);
      sprintf(msg.string, "Process %d report: Event %s - Logical: %d - Vector: %s", 
	      rank - 1, label, local_l, strVector);
      free(strVector);
      MPI_Send(&msg, 1, MESSAGE, 0, 0, MPI_COMM_WORLD);
      break;
    }

    local_l++;
    local_v[rank]++;
    label[1]++;

    if (status.MPI_SOURCE == 0) {
      if (msg.dest == 0) {
	printf("Executing event %s in process %d.\n", label, rank - 1);
      }
      else {
	printf("Message sent event %s from process %d to process %d: %s\n", 
	       label, rank - 1, msg.dest - 1, msg.string);
	for (q = 0; q < comm_sz; q++) {
	  msg.vector[q] = local_v[q];
	}
	msg.lamport = local_l;
	MPI_Send(&msg, 1, MESSAGE, msg.dest, 0, MPI_COMM_WORLD); 
      }
      sprintf(msg.string, "receievd");
      MPI_Send(&msg, 1, MESSAGE, 0, 0, MPI_COMM_WORLD);     
    } else {
      printf("Message received event %s from process %d by process %d: %s\n", 
	     label, status.MPI_SOURCE - 1, rank - 1, msg.string);
      mergeVectors(msg.vector, local_v, comm_sz);
      local_l = (msg.lamport >= (local_l - 1)? msg.lamport + 1 : local_l);
    }

    // print status to stdout
    strVector = vectorToString(local_v, comm_sz);
    printf("The Logical/Vector time of event %s at process %d is: %d / %s\n\n", 
	   label, rank - 1, local_l, strVector);
    free(strVector);
  }
}
Beispiel #30
0
/*!
   \param input the input combination ("1" = true, "0" = false).
   \return The outputs of the input combination in string form ("1" = true, "0" = false).
*/
wxString TruthTable::getOutput(wxString input)
{
    vector<bool> vInput = stringToVector(input);
    return vectorToString(getOutput(vInput));
}