TEST(TestTerminationCriterion, TestAccuracy) {
    TestAccuracyTerminationCriterion<Dtype> criterion(3);
    EXPECT_FALSE(criterion.IsCriterionMet());
    
    criterion.NotifyValidationAccuracy(0.5);
    EXPECT_FALSE(criterion.IsCriterionMet());

    //first countdown
    criterion.NotifyValidationAccuracy(0.5);
    EXPECT_FALSE(criterion.IsCriterionMet());
    
    //second countdown
    criterion.NotifyValidationAccuracy(0.5);
    EXPECT_FALSE(criterion.IsCriterionMet());
    
    //reset
    criterion.NotifyValidationAccuracy(0.6);
    EXPECT_FALSE(criterion.IsCriterionMet());
    
    //first countdown
    criterion.NotifyValidationAccuracy(0.5);
    EXPECT_FALSE(criterion.IsCriterionMet());
    
    //second countdown
    criterion.NotifyValidationAccuracy(0.5);
    EXPECT_FALSE(criterion.IsCriterionMet());
    
    //third countdown
    criterion.NotifyValidationAccuracy(0.5);
    
    EXPECT_TRUE(criterion.IsCriterionMet());
  }
  TEST(TestTerminationCriterion, ExternalRunInBackgroundTerminationCriterion) {
    int run_every = 10;
    int ret;
    ExternalRunInBackgroundTerminationCriterion<Dtype> criterion("touch test", run_every);

    EXPECT_FALSE(criterion.IsCriterionMet());

    criterion.NotifyValidationAccuracy(0.5);
    EXPECT_TRUE(std::ifstream("learning_curve.txt"));

    criterion.NotifyIteration(run_every+1);
    EXPECT_TRUE(std::ifstream("termination_criterion_running"));
    criterion.NotifyIteration(run_every+2);
    EXPECT_FALSE(criterion.IsCriterionMet());

    ret = system("rm termination_criterion_running");

    criterion.NotifyIteration(run_every+3);
    EXPECT_FALSE(criterion.IsCriterionMet());

    criterion.NotifyIteration(2*run_every+1);

    ret = system("rm termination_criterion_running");
    ret = system("touch y_predict.txt");

    criterion.NotifyIteration(2*run_every+2);

    EXPECT_TRUE(criterion.IsCriterionMet());

    //make sure the touch was run:
    EXPECT_TRUE(std::ifstream("test"));
    ret = system("rm test");
  }
