コード例 #1
0
    /// Solve pressure equation, by Newton iterations.
    void CompressibleTpfa::solve(const double dt,
                                 BlackoilState& state,
                                 WellState& well_state)
    {
        const int nc = grid_.number_of_cells;
        const int nw = (wells_ != 0) ? wells_->number_of_wells : 0;

        // Set up dynamic data.
        computePerSolveDynamicData(dt, state, well_state);
        computePerIterationDynamicData(dt, state, well_state);

        // Assemble J and F.
        assemble(dt, state, well_state);

        double inc_norm = 0.0;
        int iter = 0;
        double res_norm = residualNorm();
        std::cout << "\nIteration         Residual        Change in p\n"
                  << std::setw(9) << iter
                  << std::setw(18) << res_norm
                  << std::setw(18) << '*' << std::endl;
        while ((iter < maxiter_) && (res_norm > residual_tol_)) {
            // Solve for increment in Newton method:
            //   incr = x_{n+1} - x_{n} = -J^{-1}F
            // (J is Jacobian matrix, F is residual)
            solveIncrement();
            ++iter;

            // Update pressure vars with increment.
            for (int c = 0; c < nc; ++c) {
                state.pressure()[c] += pressure_increment_[c];
            }
            for (int w = 0; w < nw; ++w) {
                well_state.bhp()[w] += pressure_increment_[nc + w];
            }

            // Stop iterating if increment is small.
            inc_norm = incrementNorm();
            if (inc_norm <= change_tol_) {
                std::cout << std::setw(9) << iter
                          << std::setw(18) << '*'
                          << std::setw(18) << inc_norm << std::endl;
                break;
            }

            // Set up dynamic data.
            computePerIterationDynamicData(dt, state, well_state);

            // Assemble J and F.
            assemble(dt, state, well_state);

            // Update residual norm.
            res_norm = residualNorm();

            std::cout << std::setw(9) << iter
                      << std::setw(18) << res_norm
                      << std::setw(18) << inc_norm << std::endl;
        }

        if ((iter == maxiter_) && (res_norm > residual_tol_) && (inc_norm > change_tol_)) {
            OPM_THROW(std::runtime_error, "CompressibleTpfa::solve() failed to converge in " << maxiter_ << " iterations.");
        }

        std::cout << "Solved pressure in " << iter << " iterations." << std::endl;

        // Compute fluxes and face pressures.
        computeResults(state, well_state);
    }
