Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
CPP Ecosystem Project
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
RIVOAL Samuel
CPP Ecosystem Project
Commits
12ae982c
Commit
12ae982c
authored
4 years ago
by
fuzzy_bunny
Browse files
Options
Downloads
Patches
Plain Diff
commented the factories
parent
b590a1db
No related branches found
No related tags found
1 merge request
!19
Master
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/AnimalFactory.h
+3
-0
3 additions, 0 deletions
src/AnimalFactory.h
src/PetFactory.cpp
+15
-1
15 additions, 1 deletion
src/PetFactory.cpp
src/PetFactory.h
+7
-1
7 additions, 1 deletion
src/PetFactory.h
with
25 additions
and
2 deletions
src/AnimalFactory.h
+
3
−
0
View file @
12ae982c
...
...
@@ -7,9 +7,12 @@ using namespace std;
class
AnimalFactory
{
// it is used to create only one animal in other methods of the class
// not public because its behaviour is not for being called outside of this class
virtual
Animal
*
createMember
(
string
behaviour
=
"none"
)
=
0
;
public:
// function meant to initialize the population of animals at the start of the session
virtual
vector
<
Animal
*>
initializePopulation
(
int
number
)
=
0
;
virtual
~
AnimalFactory
()
{};
};
...
...
This diff is collapsed.
Click to expand it.
src/PetFactory.cpp
+
15
−
1
View file @
12ae982c
...
...
@@ -24,14 +24,18 @@ PetFactory::PetFactory(int xLim, int yLim, map<string, float> behavioursDistribu
string
choose_unchosen_element
(
const
unordered_set
<
string
>
remaining_elements
)
{
string
chosen_one
;
// randomly choosing an index in remaining_elements
default_random_engine
random_generator
;
uniform_int_distribution
<
int
>
distribution
(
0
,
remaining_elements
.
size
()
-
1
);
int
index
=
distribution
(
random_generator
);
auto
it
=
remaining_elements
.
begin
();
for
(
int
_i
=
0
;
_i
<
index
;
_i
++
){
it
++
;
}
chosen_one
=
*
it
;
return
chosen_one
;
}
...
...
@@ -40,6 +44,8 @@ unordered_set<string> choose_elements(const unordered_set<string> available_elem
unordered_set
<
string
>
remaining_elements
;
remaining_elements
=
available_elements
;
// randomly choosing a number of elements to choose
// this number is bound by the number of elements in available_elements
default_random_engine
random_generator
;
uniform_int_distribution
<
int
>
distribution
(
0
,
available_elements
.
size
());
int
nb_to_choose
=
distribution
(
random_generator
);
...
...
@@ -55,6 +61,8 @@ unordered_set<string> choose_elements(const unordered_set<string> available_elem
Animal
*
PetFactory
::
createMember
(
string
behaviour
)
{
Animal
*
pet
=
new
Pet
();
// choosing and adding captors and accessories to the returned pet
unordered_set
<
string
>
captorsAndAccessories
=
choose_elements
(
this
->
availableAccessoriesAndCaptors
);
for
(
string
e
:
captorsAndAccessories
)
{
if
(
e
==
"fin"
)
{
...
...
@@ -66,6 +74,8 @@ Animal* PetFactory::createMember(string behaviour) {
}
}
}
// adding the requested behaviour the returned pet
if
(
behaviour
==
"multiple"
){
pet
->
setBehaviourAsMultiple
();
}
...
...
@@ -74,6 +84,7 @@ Animal* PetFactory::createMember(string behaviour) {
pet
->
setBehaviour
(
behaviour
);
}
}
// updating stats
this
->
statistics
.
modifyData
(
behaviour
,
true
);
for
(
string
e
:
captorsAndAccessories
)
{
...
...
@@ -90,15 +101,18 @@ vector<Animal*> PetFactory::initializePopulation(int number){
map
<
string
,
int
>
toCreate
;
int
createdNumber
=
0
;
// computing the number of pets to create per behaviour
for
(
const
auto
&
dist
:
this
->
behavioursDistribution
)
{
toCreate
.
insert
(
pair
<
string
,
int
>
(
dist
.
first
,
static_cast
<
int
>
(
dist
.
second
*
number
/
100.
)));
}
// creating the pets with their behaviour and possibly some accessories and captors
for
(
const
auto
&
nbPetsPerBehaviourPair
:
toCreate
)
{
createdNumber
=
createdNumber
+
static_cast
<
int
>
(
nbPetsPerBehaviourPair
.
second
);
for
(
int
_i
=
0
;
_i
<
nbPetsPerBehaviourPair
.
second
;
_i
++
)
{
createdPets
.
push_back
(
this
->
createMember
(
nbPetsPerBehaviourPair
.
first
));
}
// in case the rounding
of the rounding
while casting to int results in a lower number of created pets than requested
// in case the rounding
,
while casting to int
,
results in a lower number of created pets than requested
}
for
(
int
_i
=
0
;
_i
<
number
-
createdNumber
;
_i
++
)
{
const
auto
nbPetsPerBehaviourPair
=
toCreate
.
end
();
...
...
This diff is collapsed.
Click to expand it.
src/PetFactory.h
+
7
−
1
View file @
12ae982c
...
...
@@ -14,15 +14,21 @@ class PetFactory final: public AnimalFactory {
int
YLIM
;
Statistics
&
statistics
;
// Distribution of pets to respect in the initial generation.
// A float number disctates the proportion in the total number of pets to create.
map
<
string
,
float
>
behavioursDistribution
;
// List of implemented accessories and captors.
// Can also be used the control the accessories and captors pets can have for a given session.
unordered_set
<
string
>
availableAccessoriesAndCaptors
;
// see comment in AnimalFactory
Animal
*
createMember
(
string
behaviour
=
"none"
)
override
;
public:
// PetFactory();
// xLim and yLim are the x and y limits for setting the position of pets in the window
PetFactory
(
int
xLim
,
int
yLim
,
map
<
string
,
float
>
behavioursDistribution
,
unordered_set
<
string
>
availableAccessoriesAndCaptors
,
Statistics
&
stats
);
// see comment in AnimalFactory
vector
<
Animal
*>
initializePopulation
(
int
number
)
override
;
};
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment