Skip to content

Commit

Permalink
Add constructors to multiplicative_diploid and additive_diploid
Browse files Browse the repository at this point in the history
allowing for custom mapping functions.
  • Loading branch information
molpopgen committed Mar 20, 2020
1 parent 1f9b9af commit 254a907
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
53 changes: 53 additions & 0 deletions fwdpp/fitness_models.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,20 @@ namespace fwdpp
}
}

template<typename make_return_value_fxn>
multiplicative_diploid(fitness f, make_return_value_fxn&& make_rv)
: scaling{ f.get() }, gvalue_is_trait{ assign_is_trait_value(f) },
gvalue_is_fitness{ assign_is_fitness_value(f) },
make_return_value{ std::forward<make_return_value_fxn>(make_rv) }
{
if (!std::isfinite(scaling))
{
throw std::invalid_argument(
"scaling parameter must be finite");
}
}


multiplicative_diploid(fitness gvtype)
: scaling{ gvtype.get() },
gvalue_is_trait(assign_is_trait_value(gvtype)),
Expand All @@ -520,6 +534,19 @@ namespace fwdpp
}
}

template<typename make_return_value_fxn>
multiplicative_diploid(trait t, make_return_value_fxn&& make_rv)
: scaling{ t.get() }, gvalue_is_trait{ assign_is_trait_value(t) },
gvalue_is_fitness{ assign_is_fitness_value(t) },
make_return_value{ std::forward<make_return_value_fxn>(make_rv) }
{
if (!std::isfinite(scaling))
{
throw std::invalid_argument(
"scaling parameter must be finite");
}
}

template <typename iterator_t, typename mcont_t>
inline result_type
operator()(iterator_t first1, iterator_t last1, iterator_t first2,
Expand Down Expand Up @@ -618,6 +645,19 @@ namespace fwdpp
}
}

template<typename make_return_value_fxn>
additive_diploid(fitness f, make_return_value_fxn&& make_rv)
: scaling{ f.get() }, gvalue_is_trait{ assign_is_trait_value(f) },
gvalue_is_fitness{ assign_is_fitness_value(f) },
make_return_value{ std::forward<make_return_value_fxn>(make_rv) }
{
if (!std::isfinite(scaling))
{
throw std::invalid_argument(
"scaling parameter must be finite");
}
}

additive_diploid(trait t)
: scaling{ t.get() }, gvalue_is_trait{ assign_is_trait_value(t) },
gvalue_is_fitness{ assign_is_fitness_value(t) },
Expand All @@ -632,6 +672,19 @@ namespace fwdpp
}
}

template<typename make_return_value_fxn>
additive_diploid(trait t, make_return_value_fxn&& make_rv)
: scaling{ t.get() }, gvalue_is_trait{ assign_is_trait_value(t) },
gvalue_is_fitness{ assign_is_fitness_value(t) },
make_return_value{ std::forward<make_return_value_fxn>(make_rv) }
{
if (!std::isfinite(scaling))
{
throw std::invalid_argument(
"scaling parameter must be finite");
}
}

template <typename iterator_t, typename mcont_t>
inline result_type
operator()(iterator_t first1, iterator_t last1, iterator_t first2,
Expand Down
37 changes: 37 additions & 0 deletions testsuite/unit/siteDepFitnessTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,22 @@ BOOST_AUTO_TEST_CASE(simple_multiplicative_trait)
BOOST_CHECK_CLOSE(w, -0.05, 1e-8);
}

BOOST_AUTO_TEST_CASE(simple_multiplicative_trait_custom_fxn)
{
fwdpp::haploid_genome g1(1), g2(1);

// add mutation at position 0.1, s=0.1,n=1,dominance=0.5 (but we won't use
// the dominance...)
mutations.emplace_back(0.1, -0.1, 0.5);
g1.smutations.emplace_back(0);
BOOST_CHECK_EQUAL(g1.smutations.size(), 1);

gcont_t g{ g1, g2 };
double w = fwdpp::multiplicative_diploid(fwdpp::trait(1.),[](double d) { return d-1.; })(g[0], g[1],
mutations);

BOOST_CHECK_CLOSE(w, -0.05, 1e-8);
}
/*
g2 has it, g1 does not
*/
Expand Down Expand Up @@ -185,6 +201,27 @@ BOOST_AUTO_TEST_CASE(simple_additive_trait)
BOOST_CHECK_EQUAL(w, -0.1);
}

BOOST_AUTO_TEST_CASE(simple_additive_trait_custom_fxn)
{
fwdpp::haploid_genome g1(1), g2(1);

// add mutation at position 0.1, s=0.1,n=1,dominance=1.0
mutations.emplace_back(0.1, 0.1, 1);
// s=-0.2 here
mutations.emplace_back(0.2, -0.2, 1);
g1.smutations.emplace_back(0);
g1.smutations.emplace_back(1);
BOOST_CHECK_EQUAL(g1.smutations.size(), 2);

gcont_t g{ g1, g2 };

auto wfxn = fwdpp::additive_diploid(fwdpp::trait(1.),[](double d){return d;});
BOOST_REQUIRE_EQUAL(wfxn.gvalue_is_trait, true);
BOOST_REQUIRE_EQUAL(wfxn.gvalue_is_fitness, false);
auto w = wfxn(g[0], g[1], mutations);
BOOST_CHECK_EQUAL(w, -0.1);
}

/*
API checks on fitness policies.
Expand Down

0 comments on commit 254a907

Please sign in to comment.