コード例 #2
0
ファイル: MDDAGClassifier.cpp プロジェクト: busarobi/MDDAG2
void MDDAGClassifier::run(const string& dataFileName, const string& shypFileName,
                          int numIterations, const string& outResFileName, int numRanksEnclosed)
{
    InputData* pData = loadInputData(dataFileName, shypFileName);

    if (_verbose > 0)
        cout << "Loading strong hypothesis..." << flush;

    // The class that loads the weak hypotheses
    UnSerialization us;

    // Where to put the weak hypotheses
    vector<BaseLearner*> weakHypotheses;

    // loads them
    us.loadHypotheses(shypFileName, weakHypotheses, pData);

    // where the results go
    vector< ExampleResults* > results;

    if (_verbose > 0)
        cout << "Classifying..." << flush;

    // get the results
    computeResults( pData, weakHypotheses, results, numIterations );

    const int numClasses = pData->getNumClasses();

    if (_verbose > 0)
    {
        // well.. if verbose = 0 no results are displayed! :)
        cout << "Done!" << endl;

        vector< vector<float> > rankedError(numRanksEnclosed);

        // Get the per-class error for the numRanksEnclosed-th ranks
        for (int i = 0; i < numRanksEnclosed; ++i)
            getClassError( pData, results, rankedError[i], i );

        // output it
        cout << endl;
        cout << "Error Summary" << endl;
        cout << "=============" << endl;

        for ( int l = 0; l < numClasses; ++l )
        {
            // first rank (winner): rankedError[0]
            cout << "Class '" << pData->getClassMap().getNameFromIdx(l) << "': "
                 << setprecision(4) << rankedError[0][l] * 100 << "%";

            // output the others on its side
            if (numRanksEnclosed > 1 && _verbose > 1)
            {
                cout << " (";
                for (int i = 1; i < numRanksEnclosed; ++i)
                    cout << " " << i+1 << ":[" << setprecision(4) << rankedError[i][l] * 100 << "%]";
                cout << " )";
            }

            cout << endl;
        }

        // the overall error
        cout << "\n--> Overall Error: "
             << setprecision(4) << getOverallError(pData, results, 0) * 100 << "%";

        // output the others on its side
        if (numRanksEnclosed > 1 && _verbose > 1)
        {
            cout << " (";
            for (int i = 1; i < numRanksEnclosed; ++i)
                cout << " " << i+1 << ":[" << setprecision(4) << getOverallError(pData, results, i) * 100 << "%]";
            cout << " )";
        }

        cout << endl;

    } // verbose


    // If asked output the results
    if ( !outResFileName.empty() )
    {
        const int numExamples = pData->getNumExamples();
        ofstream outRes(outResFileName.c_str());

        outRes << "Instance" << '\t' << "Forecast" << '\t' << "Labels" << '\n';

        string exampleName;

        for (int i = 0; i < numExamples; ++i)
        {
            // output the name if it exists, otherwise the number
            // of the example
            exampleName = pData->getExampleName(i);
            if ( exampleName.empty() )
                outRes << i << '\t';
            else
                outRes << exampleName << '\t';

            // output the predicted class
            outRes << pData->getClassMap().getNameFromIdx( results[i]->getWinner().first ) << '\t';

            outRes << '|';

            vector<Label>& labels = pData->getLabels(i);
            for (vector<Label>::iterator lIt=labels.begin(); lIt != labels.end(); ++lIt) {
                if (lIt->y>0)
                {
                    outRes << ' ' << pData->getClassMap().getNameFromIdx(lIt->idx);
                }
            }

            outRes << endl;
        }

        if (_verbose > 0)
            cout << "\nPredictions written on file <" << outResFileName << ">!" << endl;

    }


    // delete the input data file
    if (pData)
        delete pData;

    vector<ExampleResults*>::iterator it;
    for (it = results.begin(); it != results.end(); ++it)
        delete (*it);
}
コード例 #3
0
ファイル: MDDAGClassifier.cpp プロジェクト: busarobi/MDDAG2
void MDDAGClassifier::saveCalibratedPosteriors(const string& dataFileName, const string& shypFileName,
        const string& outFileName, int numIterations)
{
    InputData* pData = loadInputData(dataFileName, shypFileName);

    if (_verbose > 0)
        cout << "Loading strong hypothesis..." << flush;

    // The class that loads the weak hypotheses
    UnSerialization us;

    // Where to put the weak hypotheses
    vector<BaseLearner*> weakHypotheses;

    // loads them
    us.loadHypotheses(shypFileName, weakHypotheses, pData);

    // where the results go
    vector< ExampleResults* > results;

    if (_verbose > 0)
        cout << "Classifying..." << flush;

    // get the results
    computeResults( pData, weakHypotheses, results, numIterations );

    const int numClasses = pData->getNumClasses();
    const int numExamples = pData->getNumExamples();

    ofstream outFile(outFileName.c_str());
    string exampleName;

    if (_verbose > 0)
        cout << "Output posteriors..." << flush;

    for (int i = 0; i < numExamples; ++i)
    {
        // output the name if it exists, otherwise the number
        // of the example
        exampleName = pData->getExampleName(i);
        if ( !exampleName.empty() )
            outFile << exampleName << ',';

        // output the posteriors
        outFile << results[i]->getVotesVector()[0];
        for (int l = 1; l < numClasses; ++l)
            outFile << ',' << results[i]->getVotesVector()[l];
        outFile << '\n';
    }

    if (_verbose > 0)
        cout << "Done!" << endl;

    if (_verbose > 1)
    {
        cout << "\nClass order (You can change it in the header of the data file):" << endl;
        for (int l = 0; l < numClasses; ++l)
            cout << "- " << pData->getClassMap().getNameFromIdx(l) << endl;
    }

    // delete the input data file
    if (pData)
        delete pData;

    vector<ExampleResults*>::iterator it;
    for (it = results.begin(); it != results.end(); ++it)
        delete (*it);
}
コード例 #4
0
ファイル: MDDAGClassifier.cpp プロジェクト: busarobi/MDDAG2
void MDDAGClassifier::saveConfusionMatrix(const string& dataFileName, const string& shypFileName,
        const string& outFileName)
{
    InputData* pData = loadInputData(dataFileName, shypFileName);

    if (_verbose > 0)
        cout << "Loading strong hypothesis..." << flush;

    // The class that loads the weak hypotheses
    UnSerialization us;

    // Where to put the weak hypotheses
    vector<BaseLearner*> weakHypotheses;

    // loads them
    us.loadHypotheses(shypFileName, weakHypotheses, pData);

    // where the results go
    vector< ExampleResults* > results;

    if (_verbose > 0)
        cout << "Classifying..." << flush;

    // get the results
    computeResults( pData, weakHypotheses, results, (int)weakHypotheses.size() );

    const int numClasses = pData->getNumClasses();
    const int numExamples = pData->getNumExamples();

    ofstream outFile(outFileName.c_str());

    //////////////////////////////////////////////////////////////////////////

    for (int l = 0; l < numClasses; ++l)
        outFile << '\t' << pData->getClassMap().getNameFromIdx(l);
    outFile << endl;

    for (int l = 0; l < numClasses; ++l)
    {
        vector<int> winnerCount(numClasses, 0);
        for (int i = 0; i < numExamples; ++i)
        {
            if ( pData->hasPositiveLabel(i,l) )
                ++winnerCount[ results[i]->getWinner().first ];
        }

        // class name
        outFile << pData->getClassMap().getNameFromIdx(l);

        for (int j = 0; j < numClasses; ++j)
            outFile << '\t' << winnerCount[j];

        outFile << endl;
    }

    //////////////////////////////////////////////////////////////////////////

    if (_verbose > 0)
        cout << "Done!" << endl;

    // delete the input data file
    if (pData)
        delete pData;

    vector<ExampleResults*>::iterator it;
    for (it = results.begin(); it != results.end(); ++it)
        delete (*it);
}
コード例 #5
0
ファイル: MDDAGClassifier.cpp プロジェクト: busarobi/MDDAG2
void MDDAGClassifier::printConfusionMatrix(const string& dataFileName, const string& shypFileName)
{
    InputData* pData = loadInputData(dataFileName, shypFileName);

    if (_verbose > 0)
        cout << "Loading strong hypothesis..." << flush;

    // The class that loads the weak hypotheses
    UnSerialization us;

    // Where to put the weak hypotheses
    vector<BaseLearner*> weakHypotheses;

    // loads them
    us.loadHypotheses(shypFileName, weakHypotheses, pData);

    // where the results go
    vector< ExampleResults* > results;

    if (_verbose > 0)
        cout << "Classifying..." << flush;

    // get the results
    computeResults( pData, weakHypotheses, results, (int)weakHypotheses.size());

    const int numClasses = pData->getNumClasses();
    const int numExamples = pData->getNumExamples();

    if (_verbose > 0)
        cout << "Done!" << endl;

    const int colSize = 7;

    if (_verbose > 0)
    {
        cout << "Raw Confusion Matrix:\n";
        cout << setw(colSize) << "Truth       ";

        for (int l = 0; l < numClasses; ++l)
            cout << setw(colSize) << nor_utils::getAlphanumeric(l);

        cout << "\nClassification\n";

        for (int l = 0; l < numClasses; ++l)
        {
            vector<int> winnerCount(numClasses, 0);
            for (int i = 0; i < numExamples; ++i)
            {
                if ( pData->hasPositiveLabel(i, l) )
                    ++winnerCount[ results[i]->getWinner().first ];
            }

            // class
            cout << setw(colSize) << "           " << nor_utils::getAlphanumeric(l);

            for (int j = 0; j < numClasses; ++j)
                cout << setw(colSize) << winnerCount[j];

            cout << endl;
        }

    }

    cout << "\nMatrix Key:\n";

    // Print the legend
    for (int l = 0; l < numClasses; ++l)
        cout << setw(5) << nor_utils::getAlphanumeric(l) << ": " <<
             pData->getClassMap().getNameFromIdx(l) << "\n";

    // delete the input data file
    if (pData)
        delete pData;

    vector<ExampleResults*>::iterator it;
    for (it = results.begin(); it != results.end(); ++it)
        delete (*it);
}
コード例 #6
0
ファイル: AdaBoostMHClassifier.cpp プロジェクト: ShenWei/src
	void AdaBoostMHClassifier::saveROC(const string& dataFileName, const string& shypFileName, 
		const string& outFileName, int numIterations)
	{
		InputData* pData = loadInputData(dataFileName, shypFileName);
		ofstream outFile(outFileName.c_str());
		
		if ( ! outFile.is_open() )
		{
			cout << "Cannot open outfile" << endl;
			exit( -1 );
		}

		if (_verbose > 0)
			cout << "Loading strong hypothesis..." << flush;

		// The class that loads the weak hypotheses
		UnSerialization us;

		// Where to put the weak hypotheses
		vector<BaseLearner*> weakHypotheses;

		// loads them
		us.loadHypotheses(shypFileName, weakHypotheses, pData);
		weakHypotheses.resize( numIterations );

		// where the results go
		vector< ExampleResults* > results;

		if (_verbose > 0)
			cout << "Classifying..." << flush;

		// get the results
		computeResults( pData, weakHypotheses, results, weakHypotheses.size());

		const int numClasses = pData->getNumClasses();
		const int numExamples = pData->getNumExamples();

		if (_verbose > 0)
			cout << "Done!" << endl;		

		vector< pair< int, double> > sortedExample( numExamples );
		
		for( int i=0; i<numExamples; i++ )
		{
			sortedExample[i].first = i;
			sortedExample[i].second = results[i]->getVotesVector()[0];
		}
		sort( sortedExample.begin(), sortedExample.end(), nor_utils::comparePair< 2, int, double, greater<double> >() );

		vector<double> positiveWeights( numExamples );
		double sumOfPositiveWeights = 0.0;

		vector<double>  negativeWeights( numExamples );
		double sumOfNegativeWeights = 0.0;
		
		fill( positiveWeights.begin(), positiveWeights.end(), 0.0 );
		fill( negativeWeights.begin(), negativeWeights.end(), 0.0 );

		string className = pData->getClassMap().getNameFromIdx( 0 );

		vector<Label>& labels = pData->getLabels( sortedExample[0].first );
		vector<Label>::iterator labIt = find( labels.begin(), labels.end(), 0);
		
		if ( labIt != labels.end() )
		{
			if ( labIt->y > 0.0 )
			{
				positiveWeights[0] = labIt->initialWeight;
				sumOfPositiveWeights += labIt->initialWeight;
			} else
			{
				negativeWeights[0] = labIt->initialWeight;
				sumOfNegativeWeights += labIt->initialWeight;
			}
		}
		
		for( int i=1; i<numExamples; i++ )
		{
			labels = pData->getLabels( sortedExample[i].first );
			labIt = find( labels.begin(), labels.end(), 0);
			if ( labIt != labels.end() )
			{
				if ( labIt->y > 0.0 )
				{
					negativeWeights[i] = negativeWeights[i-1];
					positiveWeights[i] = positiveWeights[i-1] + labIt->initialWeight;
					sumOfPositiveWeights += labIt->initialWeight;
				} else
				{
					positiveWeights[i] = positiveWeights[i-1];
					negativeWeights[i] = negativeWeights[i-1] + labIt->initialWeight;
					sumOfNegativeWeights += labIt->initialWeight;
				}
			} else {
				positiveWeights[i] = positiveWeights[i-1];
				negativeWeights[i] = negativeWeights[i-1];
			}
		}

		outFile << "Class name: " << className << endl;
		for( int i=0; i<numExamples; i++ )
		{
			outFile <<  sortedExample[i].first << " ";
			// false positive rate
			outFile << ( positiveWeights[i] / sumOfPositiveWeights ) << " ";
			//true negative rate
			outFile << ( negativeWeights[i] / sumOfNegativeWeights ) << endl;
		}		

		outFile.close();

		// delete the input data file
		if (pData) 
			delete pData;

		vector<ExampleResults*>::iterator it;
		for (it = results.begin(); it != results.end(); ++it)
			delete (*it);
	}