Package sudoku

Class 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.
    • Field Detail

      • _sudoku

        private final Sudoku _sudoku
        Sudoku riddle to be solved.
      • _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.
    • 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:
        createReceive in class akka.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
      • 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 Player
        sudokuSize - a size of the Sudoku
        x - x coordinate of the Position of the Table
        y - 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 Player
        sudokuSize - 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().