Evoplex  0.2.1
abstractmodel.h
1 /* Evoplex <https://evoplex.org>
2  * Copyright (C) 2016-present - Marcos Cardinot <marcos@cardinot.net>
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ABSTRACT_MODEL_H
18 #define ABSTRACT_MODEL_H
19 
20 #include <memory.h>
21 #include <vector>
22 
23 #include "abstractplugin.h"
24 #include "abstractgraph.h"
25 #include "edges.h"
26 #include "nodes.h"
27 
28 namespace evoplex {
29 
36 {
37 public:
39  virtual ~AbstractModelInterface() = default;
40 
42 
47  virtual void beforeLoop() = 0;
48 
53  virtual bool algorithmStep() = 0;
54 
59  virtual void afterLoop() = 0;
60 
71  virtual Values customOutputs(const Values& inputs) const = 0;
72 };
73 
79 {
80  friend class Trial;
81 
82 public:
85 
89  const QString& graphId() const;
90 
94  AbstractGraph* graph() const;
95 
99  int step() const;
100 
104  int lastStep() const;
105 
107  inline const Nodes& nodes() const;
109  inline Node node(int nodeId) const;
110 
112  inline const Edges& edges() const;
114  inline const Edge& edge(int edgeId) const;
116  inline const Edge& edge(int originId, int neighbourId) const;
117 
118  // AbstractModelInterface stuff
119  // the default implementation of the functions below do nothing
120  inline void beforeLoop() override {}
121  inline void afterLoop() override {}
122  inline Values customOutputs(const Values& inputs) const override
123  { Q_UNUSED(inputs); return Values(); }
124 
127 protected:
129  AbstractModel() = default;
130 };
131 
132 /************************************************************************
133  AbstractModel: Inline member functions
134  ************************************************************************/
135 
136 inline const Nodes& AbstractModel::nodes() const
137 { return graph()->nodes(); }
138 
139 inline Node AbstractModel::node(int nodeId) const
140 { return graph()->node(nodeId); }
141 
142 inline const Edges& AbstractModel::edges() const
143 { return graph()->edges(); }
144 
145 inline const Edge& AbstractModel::edge(int edgeId) const
146 { return graph()->edge(edgeId); }
147 
148 inline const Edge &AbstractModel::edge(int originId, int neighbourId) const
149 { return graph()->edge(originId, neighbourId); }
150 
151 } // evoplex
152 #endif // ABSTRACT_MODEL_H
virtual void afterLoop()=0
It is executed after the algorithmStep() loop ends.
AbstractModel()=default
constructor
const Edges & edges() const
Gets the edges.
Definition: abstractgraph.h:236
AbstractGraph * graph() const
Gets the pointer to the current graph.
It wraps a std::shared_ptr<BaseNode>.
Definition: node.h:35
int step() const
Gets the current time step.
virtual Values customOutputs(const Values &inputs) const =0
Allows implementing custom outputs for the model plugin.
Abstract base class for model plugins.
Definition: abstractmodel.h:78
void beforeLoop() override
It is executed before the algorithmStep() loop.
Definition: abstractmodel.h:120
Provides a common interface for Model plugins.
Definition: abstractmodel.h:35
virtual bool algorithmStep()=0
It is executed in a loop and must contain all the logic to perform ONE step.
Values customOutputs(const Values &inputs) const override
Allows implementing custom outputs for the model plugin.
Definition: abstractmodel.h:122
const Nodes & nodes() const
Gets the nodes.
Definition: abstractmodel.h:136
const Edges & edges() const
Gets the edges.
Definition: abstractmodel.h:142
Node node(int nodeId) const
Gets the Node corresponding to nodeId.
Definition: abstractgraph.h:248
A Node container.
Definition: nodes.h:32
void afterLoop() override
It is executed after the algorithmStep() loop ends.
Definition: abstractmodel.h:121
An Edge connects a Node to itself or to another Node.
Definition: edge.h:37
const Edge & edge(int edgeId) const
Returns the Edge corresponding to edgeId.
Definition: abstractgraph.h:242
Node node(int nodeId) const
Gets the Node corresponding to nodeId.
Definition: abstractmodel.h:139
const Edge & edge(int edgeId) const
Returns the Edge corresponding to edgeId.
Definition: abstractmodel.h:145
const QString & graphId() const
Gets the graph id.
Base class for plugins.
Definition: abstractplugin.h:34
virtual ~AbstractModelInterface()=default
Provide a default destructor to keep compilers happy.
int lastStep() const
Gets the end time step (stopAt).
virtual void beforeLoop()=0
It is executed before the algorithmStep() loop.
const Nodes & nodes() const
Gets the nodes.
Definition: abstractgraph.h:239
Definition: abstractgraph.h:29
Abstract base class for graph plugins.
Definition: abstractgraph.h:54
virtual bool init()
Initializes the plugin.
An Edge container.
Definition: edges.h:32