Package sudoku
Class Teacher
- java.lang.Object
-
- akka.actor.typed.Behavior<T>
-
- akka.actor.typed.ExtensibleBehavior<T>
-
- akka.actor.typed.javadsl.AbstractBehavior<Teacher.Protocol>
-
- sudoku.Teacher
-
public class Teacher extends akka.actor.typed.javadsl.AbstractBehavior<Teacher.Protocol>
Agent that interprets development of the playing agents and rewards them. Singleton. Parent of both Players and Tables.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classTeacher.CreateMsgMessage for creating the Teacher.static classTeacher.InspectChildDigitsMsgMessage commanding the Teacher to inspect it's Players' digits.static classTeacher.IterationTimeoutMsgReply from the TimerManager, announcing that Teacher's tables are not responding.static classTeacher.MemorisedDigitsMsgReply for a request for memorised Digits and Masks by the Player.static classTeacher.PerformedMemoryResetMsgThe agent reports to the Teacher that it had performed reset of it's memory.static classTeacher.PlayerPerformedMemoryResetMsgThe Player's version of PerformedMemoryResetMsg.static interfaceTeacher.ProtocolProtocol interface for input messages.static classTeacher.RegisteredMsgMessage received after registering an agent.static classTeacher.RegisteredPlayerMsgMessage received after registering a Player.static classTeacher.RegisteredTableMsgMessage received after registering a Table.static classTeacher.RewardReceivedMsgReply from the Player after granting it new reward.static classTeacher.SimulateCrashMsgMessage for making the Teacher crash.static classTeacher.TableFinishedNegotiationsMsgMessage reporting negotiation's finish for a one of Tables, providing solution - a digitstatic classTeacher.TablePerformedMemoryResetMsgThe Table's version of PerformedMemoryResetMsg.static classTeacher.TablesAreNotRespondingMsgReply from the TimerManager, announcing that Teacher's tables are not responding.-
Nested classes/interfaces inherited from class akka.actor.typed.Behavior
akka.actor.typed.Behavior.BehaviorDecorators<Inner extends Object>, akka.actor.typed.Behavior.BehaviorDecorators$
-
-
Field Summary
Fields Modifier and Type Field Description private Map<Integer,Integer>_inspectedDigitsHashMap for inspected digits.private akka.actor.typed.ActorRef<Sudoku>_inspectorInspector's reference.private TeacherMemory_memoryData structure for counting acknowledgement messages from Players and Tables.private akka.actor.typed.ActorRef<SudokuSupervisor.Protocol>_parentParent agentprivate Map<Integer,akka.actor.typed.ActorRef<Player.Protocol>>_playersData structure for storing all Players - child agents.private Sudoku_prevSudokuSudoku solution from the previous iterationprivate Sudoku_sudokuSudoku riddle to be solved.private Map<Integer,akka.actor.typed.ActorRef<Table.Protocol>>_tablesData structure for storing all Tables - child agents.private akka.actor.typed.ActorRef<TimerManager.Protocol>_timerManagerTeacher's own Timer.
-
Constructor Summary
Constructors Modifier Constructor Description privateTeacher(akka.actor.typed.javadsl.ActorContext<Teacher.Protocol> context, Teacher.CreateMsg createMsg)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidafterTableFinished(int tableId)Teacher marks Table as finished and checks if it was the last one - if so, calls returnNewSolution().static akka.actor.typed.Behavior<Teacher.Protocol>create(Teacher.CreateMsg createMsg)Public method that calls private constructor.akka.actor.typed.javadsl.Receive<Teacher.Protocol>createReceive()Main method controlling incoming messages.private HashSet<Integer>getNormalTableIds(Sudoku sudoku)Returns tableIds only for tables that are responsible for not hardcoded fields.private floatgetPenalty(int emptyFieldsCount, int sudokuSize)Calculate amount of base reward granted to Player at the end of big iteration.private int[]getTableIdsForPlayerId(int playerId)Generates array of tableIds for given playerId.private akka.actor.typed.Behavior<Teacher.Protocol>onInspectChildDigits(Teacher.InspectChildDigitsMsg msg)Sends MemorisedDigitsRequestMsg to all child Players.private akka.actor.typed.Behavior<Teacher.Protocol>onIterationTimeout(Teacher.IterationTimeoutMsg msg)private akka.actor.typed.Behavior<Teacher.Protocol>onMemorisedDigits(Teacher.MemorisedDigitsMsg msg)Gathers received MemorisedDigitsMsg from requested Players.private akka.actor.typed.Behavior<Teacher.Protocol>onPlayerPerformedMemoryReset(Teacher.PlayerPerformedMemoryResetMsg msg)Teacher collects messages reporting it's Players' memory being reset.private TeacheronPostStop()Handler of PostStop signal.private TeacheronPreRestart()Handler of PreRestart signal.private akka.actor.typed.Behavior<Teacher.Protocol>onRewardReceived(Teacher.RewardReceivedMsg msg)Teacher collects messages reporting receiving rewards.private akka.actor.typed.Behavior<Teacher.Protocol>onSimulateCrash(Teacher.SimulateCrashMsg simulateCrashMsg)Behaviour towards crashing message - simulates Teacher crashing.private akka.actor.typed.Behavior<Teacher.Protocol>onTableFinishedNegotiations(Teacher.TableFinishedNegotiationsMsg msg)Teacher collects messages reporting negotiations' solutions.private akka.actor.typed.Behavior<Teacher.Protocol>onTablePerformedMemoryReset(Teacher.TablePerformedMemoryResetMsg msg)Teacher collects messages reporting it's Tables' memory being reset.private akka.actor.typed.Behavior<Teacher.Protocol>onTablesAreNotResponding(Teacher.TablesAreNotRespondingMsg msg)When Teacher got TablesAreNotRespondingMsg, mentioned Tables are treated dead.private voidprepareForNewBigIterationAndRun()private voidprepareForNewSmallIterationAndRun()Teacher commands its agents (by sending ResetMemoryMsg) in appropriate order to reset theirs memory.private SudokuprepareInspectionResults()Prepares inspection results.private voidregisterAgentsOnSetup()Action of registering all Players to Tables and all Tables to Players during startup.private voidregisterMutually(akka.actor.typed.ActorRef<Player.Protocol> playerRef, int playerId, int sudokuSize, int x, int y)For given Player, registers to it the Table assigned to given position: (x,y).private voidreturnNewSolution()After collecting all parts of current iteration's solution - all digits, Teacher returns new solution as a whole represented by Sudoku object to the parent - SudokuSupervisor.private voidrewardPlayersAndRun()Hands out rewards to players and properly starts a new big iteration (resets agents' states beforehand).private voidspawnPlayers()Action of spawning all child Players agents.private voidspawnTables()Action of spawning all child Tables agents.private voidstartNewIteration()The Teacher starts new iteration of solving the sudoku.-
Methods inherited from class akka.actor.typed.javadsl.AbstractBehavior
getContext, newReceiveBuilder, receive, receiveSignal
-
-
-
-
Field Detail
-
_sudoku
private final Sudoku _sudoku
Sudoku riddle to be solved.
-
_parent
private final akka.actor.typed.ActorRef<SudokuSupervisor.Protocol> _parent
Parent agent
-
_players
private Map<Integer,akka.actor.typed.ActorRef<Player.Protocol>> _players
Data structure for storing all Players - child agents.
-
_tables
private Map<Integer,akka.actor.typed.ActorRef<Table.Protocol>> _tables
Data structure for storing all Tables - child agents.
-
_memory
private TeacherMemory _memory
Data structure for counting acknowledgement messages from Players and Tables.
-
_prevSudoku
private Sudoku _prevSudoku
Sudoku solution from the previous iteration
-
_inspectedDigits
private Map<Integer,Integer> _inspectedDigits
HashMap for inspected digits. Key: tableId, Value: inspectedDigit.
-
_inspector
private akka.actor.typed.ActorRef<Sudoku> _inspector
Inspector's reference.
-
_timerManager
private akka.actor.typed.ActorRef<TimerManager.Protocol> _timerManager
Teacher's own Timer.
-
-
Constructor Detail
-
Teacher
private Teacher(akka.actor.typed.javadsl.ActorContext<Teacher.Protocol> context, Teacher.CreateMsg createMsg)
-
-
Method Detail
-
create
public static akka.actor.typed.Behavior<Teacher.Protocol> create(Teacher.CreateMsg createMsg)
Public method that calls private constructor. Existence required by Akka.- Parameters:
createMsg- message initialising the start of the agent- Returns:
- wrapped Behavior
-
createReceive
public akka.actor.typed.javadsl.Receive<Teacher.Protocol> createReceive()
Main method controlling incoming messages. Existence required by Akka.- Specified by:
createReceivein classakka.actor.typed.javadsl.AbstractBehavior<Teacher.Protocol>- Returns:
- wrapped Behavior
-
onSimulateCrash
private akka.actor.typed.Behavior<Teacher.Protocol> onSimulateCrash(Teacher.SimulateCrashMsg simulateCrashMsg)
Behaviour towards crashing message - simulates Teacher crashing.- Parameters:
simulateCrashMsg- crashing message- Returns:
- wrapped Behavior
-
onInspectChildDigits
private akka.actor.typed.Behavior<Teacher.Protocol> onInspectChildDigits(Teacher.InspectChildDigitsMsg msg)
Sends MemorisedDigitsRequestMsg to all child Players.- Parameters:
msg- message commanding Teacher to inspect Players- Returns:
- wrapped Behavior
-
onMemorisedDigits
private akka.actor.typed.Behavior<Teacher.Protocol> onMemorisedDigits(Teacher.MemorisedDigitsMsg msg)
Gathers received MemorisedDigitsMsg from requested Players.- Parameters:
msg- inspection results- Returns:
- wrapped Behavior
-
onTablePerformedMemoryReset
private akka.actor.typed.Behavior<Teacher.Protocol> onTablePerformedMemoryReset(Teacher.TablePerformedMemoryResetMsg msg)
Teacher collects messages reporting it's Tables' memory being reset.- Parameters:
msg- reporting message- Returns:
- wrapped Behavior
-
onPlayerPerformedMemoryReset
private akka.actor.typed.Behavior<Teacher.Protocol> onPlayerPerformedMemoryReset(Teacher.PlayerPerformedMemoryResetMsg msg)
Teacher collects messages reporting it's Players' memory being reset.- Parameters:
msg- reporting message- Returns:
- wrapped Behavior
-
onTableFinishedNegotiations
private akka.actor.typed.Behavior<Teacher.Protocol> onTableFinishedNegotiations(Teacher.TableFinishedNegotiationsMsg msg)
Teacher collects messages reporting negotiations' solutions. When collected last message - it should call prepareForNewIterationAndRun and returnNewSolution. Note that a solution may be zero - meaning no consensus could be made with the Players.- Parameters:
msg- message containing solution - a digit for a specific Position in Sudoku riddle- Returns:
- wrapped Behavior
-
onRewardReceived
private akka.actor.typed.Behavior<Teacher.Protocol> onRewardReceived(Teacher.RewardReceivedMsg msg)
Teacher collects messages reporting receiving rewards. When collected last message - it is ready for new big iteration.- Parameters:
msg- message confirming reception of a reward- Returns:
- wrapped Behavior
-
onTablesAreNotResponding
private akka.actor.typed.Behavior<Teacher.Protocol> onTablesAreNotResponding(Teacher.TablesAreNotRespondingMsg msg)
When Teacher got TablesAreNotRespondingMsg, mentioned Tables are treated dead.- Parameters:
msg- warning message- Returns:
- wrapped Behavior
-
onIterationTimeout
private akka.actor.typed.Behavior<Teacher.Protocol> onIterationTimeout(Teacher.IterationTimeoutMsg msg)
-
onPostStop
private Teacher onPostStop()
Handler of PostStop signal. Expected after stopping agent.- Returns:
- wrapped Behavior
-
onPreRestart
private Teacher onPreRestart()
Handler of PreRestart signal. Expected just before restarting the agent.- Returns:
- wrapped Behavior
-
spawnPlayers
private void spawnPlayers()
Action of spawning all child Players agents.
-
spawnTables
private void spawnTables()
Action of spawning all child Tables agents.
-
registerMutually
private void registerMutually(akka.actor.typed.ActorRef<Player.Protocol> playerRef, int playerId, int sudokuSize, int x, int y)
For given Player, registers to it the Table assigned to given position: (x,y). Also, to the very same Table registers given Player. On registration on the particular Position, there is also passed to the Player a particular piece of Sudoku.- Parameters:
playerRef- a reference to the Player to whom a Table should be registered (and vice versa)playerId- ID of the PlayersudokuSize- a size of the Sudokux- x coordinate of the Position of the Tabley- y coordinate of the Position of the Table
-
registerAgentsOnSetup
private void registerAgentsOnSetup()
Action of registering all Players to Tables and all Tables to Players during startup. On registration on the particular Position, there is also passed to the Player a particular piece of Sudoku.
-
getTableIdsForPlayerId
private int[] getTableIdsForPlayerId(int playerId)
Generates array of tableIds for given playerId.- Parameters:
playerId- (global) ID of a Player- Returns:
- array of IDs of Tables matching to given Player
-
prepareInspectionResults
private Sudoku prepareInspectionResults()
Prepares inspection results.- Returns:
- inspection results - Sudoku made of inspected Digits.
-
getPenalty
private float getPenalty(int emptyFieldsCount, int sudokuSize)Calculate amount of base reward granted to Player at the end of big iteration. This reward value doesn't take reward equalization into account (the actual reward sent to Player may differ).- Parameters:
emptyFieldsCount- count of empty fields left by PlayersudokuSize- size of sudoku side- Returns:
- amount of reward (should be non-positive in most cases)
-
rewardPlayersAndRun
private void rewardPlayersAndRun()
Hands out rewards to players and properly starts a new big iteration (resets agents' states beforehand). Teacher expects a reply from Players in onRewardReceived method.
-
prepareForNewSmallIterationAndRun
private void prepareForNewSmallIterationAndRun()
Teacher commands its agents (by sending ResetMemoryMsg) in appropriate order to reset theirs memory. It should collect all messages in onTablePerformedMemoryReset and onPlayerPerformedMemoryReset, and call startNewIteration method on collecting the last one.
-
prepareForNewBigIterationAndRun
private void prepareForNewBigIterationAndRun()
-
startNewIteration
private void startNewIteration()
The Teacher starts new iteration of solving the sudoku. Method is called when all Teacher's agents are ready for new iteration. Initialisation is done by sending ConsentToStartIterationMsg to all Players.
-
returnNewSolution
private void returnNewSolution()
After collecting all parts of current iteration's solution - all digits, Teacher returns new solution as a whole represented by Sudoku object to the parent - SudokuSupervisor.
-
getNormalTableIds
private HashSet<Integer> getNormalTableIds(Sudoku sudoku)
Returns tableIds only for tables that are responsible for not hardcoded fields.
-
afterTableFinished
private void afterTableFinished(int tableId)
Teacher marks Table as finished and checks if it was the last one - if so, calls returnNewSolution().
-
-