Skip to content

Commit

Permalink
use a histogram
Browse files Browse the repository at this point in the history
  • Loading branch information
kostmo committed Sep 1, 2023
1 parent c989488 commit 12d4d0b
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 12 deletions.
13 changes: 7 additions & 6 deletions src/Swarm/Game/Robot.hs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ module Swarm.Game.Robot (
activityCounts,
tickStepBudget,
tangibleCommandCount,
anyCommandCount,
commandsHistogram,
lifetimeStepCount,

-- ** Creation & instantiation
Expand All @@ -80,10 +80,11 @@ module Swarm.Game.Robot (
hearingDistance,
) where

import Control.Lens hiding (contains)
import Control.Lens hiding (Const, contains)
import Data.Aeson (FromJSON, ToJSON)
import Data.Hashable (hashWithSalt)
import Data.Kind qualified
import Data.Map (Map)
import Data.Maybe (fromMaybe, isNothing)
import Data.Sequence (Seq)
import Data.Sequence qualified as Seq
Expand All @@ -103,7 +104,7 @@ import Swarm.Game.Universe
import Swarm.Language.Capability (Capability)
import Swarm.Language.Context qualified as Ctx
import Swarm.Language.Requirement (ReqCtx)
import Swarm.Language.Syntax (Syntax)
import Swarm.Language.Syntax (Const, Syntax)
import Swarm.Language.Text.Markdown (Document)
import Swarm.Language.Typed (Typed (..))
import Swarm.Language.Types (TCtx)
Expand Down Expand Up @@ -174,7 +175,7 @@ data RobotPhase
data ActivityCounts = ActivityCounts
{ _tickStepBudget :: Int
, _tangibleCommandCount :: Int
, _anyCommandCount :: Int
, _commandsHistogram :: Map Const Int
, _lifetimeStepCount :: Int
}
deriving (Eq, Show, Generic, FromJSON, ToJSON)
Expand Down Expand Up @@ -226,7 +227,7 @@ tickStepBudget :: Lens' ActivityCounts Int
tangibleCommandCount :: Lens' ActivityCounts Int

-- | Total number of commands executed over robot's lifetime
anyCommandCount :: Lens' ActivityCounts Int
commandsHistogram :: Lens' ActivityCounts (Map Const Int)

-- | Total number of CESK steps executed over robot's lifetime.
-- This could be thought of as "CPU cycles" consumed, and is labeled
Expand Down Expand Up @@ -520,7 +521,7 @@ mkRobot rid pid name descr loc dir disp m devs inv sys heavy ts =
ActivityCounts
{ _tickStepBudget = 0
, _tangibleCommandCount = 0
, _anyCommandCount = 0
, _commandsHistogram = mempty
, _lifetimeStepCount = 0
}
, _runningAtomic = False
Expand Down
2 changes: 1 addition & 1 deletion src/Swarm/Game/Step.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1006,7 +1006,7 @@ execConst c vs s k = do
when (isTangible c) $
activityCounts . tangibleCommandCount %= (+ 1)

activityCounts . anyCommandCount %= (+ 1)
activityCounts . commandsHistogram %= M.insertWith (+) c 1

-- Now proceed to actually carry out the operation.
case c of
Expand Down
2 changes: 1 addition & 1 deletion src/Swarm/Language/Syntax.hs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ data Const
RobotNumbered
| -- | Check if an entity is known.
Knows
deriving (Eq, Ord, Enum, Bounded, Data, Show, Generic, FromJSON, ToJSON)
deriving (Eq, Ord, Enum, Bounded, Data, Show, Generic, FromJSON, ToJSON, FromJSONKey, ToJSONKey)

allConst :: [Const]
allConst = Util.listEnums
Expand Down
2 changes: 1 addition & 1 deletion src/Swarm/TUI/View.hs
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ robotsListWidget s = hCenter table
, padRight (Pad 1) (str $ show rInvCount)
, statusWidget
, str $ show $ robot ^. activityCounts . tangibleCommandCount
, str $ show $ robot ^. activityCounts . anyCommandCount
, str . show . sum . M.elems $ robot ^. activityCounts . commandsHistogram
, str $ show $ robot ^. activityCounts . lifetimeStepCount
, txt rLog
]
Expand Down
6 changes: 3 additions & 3 deletions test/integration/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import Swarm.Game.CESK (emptyStore, getTickNumber, initMachine)
import Swarm.Game.Entity (EntityMap, lookupByName)
import Swarm.Game.Failure (SystemFailure)
import Swarm.Game.Log (ErrorLevel (..), LogEntry, LogSource (..), leSource, leText)
import Swarm.Game.Robot (activityCounts, anyCommandCount, defReqs, equippedDevices, lifetimeStepCount, machine, robotContext, robotLog, systemRobot, tangibleCommandCount, waitingUntil)
import Swarm.Game.Robot (activityCounts, commandsHistogram, defReqs, equippedDevices, lifetimeStepCount, machine, robotContext, robotLog, systemRobot, tangibleCommandCount, waitingUntil)
import Swarm.Game.Scenario (Scenario)
import Swarm.Game.State (
GameState,
Expand Down Expand Up @@ -344,8 +344,8 @@ testScenarioSolutions rs ui =
Just base -> do
let counters = base ^. activityCounts
assertEqual "Incorrect tangible command count." 7 $ view tangibleCommandCount counters
assertEqual "Incorrect command count." 10 $ view anyCommandCount counters
assertEqual "Incorrect step count." 64 $ view lifetimeStepCount counters
assertEqual "Incorrect command count." 10 $ sum . M.elems $ view commandsHistogram counters
assertEqual "Incorrect step count." 62 $ view lifetimeStepCount counters
]
where
-- expectFailIf :: Bool -> String -> TestTree -> TestTree
Expand Down

0 comments on commit 12d4d0b

Please sign in to comment.