Link Search Menu Expand Document

Writing the model’s header files – Declaring the attributes and operations of the classes

In this tutorial, we will use Sublime Text editor to complete the model.

[ACTION] Open Sublime Text editor by clicking the symbol on the left panel. Select “File” - “Open folder” - Navigate to the MBSSM_Tutorial folder - Click “Open”.

Open folder screenshot

[ACTION] On the left panel, expand SegregationModelTutorial folder then include and src folder. Now we will define the classes from the micro level (SegregationAgent, SchellingTheory, SegregationTheoryMediator) to the macro level (Board) and finally the Model class.

Sublime Text screenshot

SegregationAgent.h

The SegregationAgent extends the MicroAgent class from the MBSSM core. It can use attributes of the core class (such as AgentId) but can also define its own attributes (agent type, satisfaction status, threshold). In the original Schelling model, the threshold is the same for all agents so we can implement that as a global variable. However, in this UML design, threshold is an attribute in the agent because we know that people have different preferences in real life (Clark and Fosset, 2008). Thus, this design is more flexible if we want to introduce threshold heterogeneity later.

In this class, we also define the agent’s ‘move’ behaviour.

UML SegregationAgent

[ACTION] Add the following lines after //TODO in the SegregationAgent.h

	/TODO: inherit MicroAgent
	class SegregationAgent : public MicroAgent {

	//TODO: define 3 variables: agent type, satisfied status, threshold
	int mAgentType;
	bool mIsSatisfied;
	double mThreshold;

	//TODO: define move() function
	void move();

SchellingTheory.h

The SchellingTheory extends the Theory class. This is where we introduce for the first time the mechanisms from Schelling’s theory – specifically the situational mechanisms and action mechanism. Since MBSSM’s systems ontology requires these types of mechanisms to be considered, it is mandatory for the modeller to include the corresponding functions, doSituation and doAction, in the model-specific theory class.

Note that isSatisfied appears as an attribute in both the SegregationAgent class and SchellingTheory class because there might be other theories that define satisfaction – but these might interact in order to produce the overall agent satisfaction.

UML SchellingTheory

[ACTION] Add the following lines after //TODO in the SchellingTheory.h

	//TODO: inherit Theory
	class SchellingTheory : public Theory {

	//TODO: define a variable satisfaction status
	bool mIsSatisfied;

	//TODO: define a variable for moving decision
	bool mMovingIntention;

	//TODO: override doSituation()
	void doSituation() override;
	
	//TODO: override doAction()
	void doAction() override;

SegregationTheoriesMediator.h

SegregationTheoriesMediator extends the TheoryMediator and connects the agent and theories together. To comply with the MBSSM systems ontology, SegregationTheoriesMediator must override the virtual functions from the core: mediateSituation and mediateAction.

UML SegregationTheoriesMediator

[ACTION] Add the following lines after //TODO in the SegregationTheoriesMediator.h

	//TODO: inherit TheoryMediator
	class SegregationTheoriesMediator : public TheoryMediator {

	//TODO: define the constructor
	SegregationTheoriesMediator(std::vector<Theory*> theoryList);
	
	//TODO: override mediateSituation()
	void mediateSituation() override;

	//TODO: override mediateAction()
	void mediateAction() override;

Board.h

UML Board

Having considered the agent entities and the situational and action mechanisms, we now move to consider the structural entities and the transformational mechanism. The Board class extends the StructuralEntity class. In order to comply with the MBSSM ontology, it must override the virtual function doTransformation. In addition, it defines 2 variables and 2 functions for updating average satisfaction and the segregation index mechanisms. These are all defined as private because they are operations that are entirely contained within the class. The overall transformational mechanism will be triggered in the doTransformation function, which is already public.

**[ACTION]** Add the following lines after //TODO in the Board.h
	//TODO: inherit StructuralEntity
	class Board : public StructuralEntity {

	//TODO: define 2 variables for satisfaction and segregation index
	double mAvgSatisfaction;
	double mSegregationIndex;

	//TODO: define 2 functions to update avg satisfaction and segregation index
	void updateAvgSatisfaction();
	void updateSegregationIndex();

	//TODO: override doTransformation();
	void doTransformation() override;

SegregationModel.h

UML SegregationModel

In the SegregationModel class, we: instantiate the agents, sequence the three mechanisms to our requirements, and tell the scheduler how often to perform the sequence.

[ACTION] Add the following lines after //TODO in the SegregationModel.h

	//TODO: define the context for agents
	repast::SharedContext<SegregationAgent> mContext;

	//TODO: define the Board structural entity
	Board* mpBoard;

	//TODO: define a function to perform actions every tick
	void doPerTick();

	//TODO: define a function to init agents
	void initAgents();

	//TODO: define a function to init schedulers
	void initSchedule(repast::ScheduleRunner& runner);