Ejemplo n.º 3
0
inline void
createAMGPreconditionerPointer( Op& opA, const double relax, const P& comm, std::unique_ptr< AMG >& amgPtr )
{
    // type of matrix
    typedef typename Op::matrix_type  M;

    // The coupling metric used in the AMG
    typedef Dune::Amg::FirstDiagonal CouplingMetric;

    // The coupling criterion used in the AMG
    typedef Dune::Amg::SymmetricCriterion<M, CouplingMetric> CritBase;

    // The coarsening criterion used in the AMG
    typedef Dune::Amg::CoarsenCriterion<CritBase> Criterion;

    // TODO: revise choice of parameters
    int coarsenTarget=1200;
    Criterion criterion(15,coarsenTarget);
    criterion.setDebugLevel( 0 ); // no debug information, 1 for printing hierarchy information
    criterion.setDefaultValuesIsotropic(2);
    criterion.setNoPostSmoothSteps( 1 );
    criterion.setNoPreSmoothSteps( 1 );

    // for DUNE 2.2 we also need to pass the smoother args
    typedef typename AMG::Smoother Smoother;
    typedef typename Dune::Amg::SmootherTraits<Smoother>::Arguments  SmootherArgs;
    SmootherArgs  smootherArgs;
    smootherArgs.iterations = 1;
    smootherArgs.relaxationFactor = relax;

    amgPtr.reset( new AMG(opA, criterion, smootherArgs, comm ) );
}
void MatchScoringMapPreparer::prepMap(OsmMapPtr map, const bool removeNodes)
{
  // if an element has a uuid, but no REF1/REF2 tag then create a REF tag with the uuid. The
  // 1/2 is determined by the unknown status.
  ConvertUuidToRefVisitor convertUuidToRef;
  map->visitRw(convertUuidToRef);

  // #5891 if the feature is marked as todo then there is no need to conflate & evaluate it.
  shared_ptr<TagCriterion> isTodo(new TagCriterion("REF2", "todo"));
  RemoveElementsVisitor remover(isTodo);
  remover.setRecursive(true);
  map->visitRw(remover);

  // add a uuid to all elements with a REF tag.
  HasTagCriterion criterion("REF1", "REF2", "REVIEW");
  AddUuidVisitor uuid("uuid");
  FilteredVisitor v(criterion, uuid);
  map->visitRw(v);

  if (removeNodes)
  {
    // remove all REF1/REF2 tags from the nodes.
    RemoveTagVisitor removeRef("REF1", "REF2");
    IsNodeFilter nodeFilter(Filter::KeepMatches);
    FilteredVisitor removeRefV(nodeFilter, removeRef);
    map->visitRw(removeRefV);
  }

  //MapCleaner().apply(map);
}
Ejemplo n.º 5
0
Vector<double> OtsuThresholding<T>::computeCriterion(const Vector<unsigned int>& histogram) const
{
  const int numBins = histogram.getSize();
  Vector<double> criterion( numBins );
  int n0 = 0, n1 = 0; // class sizes
  int s0 = 0, s1 = 0; // sum of values
  double m0, m1; // mean values
  double p0, p1; // class proportions
  int g;

  for (g = 0; g < numBins; ++g)
  {
    criterion[g] = 0;
    n1 += histogram[g];
    s1 += g * histogram[g];
  }

  for (g = 1; g < numBins; ++g)
  {
    n0 += histogram[g-1];
    n1 -= histogram[g-1];
    s0 += (g-1) * histogram[g-1];
    s1 -= (g-1) * histogram[g-1];
    if ( n0 > 0 && n1 > 0 )
    {
      m0 = static_cast<double>(s0) / n0;
      m1 = static_cast<double>(s1) / n1;
      p0 = static_cast<double>(n0) / (n0+n1);
      p1 = static_cast<double>(n1) / (n0+n1);
      criterion[g] = p0 * p1 * Maths::sqr( m0-m1 );
    }
  }

  return criterion;
}
  TEST(TestTerminationCriterion, ExternalRunInBackgroundTerminationCriterionIsRun) {
    int run_every = 10;
    int ret;
    ExternalRunInBackgroundTerminationCriterion<Dtype> criterion("touch test", run_every);

    //check that the command is actually run in the background.

    criterion.NotifyIteration(run_every+1);
    sleep(1);
    EXPECT_TRUE(std::ifstream("test"));
    ret = system("rm test");
  }
 TEST(TestTerminationCriterion, MaxIter) {
   MaxIterTerminationCriterion<Dtype> criterion(3);
   EXPECT_FALSE(criterion.IsCriterionMet());
   
   criterion.NotifyIteration(1);
   EXPECT_FALSE(criterion.IsCriterionMet());
   
   criterion.NotifyIteration(2);
   EXPECT_FALSE(criterion.IsCriterionMet());
   
   criterion.NotifyIteration(3);
   EXPECT_TRUE(criterion.IsCriterionMet());
 }
  TEST(TestTerminationCriterion, ExternalRunInBackgroundTerminationCriterionIsRunInBackground) {
    int run_every = 10;
    double epsilon_time = 1.;
    int ret;
    ExternalRunInBackgroundTerminationCriterion<Dtype> criterion("sleep 5", run_every);

    //check that the command is actually run in the background.

    clock_t begin = clock();
    criterion.NotifyIteration(run_every+1);
    clock_t end = clock();
    double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
    EXPECT_TRUE(elapsed_secs < epsilon_time);
    LOG(INFO) << elapsed_secs;

    system("kill `pidof sleep`");
  }
  TEST(TestTerminationCriterion, ExternalRunInBackgroundTerminationCriterionIsKilled) {
    int run_every = 10;
    int ret;

    //make sure
    ret = system("pidof sleep");
    EXPECT_NE(ret, 0) << " " <<"WARNING: make sure there's no sleep process running at the moment!";

    {
        ExternalRunInBackgroundTerminationCriterion<Dtype> criterion("sleep 10000", run_every);
        criterion.NotifyIteration(run_every+1);
        sleep(1);
        //this is a little hacky, because it assumes only a single sleep instance is running at the moment
        system("pidof sleep > termination_criterion_running_pid");
        ret = system("pidof sleep");
        EXPECT_EQ(ret, 0);
    }
    ret = system("pidof sleep");

    //getting the pid of sleep should be false now, because it should have been killed
    EXPECT_NE(ret, 0);
  }
