Commit 2bae6f9e authored by amitjans's avatar amitjans
Browse files

Add ex6

parent 32db351f
......@@ -35,73 +35,16 @@ T simpson(const T a, const T b, const unsigned bins, const F& func)
return (func(a) + 2.*I2 + 4.*I4 - func(b)) * (dr/3.);
}
namespace A {
using result_t = double;
using argument_t = double;
class MyFunc {
/* This class takes as argument a function pointer
* to the function that we want to integrate.
* When using operator () with argument y, it returns f(x_,y) */
public:
// default constructor
MyFunc(result_t (*func)(argument_t, argument_t) = NULL, argument_t x = 0)
: func_(func), x_(x) {}
void set_x(const argument_t x) { x_ = x; }
result_t operator()(const argument_t y) const {
return (*func_)(x_, y);
}
result_t (*func_)(argument_t, argument_t);
private:
argument_t x_;
};
class MyIntegral {
/* This class takes a class MyFunc as argument.
* When calling operator () it returns the integral of f(x,y)dy at a given x*/
public:
MyIntegral(result_t (*my_func)(argument_t, argument_t), const unsigned ybins,
const argument_t a, const argument_t b)
: my_func_(my_func), a_(a), b_(b), ybins_(ybins) {}
result_t operator()(const argument_t x) {
my_func_.set_x(x);
return simpson(a_, b_, ybins_, my_func_);
}
private:
MyFunc my_func_;
const argument_t a_;
const argument_t b_;
const unsigned ybins_;
};
}
// Overloading simpson function for 2D
template<typename F, typename T>
T simpson(const T a, const T b, const T c, const T d,
const unsigned xbins, const unsigned ybins, const F& func) {
assert(xbins > 0);
assert(ybins > 0);
typename A::MyIntegral my_integral(&func, ybins, c, d);
const T dr = (b - a) / (2.*xbins);
T I2(0), I4(0); // initialize two T values to 0
T pos = a;
for(unsigned int i = 0; i < xbins; ++i) {
pos += dr;
I4 += my_integral(pos);
pos += dr;
I2 += my_integral(pos);
}
return (my_integral(a) + 2.*I2 + 4.*I4 - my_integral(b)) * (dr/3.);
auto function = [&] (T x) { return simpson(c, d, ybins,
[&] (T y) { return func(x, y); });
};
return simpson(a, b, xbins, function);
}
#endif // SIMPSON_HPP
......@@ -30,6 +30,10 @@ struct sin_lambda_x {
const double lambda;
};
// We need to provide specialization on the traits for the sin_lambda_x as it
// does not have defined input_t and output_t.
// TODO: insert code here
template<>
struct domain_t<sin_lambda_x> {
typedef double type;
......@@ -40,11 +44,6 @@ struct result_t<sin_lambda_x> {
typedef double type;
};
// We need to provide specialization on the traits for the sin_lambda_x as it
// does not have defined input_t and output_t.
// TODO: insert code here
std::complex<double> complex_exp(std::complex<double> x) { return std::exp(x); }
......
#include <iostream> // for std::cout
#include <stdexcept> // for std::runtime_error
class Plane {
public:
void start() {
std::cout << "Plane started successfully." << std::endl;
}
void serve_food() {
throw std::runtime_error("The food is not edible!");
}
void land() {
std::cout << "Plane landed successfully." << std::endl;
}
};
int main() {
// TODO: Modify this code so that the plane lands,
// but serve_food is still called.
Plane plane;
plane.start();
try {
plane.serve_food();
plane.land();
}
catch (std::runtime_error e) {
std::cout << "We catched the following error: " << e.what() << "\n";
std::cout << "We need to land immediately! Landing..." << "\n";
plane.land();
}
return 0;
}
# Require 3.1 for CMAKE_CXX_STANDARD property
cmake_minimum_required(VERSION 3.1)
project(PennaGenomeAnimalTesting)
set(CMAKE_CXX_STANDARD 11)
# Make it a Release build to add -O3 and -DNDEBUG flags by default
# Comment it out to revert to standard behaviour
set(CMAKE_BUILD_TYPE Release)
# Also add -march-native for Release builds
add_compile_options($<$<CONFIG:RELEASE>:-march=native>)
if(CMAKE_CXX_COMPILER_ID MATCHES "(C|c?)lang")
add_compile_options(-Weverything)
else()
add_compile_options(-Wall -Wextra -Wpedantic -g)
endif()
add_library(Penna STATIC genome.cpp animal.cpp)
add_executable(genome-test test-genome.cpp)
add_executable(animal-test animal-test.cpp)
target_link_libraries(genome-test Penna)
target_link_libraries(animal-test Penna)
install(TARGETS genome-test animal-test DESTINATION bin)
install(TARGETS Penna
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib)
install(FILES genome.hpp animal.hpp DESTINATION include)
enable_testing()
add_test(NAME test-genome COMMAND genome-test)
/*
* This is the test program for the animal class.
*/
#include "animal.hpp"
#include <cassert>
#include <iostream>
void test (unsigned bad_threshold, unsigned maturity_age) {
Penna::Animal::set_bad_threshold(bad_threshold);
Penna::Animal::set_maturity_age(maturity_age);
Penna::Animal::set_probability_to_get_pregnant(1);
Penna::Genome genome;
for(std::size_t index=0; index < 1000; ++index) {
genome.mutate();
Penna::Animal animal(genome);
while(!animal.is_dead()) {
assert(genome.count_bad(animal.age()) <= bad_threshold);
if(animal.is_pregnant()) {
Penna::Animal child = animal.give_birth();
assert(child.age()==0);
assert(!child.is_pregnant());
}
animal.grow();
}
assert(animal.age() == Penna::Animal::maximum_age + 1 ||
genome.count_bad(animal.age()) == bad_threshold + 1);
}
}
int main() {
//seed the random number generator
srand(42);
// normal cases
test(4,1);
test(3,4);
test(2,5);
// boundary cases
test(0,5);
test(4,Penna::Animal::maximum_age);
test(Penna::Animal::maximum_age,6);
test(Penna::Animal::maximum_age,Penna::Animal::maximum_age);
std::cout << "Animal tests passed." << std::endl;
return 0;
}
/**
* Implementation of the Penna animal class.
* Programming Techniques for Scientific Simulations, ETH Zürich
*/
#include "animal.hpp"
#include <cassert>
namespace Penna {
// Definition of static data members
age_t Animal::bad_threshold_;
age_t Animal::maturity_age_;
double Animal::probability_to_get_pregnant_;
void Animal::set_maturity_age( age_t r ) {
maturity_age_ = r;
}
void Animal::set_probability_to_get_pregnant( double p ) {
probability_to_get_pregnant_ = p;
}
void Animal::set_bad_threshold( age_t t ) {
bad_threshold_ = t;
}
age_t Animal::get_bad_threshold() {
return bad_threshold_;
}
Animal::Animal() : gen_(), age_(0), pregnant_(false) {
}
Animal::Animal( const Genome& gen ) : gen_(gen), age_(0), pregnant_(false) {
}
bool Animal::is_dead() const {
return age_ > maximum_age || gen_.count_bad(age_) > bad_threshold_;
}
bool Animal::is_pregnant() const {
return pregnant_;
}
age_t Animal::age() const {
return age_;
}
void Animal::grow() {
assert( !is_dead() );
++age_;
if (age_ > maturity_age_ && !pregnant_)
if (probability_to_get_pregnant_ > drand48())
pregnant_ = true;
}
Animal Animal::give_birth() {
assert( pregnant_ == true );
pregnant_ = false;
Genome child_genome = gen_;
// Note: this "should" return a copy such that we don't expose a freely callable mutate
// method but not doing it in-place is slow -> ideal use for C++11 move semantics
child_genome.mutate();
return Animal( child_genome );
}
} // end namespace Penna
/**
* Header for the Penna animal class.
* Programming Techniques for Scientific Simulations, ETH Zürich
*/
#ifndef ANIMAL_HPP
#define ANIMAL_HPP
#include "genome.hpp"
/// Penna namespace.
namespace Penna {
/// Animal with a Genome and age.
class Animal {
public:
static const age_t maximum_age = Genome::number_of_genes; ///< upper age limit
static void set_maturity_age( age_t ); ///< set maturity age
static void set_probability_to_get_pregnant ( double ); ///< set birth rate modifier
static void set_bad_threshold( age_t ); ///< set threshold for deletrious mutation
static age_t get_bad_threshold(); ///< get deletrious mutation threshold
Animal(); ///< default constructor: Uses all good genome
Animal( const Genome& ); ///< constructor using a given genome
bool is_dead() const; ///< get death status
bool is_pregnant() const; ///< get pregnancy status
age_t age() const; ///< get age
void grow(); ///< grow the animal older by one year
Animal give_birth(); ///< create baby with inherited, randomly mutated genes
private:
static age_t bad_threshold_; ///< number T of bad genes an animal can tolerate
static age_t maturity_age_; ///< maturity age parameter R
static double probability_to_get_pregnant_; ///< birth rate modifier b
const Genome gen_;
age_t age_;
bool pregnant_;
};
} // namespace Penna
#endif // ANIMAL_HPP
# This is the CMakeCache file.
# For build in directory: /home/albertmitjans/Developer/PTSS/albertmitjans/ex6/problem2/build
# It was generated by CMake: /usr/bin/cmake
# You can edit this file to change values found and used by cmake.
# If you do not want to change any of the values, simply exit the editor.
# If you do want to change a value, simply edit, save, and exit the editor.
# The syntax for the file is as follows:
# KEY:TYPE=VALUE
# KEY is the name of a variable in the cache.
# TYPE is a hint to GUIs for the type of VALUE, DO NOT EDIT TYPE!.
# VALUE is the current value for the KEY.
########################
# EXTERNAL cache entries
########################
//Path to a program.
CMAKE_ADDR2LINE:FILEPATH=/usr/bin/addr2line
//Path to a program.
CMAKE_AR:FILEPATH=/usr/bin/ar
//Choose the type of build, options are: None Debug Release RelWithDebInfo
// MinSizeRel ...
CMAKE_BUILD_TYPE:STRING=
//Enable/Disable color output during build.
CMAKE_COLOR_MAKEFILE:BOOL=ON
//CXX compiler
CMAKE_CXX_COMPILER:FILEPATH=/usr/bin/c++
//A wrapper around 'ar' adding the appropriate '--plugin' option
// for the GCC compiler
CMAKE_CXX_COMPILER_AR:FILEPATH=/usr/bin/gcc-ar
//A wrapper around 'ranlib' adding the appropriate '--plugin' option
// for the GCC compiler
CMAKE_CXX_COMPILER_RANLIB:FILEPATH=/usr/bin/gcc-ranlib
//Flags used by the CXX compiler during all build types.
CMAKE_CXX_FLAGS:STRING=
//Flags used by the CXX compiler during DEBUG builds.
CMAKE_CXX_FLAGS_DEBUG:STRING=-g
//Flags used by the CXX compiler during MINSIZEREL builds.
CMAKE_CXX_FLAGS_MINSIZEREL:STRING=-Os -DNDEBUG
//Flags used by the CXX compiler during RELEASE builds.
CMAKE_CXX_FLAGS_RELEASE:STRING=-O3 -DNDEBUG
//Flags used by the CXX compiler during RELWITHDEBINFO builds.
CMAKE_CXX_FLAGS_RELWITHDEBINFO:STRING=-O2 -g -DNDEBUG
//C compiler
CMAKE_C_COMPILER:FILEPATH=/usr/bin/cc
//A wrapper around 'ar' adding the appropriate '--plugin' option
// for the GCC compiler
CMAKE_C_COMPILER_AR:FILEPATH=/usr/bin/gcc-ar
//A wrapper around 'ranlib' adding the appropriate '--plugin' option
// for the GCC compiler
CMAKE_C_COMPILER_RANLIB:FILEPATH=/usr/bin/gcc-ranlib
//Flags used by the C compiler during all build types.
CMAKE_C_FLAGS:STRING=
//Flags used by the C compiler during DEBUG builds.
CMAKE_C_FLAGS_DEBUG:STRING=-g
//Flags used by the C compiler during MINSIZEREL builds.
CMAKE_C_FLAGS_MINSIZEREL:STRING=-Os -DNDEBUG
//Flags used by the C compiler during RELEASE builds.
CMAKE_C_FLAGS_RELEASE:STRING=-O3 -DNDEBUG
//Flags used by the C compiler during RELWITHDEBINFO builds.
CMAKE_C_FLAGS_RELWITHDEBINFO:STRING=-O2 -g -DNDEBUG
//Path to a program.
CMAKE_DLLTOOL:FILEPATH=CMAKE_DLLTOOL-NOTFOUND
//Flags used by the linker during all build types.
CMAKE_EXE_LINKER_FLAGS:STRING=
//Flags used by the linker during DEBUG builds.
CMAKE_EXE_LINKER_FLAGS_DEBUG:STRING=
//Flags used by the linker during MINSIZEREL builds.
CMAKE_EXE_LINKER_FLAGS_MINSIZEREL:STRING=
//Flags used by the linker during RELEASE builds.
CMAKE_EXE_LINKER_FLAGS_RELEASE:STRING=
//Flags used by the linker during RELWITHDEBINFO builds.
CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO:STRING=
//Enable/Disable output of compile commands during generation.
CMAKE_EXPORT_COMPILE_COMMANDS:BOOL=
//Install path prefix, prepended onto install directories.
CMAKE_INSTALL_PREFIX:PATH=/usr/local
//Path to a program.
CMAKE_LINKER:FILEPATH=/usr/bin/ld
//Path to a program.
CMAKE_MAKE_PROGRAM:FILEPATH=/usr/bin/make
//Flags used by the linker during the creation of modules during
// all build types.
CMAKE_MODULE_LINKER_FLAGS:STRING=
//Flags used by the linker during the creation of modules during
// DEBUG builds.
CMAKE_MODULE_LINKER_FLAGS_DEBUG:STRING=
//Flags used by the linker during the creation of modules during
// MINSIZEREL builds.
CMAKE_MODULE_LINKER_FLAGS_MINSIZEREL:STRING=
//Flags used by the linker during the creation of modules during
// RELEASE builds.
CMAKE_MODULE_LINKER_FLAGS_RELEASE:STRING=
//Flags used by the linker during the creation of modules during
// RELWITHDEBINFO builds.
CMAKE_MODULE_LINKER_FLAGS_RELWITHDEBINFO:STRING=
//Path to a program.
CMAKE_NM:FILEPATH=/usr/bin/nm
//Path to a program.
CMAKE_OBJCOPY:FILEPATH=/usr/bin/objcopy
//Path to a program.
CMAKE_OBJDUMP:FILEPATH=/usr/bin/objdump
//Value Computed by CMake
CMAKE_PROJECT_DESCRIPTION:STATIC=
//Value Computed by CMake
CMAKE_PROJECT_HOMEPAGE_URL:STATIC=
//Value Computed by CMake
CMAKE_PROJECT_NAME:STATIC=PennaGenomeAnimalTesting
//Path to a program.
CMAKE_RANLIB:FILEPATH=/usr/bin/ranlib
//Path to a program.
CMAKE_READELF:FILEPATH=/usr/bin/readelf
//Flags used by the linker during the creation of shared libraries
// during all build types.
CMAKE_SHARED_LINKER_FLAGS:STRING=
//Flags used by the linker during the creation of shared libraries
// during DEBUG builds.
CMAKE_SHARED_LINKER_FLAGS_DEBUG:STRING=
//Flags used by the linker during the creation of shared libraries
// during MINSIZEREL builds.
CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL:STRING=
//Flags used by the linker during the creation of shared libraries
// during RELEASE builds.
CMAKE_SHARED_LINKER_FLAGS_RELEASE:STRING=
//Flags used by the linker during the creation of shared libraries
// during RELWITHDEBINFO builds.
CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO:STRING=
//If set, runtime paths are not added when installing shared libraries,
// but are added when building.
CMAKE_SKIP_INSTALL_RPATH:BOOL=NO
//If set, runtime paths are not added when using shared libraries.
CMAKE_SKIP_RPATH:BOOL=NO
//Flags used by the linker during the creation of static libraries
// during all build types.
CMAKE_STATIC_LINKER_FLAGS:STRING=
//Flags used by the linker during the creation of static libraries
// during DEBUG builds.
CMAKE_STATIC_LINKER_FLAGS_DEBUG:STRING=
//Flags used by the linker during the creation of static libraries
// during MINSIZEREL builds.
CMAKE_STATIC_LINKER_FLAGS_MINSIZEREL:STRING=
//Flags used by the linker during the creation of static libraries
// during RELEASE builds.
CMAKE_STATIC_LINKER_FLAGS_RELEASE:STRING=
//Flags used by the linker during the creation of static libraries
// during RELWITHDEBINFO builds.
CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO:STRING=
//Path to a program.
CMAKE_STRIP:FILEPATH=/usr/bin/strip
//If this value is on, makefiles will be generated without the
// .SILENT directive, and all commands will be echoed to the console
// during the make. This is useful for debugging only. With Visual
// Studio IDE projects all commands are done without /nologo.
CMAKE_VERBOSE_MAKEFILE:BOOL=FALSE
//Value Computed by CMake
PennaGenomeAnimalTesting_BINARY_DIR:STATIC=/home/albertmitjans/Developer/PTSS/albertmitjans/ex6/problem2/build
//Value Computed by CMake
PennaGenomeAnimalTesting_IS_TOP_LEVEL:STATIC=ON
//Value Computed by CMake
PennaGenomeAnimalTesting_SOURCE_DIR:STATIC=/home/albertmitjans/Developer/PTSS/albertmitjans/ex6/problem2
########################
# INTERNAL cache entries
########################
//ADVANCED property for variable: CMAKE_ADDR2LINE
CMAKE_ADDR2LINE-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_AR
CMAKE_AR-ADVANCED:INTERNAL=1
//This is the directory where this CMakeCache.txt was created
CMAKE_CACHEFILE_DIR:INTERNAL=/home/albertmitjans/Developer/PTSS/albertmitjans/ex6/problem2/build
//Major version of cmake used to create the current loaded cache
CMAKE_CACHE_MAJOR_VERSION:INTERNAL=3
//Minor version of cmake used to create the current loaded cache
CMAKE_CACHE_MINOR_VERSION:INTERNAL=21
//Patch version of cmake used to create the current loaded cache
CMAKE_CACHE_PATCH_VERSION:INTERNAL=3
//ADVANCED property for variable: CMAKE_COLOR_MAKEFILE
CMAKE_COLOR_MAKEFILE-ADVANCED:INTERNAL=1
//Path to CMake executable.
CMAKE_COMMAND:INTERNAL=/usr/bin/cmake
//Path to cpack program executable.
CMAKE_CPACK_COMMAND:INTERNAL=/usr/bin/cpack
//Path to ctest program executable.
CMAKE_CTEST_COMMAND:INTERNAL=/usr/bin/ctest
//ADVANCED property for variable: CMAKE_CXX_COMPILER
CMAKE_CXX_COMPILER-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_CXX_COMPILER_AR
CMAKE_CXX_COMPILER_AR-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_CXX_COMPILER_RANLIB
CMAKE_CXX_COMPILER_RANLIB-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_CXX_FLAGS
CMAKE_CXX_FLAGS-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_CXX_FLAGS_DEBUG
CMAKE_CXX_FLAGS_DEBUG-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_CXX_FLAGS_MINSIZEREL
CMAKE_CXX_FLAGS_MINSIZEREL-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_CXX_FLAGS_RELEASE
CMAKE_CXX_FLAGS_RELEASE-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_CXX_FLAGS_RELWITHDEBINFO
CMAKE_CXX_FLAGS_RELWITHDEBINFO-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_C_COMPILER
CMAKE_C_COMPILER-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_C_COMPILER_AR
CMAKE_C_COMPILER_AR-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_C_COMPILER_RANLIB
CMAKE_C_COMPILER_RANLIB-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_C_FLAGS
CMAKE_C_FLAGS-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_C_FLAGS_DEBUG
CMAKE_C_FLAGS_DEBUG-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_C_FLAGS_MINSIZEREL
CMAKE_C_FLAGS_MINSIZEREL-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_C_FLAGS_RELEASE