-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplugin.cpp
More file actions
88 lines (73 loc) · 2.76 KB
/
Copy pathplugin.cpp
File metadata and controls
88 lines (73 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/**
* Evoplex <https://evoplex.org>
* Copyright (C) 2016-present
*/
#include "plugin.h"
namespace evoplex {
bool MinimalModel::init()
{
// gets the id of the `state` node's attribute, which is the same for all nodes
m_stateAttrId = node(0).attrs().indexOf("state");
// initializing model attributes, which is constant throughout the simulation
m_toroidal = attr("toroidal").toBool();
// if the structure is toroidal, this attribute is irrelevant
m_edgeCaseValue = attr("edgeCaseValue").toBool();
// determines which rule to use
m_rule = attr("rule").toInt();
return m_stateAttrId >= 0;
}
bool MinimalModel::algorithmStep()
{
std::vector<bool> nextStates;
nextStates.reserve(nodes().size());
for (Node node : nodes()) {
bool left_cell, right_cell;
if (node.id() == 0) { // Edge case 1: first cell
if (m_toroidal) {
left_cell = nodes().at(nodes().size()-1).attr(m_stateAttrId).toBool(); // the left cell is the last cell
} else {
left_cell = m_edgeCaseValue; // the left cell is the edge case value
}
right_cell = nodes().at(1).attr(m_stateAttrId).toBool();
} else if (node.id() == (nodes().size()-1)) { // Edge case 2: last cell
if (m_toroidal) {
right_cell = nodes().at(0).attr(m_stateAttrId).toBool(); // the right cell is the first cell
} else {
right_cell = m_edgeCaseValue; // the right cell is the edge case value
}
left_cell = nodes().at(nodes().size()-2).attr(m_stateAttrId).toBool();
} else {
// For all other cells
left_cell = nodes().at(node.id()-1).attr(m_stateAttrId).toBool();
right_cell = nodes().at(node.id()+1).attr(m_stateAttrId).toBool();
}
bool central_cell = node.attr(m_stateAttrId).toBool();
bool central_cell_next_state;
switch (m_rule) {
case 30:
central_cell_next_state = left_cell ^ (central_cell || right_cell);
break;
case 110:
central_cell_next_state = (right_cell && !central_cell) || (!left_cell && central_cell) || (!right_cell && central_cell);
break;
case 32:
central_cell_next_state = left_cell && !central_cell && right_cell;
break;
case 250:
central_cell_next_state = left_cell || right_cell;
break;
default:
break;
}
nextStates.emplace_back(central_cell_next_state);
}
size_t i = 0;
for (Node node : nodes()) {
node.setAttr(m_stateAttrId, Value(nextStates.at(i)).toBool());
++i;
}
return true;
}
} // evoplex
REGISTER_PLUGIN(MinimalModel)
#include "plugin.moc"