Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgenripa committed Jan 23, 2020
1 parent b9081de commit 70c021d
Show file tree
Hide file tree
Showing 32 changed files with 2,800 additions and 1,027 deletions.
96 changes: 96 additions & 0 deletions TREES_code/bit_vector.cpp
@@ -0,0 +1,96 @@
//
// TREES -
// A TRait-based Eco-Evolutionary Simulation tool
// Copyright (C) 2017 Jörgen Ripa
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// Contact: jorgen.ripa@biol.lu.se
//

#include <cassert>

#include "bit_vector.h"

bit_vector::bit_vector() { // default constructor
current_int = 0;
next_position = 0;
}

void bit_vector::reserve(size_type bit_count) {
size_type int_count = bit_count/(sizeof(store_type)*8) + 1;
std::vector<store_type>:: reserve(int_count);
}

void bit_vector::push_back(bool bit) {
if (bit) {
store_type mask = store_type(1)<<next_position;
current_int |= mask;
}
next_position++;
if (next_position==sizeof(store_type)*8) {
std::vector<store_type>::push_back(current_int); // store in base class
current_int = 0;
next_position = 0;
}
}

bit_vector::size_type bit_vector::get_total_bit_count() {
return size()*sizeof(store_type)*8 + next_position;
}

bit_vector::size_type bit_vector::get_total_byte_count() {
return size()*sizeof(store_type);
}

void bit_vector::write_to_file(oSimfile& osf) {
// Write size of bitstream, followed by the bits themselves
osf.write<uint32_t>((uint32_t)get_total_bit_count());
osf.write<uint32_t>((uint32_t)sizeof(store_type));
if(size()>0) {
osf.writeArray<store_type>(&at(0), (int)size());
}
if (next_position>0) {
osf.write<store_type>(current_int);
}
}

void bit_vector::read_from_file(iSimfile &isf) {
clear();
uint32_t total_bits = isf.read<uint32_t>();
uint32_t chunk_size = isf.read<uint32_t>(); // size of sore_type (in bytes)
assert(chunk_size==sizeof(store_type));
uint32_t total_bytes = total_bits / (sizeof(store_type)*8);
next_position = total_bits % (sizeof(store_type)*8); // rest bits
if(total_bytes>0) {
isf.readArray<store_type>(*this, total_bytes);
}
if (next_position>0) {
current_int = isf.read<store_type>();
} else {
current_int = 0;
}
}

bool bit_vector::get_bit(size_type bit_pos) {
size_type int_pos = bit_pos/(sizeof(store_type)*8);
store_type the_int;
if (int_pos<size()) {
the_int = at(int_pos);
} else {
the_int = current_int;
}
int bit = bit_pos % (sizeof(store_type)*8);
return (the_int & store_type(1)<<bit)>0;
}
47 changes: 47 additions & 0 deletions TREES_code/bit_vector.h
@@ -0,0 +1,47 @@
//
// TREES -
// A TRait-based Eco-Evolutionary Simulation tool
// Copyright (C) 2017 Jörgen Ripa
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// Contact: jorgen.ripa@biol.lu.se
//

#ifndef bit_vector_hpp
#define bit_vector_hpp

#include <stdio.h>
#include <vector>
#include <cstdint>
#include "simfile.h"

typedef uint64_t store_type;

class bit_vector : protected std::vector<store_type> {
protected:
store_type current_int;
int next_position;

public:
bit_vector(); // default constructor
void reserve(size_type bit_count);
void push_back(bool bit);
size_type get_total_bit_count();
size_type get_total_byte_count();
void write_to_file(oSimfile& osf);
void read_from_file(iSimfile& isf);
bool get_bit(size_type i);
};
#endif /* bit_vector_hpp */
17 changes: 9 additions & 8 deletions TREES_code/build.sh
Expand Up @@ -2,25 +2,26 @@

# build.sh
# TREES
# Created by Jörgen Ripa
# Created by Jörgen Ripa on 26/6 -14.

g++ -c -std=c++0x main.cpp
g++ -c -std=c++0x simulation.cpp
g++ -c -std=c++0x genetics.cpp
g++ -c -std=c++0x population.cpp
g++ -c -std=c++0x trait.cpp
g++ -c -std=c++0x space.cpp
g++ -c -std=c++0x transform.cpp
g++ -c -std=c++0x fitness.cpp
g++ -c -std=c++0x mating.cpp
g++ -c -std=c++0x population.cpp
g++ -c -std=c++0x space.cpp
g++ -c -std=c++0x genetics.cpp
g++ -c -std=c++0x geneTracking.cpp
g++ -c -std=c++0x genotype_phenotype_map.cpp
g++ -c -std=c++0x parameterFile.cpp
g++ -c -std=c++0x sample.cpp
g++ -c -std=c++0x simfile.cpp
g++ -c -std=c++0x bit_vector.cpp
g++ -c -std=c++0x fastmath.cpp
g++ -c -std=c++0x mytime.cpp
g++ -c -std=c++0x randgen.cpp
g++ -c -std=c++0x transform.cpp
g++ -c -std=c++0x mytime.cpp

g++ -o TREES main.o simulation.o genetics.o trait.o space.o fitness.o population.o mating.o geneTracking.o sample.o parameterFile.o simfile.o fastmath.o randgen.o mytime.o transform.o
g++ -o TREES main.o simulation.o population.o trait.o transform.o fitness.o mating.o space.o genetics.o geneTracking.o genotype_phenotype_map.o parameterFile.o simfile.o bit_vector.o fastmath.o randgen.o mytime.o

rm *.o
32 changes: 18 additions & 14 deletions TREES_code/fitness.cpp
Expand Up @@ -19,6 +19,8 @@
// Contact: jorgen.ripa@biol.lu.se
//

// Fitness calculations are generally done with double precision,
// although trait values may be in single precision.

#include <math.h>
#include <iostream>
Expand All @@ -41,6 +43,7 @@ StabilizingSelection::StabilizingSelection(Population& p, ParameterFile& pf) :
Fitness(p) {
std::string zTraitname = pf.getString("trait");
zTrait = pop.findTrait(zTraitname);
optimum = pf.getDouble("optimal_value");
cost_coefficient = pf.getDouble("cost_coefficient");
cost_exponent = pf.getDouble("cost_exponent");
}
Expand All @@ -49,17 +52,18 @@ StabilizingSelection::~StabilizingSelection() {}

void StabilizingSelection::aggregateFitness(std::vector<double>& fitness) {
//double* fi = &fitness[0];
Fastexp fexp;
for (int i=0; i<pop.size(); ++i) {
if (fitness[i]>0) {
double dzsum = 0;
for (int d=0; d<zTrait->getDims(); ++d) {
double z = zTrait->traitValue(i,d);
dzsum += z*z;
for (int d=0; d<zTrait->get_dims(); ++d) {
double dz = zTrait->traitValue(i,d) - optimum;
dzsum += dz*dz;
}
if(cost_exponent!=2) {
dzsum = fastabspow(dzsum,cost_exponent/2);
}
fitness[i] *= std::max(0.0,(1-cost_coefficient*dzsum));
fitness[i] *= fexp.exp(-cost_coefficient*dzsum);
}
}
}
Expand Down Expand Up @@ -97,7 +101,7 @@ void DensityDependence::aggregateFitness(std::vector<double>& fitness) {

// Within patch case:
} else if (pop.getSpace().isDiscrete() && s_space==0) {
DiscreteSpace& theSpace = dynamic_cast<DiscreteSpace&>(pop.getSpace());
Discrete_space& theSpace = dynamic_cast<Discrete_space&>(pop.getSpace());
int n = pop.size();

// Vector of local patch densities (linear indexing):
Expand Down Expand Up @@ -152,13 +156,13 @@ ResourceLandscape::~ResourceLandscape() {

double ResourceLandscape::getTraitDist2( int ind1, int ind2) { // squared distance in trait space
double dx2 = 0;
int dims = xTrait->getDims();
int dims = xTrait->get_dims();
if (dims==1) {
dx2 = getX(ind1) - getX(ind2);
dx2 *= dx2;
} else {
double *xp1 = &getX(ind1,0);
double *xp2 = &getX(ind2,0);
traitType *xp1 = &getX(ind1,0);
traitType *xp2 = &getX(ind2,0);
for (int d=0; d<dims; ++d) {
double dx = *xp1 - *xp2;
dx2 += dx*dx;
Expand All @@ -174,7 +178,7 @@ void ResourceLandscape::aggregateFitness(std::vector<double>& fitness)
{
double twosa2 = 2*sa*sa;
int n = pop.size();
int xdims = xTrait->getDims();
int xdims = xTrait->get_dims();
int sdims = pop.getSpace().getDims();

std::vector<double> comp;
Expand All @@ -191,7 +195,7 @@ void ResourceLandscape::aggregateFitness(std::vector<double>& fitness)
if (i<n-1) {
double ix = getX(i);
// This is a loop of complexity n^2. Use efficient iterators.
double* xcip = &getX(i+1);
traitType* xcip = &getX(i+1);
double* compcip = &comp.at(i+1);
for (int ci=i+1; ci<n; ++ci) {
double xdiff = ix - *xcip;
Expand All @@ -212,7 +216,7 @@ void ResourceLandscape::aggregateFitness(std::vector<double>& fitness)
return;
} else if (pop.getSpace().isDiscrete() && s_space==0) {
// Special case 2: within patch competition
DiscreteSpace& theSpace = dynamic_cast<DiscreteSpace&>(pop.getSpace());
Discrete_space& theSpace = dynamic_cast<Discrete_space&>(pop.getSpace());
for (int i=0; i<n; ++i) {
double compi = comp.at(i);
int patchi = theSpace.getLinearPatch(i);
Expand Down Expand Up @@ -252,7 +256,7 @@ void ResourceLandscape::aggregateFitness(std::vector<double>& fitness)
}
}
if (xdims==1) {
double *xip = &getX(0,0);
traitType *xip = &getX(0,0);
for (int i=0; i<n; ++i) {
double xopt = k_space*pop.getSpace().getPosition(i, 0);
double dx = *xip - xopt;
Expand All @@ -261,7 +265,7 @@ void ResourceLandscape::aggregateFitness(std::vector<double>& fitness)
++xip;
}
} else {
double *xip = &getX(0,0);
traitType *xip = &getX(0,0);
for (int i=0; i<n; ++i) {
double xopt = k_space*pop.getSpace().getPosition(i, 0);
double dx2 = 0;
Expand Down Expand Up @@ -325,7 +329,7 @@ void DiscreteResources::aggregateFitness(std::vector<double>& fitness) {
}
}
} else { // we can assume discrete space
DiscreteSpace& theSpace = dynamic_cast<DiscreteSpace&>(pop.getSpace());
Discrete_space& theSpace = dynamic_cast<Discrete_space&>(pop.getSpace());
for (int patchi=0; patchi<theSpace.getPatchCount(); ++patchi) {
for (int ri=0; ri<nR; ++ri) {
double consumption = 0;
Expand Down
7 changes: 4 additions & 3 deletions TREES_code/fitness.h
Expand Up @@ -51,6 +51,7 @@ class StabilizingSelection : public Fitness {
protected:
// cost = costCoefficient*sum(|dx|^costExponent)
Trait* zTrait;
double optimum;
double cost_coefficient;
double cost_exponent;
public:
Expand All @@ -77,8 +78,8 @@ class ResourceLandscape : public Fitness {
double r, K0, sK, sa, s_space, k_space;
// inline double& getTrait(int individual) { return traits[0].traitValue(individual); }
Trait* xTrait;
inline double& getX(int individual) { return xTrait->traitValue(individual); }
inline double& getX(int individual, int dim) { return xTrait->traitValue(individual,dim); }
inline traitType& getX(int individual) { return xTrait->traitValue(individual); }
inline traitType& getX(int individual, int dim) { return xTrait->traitValue(individual,dim); }
double getTraitDist2( int ind1, int ind2); // squared distance in trait space
public:
ResourceLandscape(Population& pop, ParameterFile& pf);
Expand All @@ -100,7 +101,7 @@ class DiscreteResources : public Fitness {
double ta; // trade-off
double cmin; // minimal consumption level for status quo (standard = 1)
Trait* xTrait; // resource adaptation trait (only first dimension is used)
inline double& getX(int individual) { return xTrait->traitValue(individual,0); }
inline traitType& getX(int individual) { return xTrait->traitValue(individual,0); }
public:
DiscreteResources(Population& pop, ParameterFile& pf);
virtual ~DiscreteResources();
Expand Down

0 comments on commit 70c021d

Please sign in to comment.