Ejemplo n.º 10
0
        void prepareSolver(Operator& wellOpA, Comm& comm)
        {

            Vector& istlb = *(this->rhs_);
            comm.copyOwnerToAll(istlb, istlb);

            const double relax = this->parameters_.ilu_relaxation_;
            const MILU_VARIANT ilu_milu  = this->parameters_.ilu_milu_;

            // TODO: revise choice of parameters
            // int coarsenTarget = 4000;
            int coarsenTarget = 1200;
            Criterion criterion(15, coarsenTarget);
            criterion.setDebugLevel( this->parameters_.cpr_solver_verbose_ ); // no debug information, 1 for printing hierarchy information
            criterion.setDefaultValuesIsotropic(2);
            criterion.setNoPostSmoothSteps( 1 );
            criterion.setNoPreSmoothSteps( 1 );
            //new guesses by hmbn
            //criterion.setAlpha(0.01); // criterion for connection strong 1/3 is default
            //criterion.setMaxLevel(2); //
            //criterion.setGamma(1); //  //1 V cycle 2 WW

            // Since DUNE 2.2 we also need to pass the smoother args instead of steps directly
            using AmgType           = typename std::conditional<std::is_same<Comm, Dune::Amg::SequentialInformation>::value,
                                                                BlackoilAmgType, ParallelBlackoilAmgType>::type;
            using SpType            = typename std::conditional<std::is_same<Comm, Dune::Amg::SequentialInformation>::value,
                                                                Dune::SeqScalarProduct<Vector>,
                                                                ParallelScalarProduct >::type;
            using OperatorType      = typename std::conditional<std::is_same<Comm, Dune::Amg::SequentialInformation>::value,
                                                                MatrixAdapter, ParallelMatrixAdapter>::type;
            typedef typename AmgType::Smoother Smoother;
            typedef typename Dune::Amg::SmootherTraits<Smoother>::Arguments  SmootherArgs;
            SmootherArgs  smootherArgs;
            smootherArgs.iterations = 1;
            smootherArgs.relaxationFactor = relax;
            const Opm::CPRParameter& params(this->parameters_); // strange conversion
            ISTLUtility::setILUParameters(smootherArgs, ilu_milu);

            auto& opARef = reinterpret_cast<OperatorType&>(*opA_);
            int newton_iteration = this->simulator_.model().newtonMethod().numIterations();
            bool update_preconditioner = false;

            if (this->parameters_.cpr_reuse_setup_ < 1) {
                update_preconditioner = true;
            }
            if (this->parameters_.cpr_reuse_setup_ < 2) {
                if (newton_iteration < 1) {
                    update_preconditioner = true;
                }
            }
            if (this->parameters_.cpr_reuse_setup_ < 3) {
                if (this->iterations() > 10) {
                    update_preconditioner = true;
                }
            }

            if ( update_preconditioner or (amg_== 0) ) {
                amg_.reset( new AmgType( params, this->weights_, opARef, criterion, smootherArgs, comm ) );
            } else {
                if (this->parameters_.cpr_solver_verbose_) {
                    std::cout << " Only update amg solver " << std::endl;
                }
                reinterpret_cast<AmgType*>(amg_.get())->updatePreconditioner(opARef, smootherArgs, comm);
            }
            // Solve.
            //SuperClass::solve(linearOperator, x, istlb, *sp, *amg, result);
            //references seems to do something els than refering

            int verbosity_linsolve = 0;
            if (comm.communicator().rank() == 0) {
                verbosity_linsolve = this->parameters_.linear_solver_verbosity_;
            }

            linsolve_.reset(new Dune::BiCGSTABSolver<Vector>(wellOpA, reinterpret_cast<SpType&>(*sp_), reinterpret_cast<AmgType&>(*amg_),
                                                             this->parameters_.linear_solver_reduction_,
                                                             this->parameters_.linear_solver_maxiter_,
                                                             verbosity_linsolve));
        }