Commit 88448b16 authored by amitjans's avatar amitjans
Browse files

Merge branch 'main' of gitlab.ethz.ch:amitjans/pt1-hs2021-project

parents 817bf546 3b7a0aa7
/*
Pascal: Looks good overall, well done. A few comments:
The operators
T operator*(const T a, const Z2 b)
T operator*(const Z2 b, const T a)
are symmetric. As such, you should implement them by reusing, e.g.
template<class T>
T operator*(const Z2 b, const T a) {
return a * b;
}
There's a more efficient way to implement the power function, check the
solutions.
Whenever you write a template, you must specify the corresponding concepts.
*/
/* Programming Techniques for Scientific Simulations, HS 2021
* Exercise 4.1
*/
......
......@@ -2,6 +2,14 @@
* Simpson library header
*/
/*
Pascal:
This will only work with objects of the specific type "MyFunc". You should
have used templates instead.
Note that your operator() can be const.
*/
#ifndef SIMPSON_HPP // IMPORTANT! To avoid duplications.
#define SIMPSON_HPP
#include <math.h>
......@@ -49,4 +57,4 @@ double integrate(const double a,
each bin of the corresponding interpolating parabola
*/
#endif
#endif
......@@ -2,33 +2,65 @@
#ifndef __PENNA_H__
#define __PENNA_H__
/*
Pascal: This should really be two files.
You're defining things instead of just declaring everything.
Note that the type 'unsigned long' is only guaranteed to be at least 32-bit.
See here: https://en.cppreference.com/w/cpp/language/types
If you want a type with guaranteed fixed width, you should check here:
https://en.cppreference.com/w/cpp/types/integer
In your constructor, what type does '~0' have?
What is the purpose of this constructor?
A genome should probably be able to mutate(), and we also may want to get the
number_of_bad_genes_up_to(unsigned short i)
*/
class Genome {
public:
using gentype = const unsigned long;
static const unsigned short gene_number = 64; // number B of bits of genome
Genome(const unsigned int i = 0) { // ctor
gentype temp = (i > 0) ? (gentype)1 << i : 0; // set the ith bit of temp to 1
gentype mask = ~(~0 << gene_number);
genome_ = mask & temp; // set all bits over "gene_number" to 0
}
static void set_mutation_rate(unsigned short); // setter
static void get_mutation_rate(unsigned short); // getter
static Genome clone() const;
private:
gentype genome_;
static unsigned short mutation_rate_ = 2;
}
/*
Pascal:
I would say anything related to the total population of the animals should not
be stored in an individual animal (e.g. get_max_population and the like).
I don't think something like an 'unsigned float' exists.
An animal also has an age. You may also want to have a function that ages the
animal.
Why are you providing a default value to the genome of the animal? We probably
want to be able to construct an animal from a genome.
Why do you have all these static values, if you're then trying to change them
all whenever an animal is constructed?
*/
class Fish {
public:
using intval = const unsigned int;
using floatval = const unsigned float;
Fish(intval T, intval N, intval R, floatval M) : threshold_(T), max_population_(N), mutation_rate_(R), reproduction_age_(M) {} // ctor
static void set_threshold(intval); // setters
......@@ -41,11 +73,11 @@ class Fish {
static void get_reproduction_age(intval);
static Fish generate_baby() const;
private:
Genome genome_{};
static intval threshold_; // max number of deleterious mutations before dying
static intval max_population_;
static floatval mutation_rate_; // number of bits flip when child is born
static floatval mutation_rate_; // number of bits flip when child is born
static intval reproduction_age_; // minimum age of reproduction
}
/*
Pascal: Good, that works.
Note however, that the appropriate solution to "bad food" is not really
landing immediately.
Instead, when this exception is caught, you want to alleviate the issue such
that the program can then flow on normally.
*/
#include <iostream> // for std::cout
#include <stdexcept> // for std::runtime_error
......@@ -17,7 +27,7 @@ class Plane {
};
int main() {
// TODO: Modify this code so that the plane lands,
// TODO: Modify this code so that the plane lands,
// but serve_food is still called.
Plane plane;
plane.start();
......
/*
Pascal: Looks good.
Except that you're using the index boundaries of the genome inconsistently.
Once you're calling count_bad(number_of_genes), then in the other test you
only go up to number_of_genes-1.
You may also want to test more different genomes, in a loop maybe.
*/
/* Test that the genome is initialized with only good genes by default */
#include "genome.hpp"
......
# Pascal:
# I told you weeks ago that your code to choose between shared and static
# libraries doesn't work. And now you just go on and copy paste it here again,
# with exactly the same issue.
# It's okay to make mistakes, but you have to learn from them.
# require minimum version of CMake
cmake_minimum_required(VERSION 3.21)
cmake_minimum_required(VERSION 3.21)
# set name of project
project(RandomAndTimer)
......
/* Header file for random generator */
/*
Pascal: Looks good.
You may want to have two distinct types, one that is returned
by generate(), and one that is used for internal storage.
Note that the numbers stored internally can be much larger
than what can be returned by generate().
Why are min() and max() not const?
*/
#ifndef __RANDOM_H__
#define __RANDOM_H__
......
/*
Pascal:
Note that std::clock does not perform to the specs asked
for in the exercise sheet.
*/
#include "timer.hpp"
#include <ctime>
#include <stdexcept>
......
/*
Pascal:
Why do your functions have void arguments?
*/
/* Timer header file */
#ifndef __TIMER_H__
......@@ -12,11 +17,11 @@ class Timer {
void start(void); // start counting
void stop(void); // stop counting
result_t duration(void) const; // get number of seconds
private:
time_t start_time_;
result_t duration_;
bool running_; // running state
bool running_; // running state
};
#endif
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment