Skip to content

Commit

Permalink
track total commands and steps
Browse files Browse the repository at this point in the history
  • Loading branch information
kostmo committed Aug 31, 2023
1 parent 047812a commit d46c986
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/Swarm/Game/Robot.hs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ module Swarm.Game.Robot (
activityCounts,
tickStepBudget,
tangibleCommandCount,
anyCommandCount,
lifetimeStepCount,

-- ** Creation & instantiation
mkRobot,
Expand Down Expand Up @@ -172,6 +174,8 @@ data RobotPhase
data ActivityCounts = ActivityCounts
{ _tickStepBudget :: Int
, _tangibleCommandCount :: Int
, _anyCommandCount :: Int
, _lifetimeStepCount :: Int
}
deriving (Eq, Show, Generic, FromJSON, ToJSON)

Expand Down Expand Up @@ -218,9 +222,17 @@ makeLensesNoSigs ''ActivityCounts
-- can tell when the counter increments.
tickStepBudget :: Lens' ActivityCounts Int

-- | Total number of commands executed over robot's lifetime
-- | Total number of tangible commands executed over robot's lifetime
tangibleCommandCount :: Lens' ActivityCounts Int

-- | Total number of commands executed over robot's lifetime
anyCommandCount :: Lens' ActivityCounts Int

-- | Total number of CESK steps executed over robot's lifetime.
-- This could be thought of as "CPU cycles" consumed, and is labeled
-- as "cycles" in the F2 dialog in the UI.
lifetimeStepCount :: Lens' ActivityCounts Int

-- | With a robot template, we may or may not have a location. With a
-- concrete robot we must have a location.
type family RobotLocation (phase :: RobotPhase) :: Data.Kind.Type where
Expand Down Expand Up @@ -508,6 +520,8 @@ mkRobot rid pid name descr loc dir disp m devs inv sys heavy ts =
ActivityCounts
{ _tickStepBudget = 0
, _tangibleCommandCount = 0
, _anyCommandCount = 0
, _lifetimeStepCount = 0
}
, _runningAtomic = False
}
Expand Down
7 changes: 6 additions & 1 deletion src/Swarm/Game/Step.hs
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,10 @@ stepRobot :: (Has (State GameState) sig m, Has (Lift IO) sig m) => Robot -> m Ro
stepRobot r = do
(r', cesk') <- runState (r & activityCounts . tickStepBudget -~ 1) (stepCESK (r ^. machine))
-- sendIO $ appendFile "out.txt" (prettyString cesk' ++ "\n")
return $ r' & machine .~ cesk'
return $
r'
& machine .~ cesk'
& activityCounts . lifetimeStepCount +~ 1

-- | replace some entity in the world with another entity
updateWorld ::
Expand Down Expand Up @@ -1003,6 +1006,8 @@ execConst c vs s k = do
when (isTangible c) $
activityCounts . tangibleCommandCount %= (+ 1)

activityCounts . anyCommandCount %= (+ 1)

-- Now proceed to actually carry out the operation.
case c of
Noop -> return $ Out VUnit s k
Expand Down
4 changes: 4 additions & 0 deletions src/Swarm/TUI/View.hs
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,8 @@ robotsListWidget s = hCenter table
, "Inventory"
, "Status"
, "Actions"
, "Commands"
, "Cycles"
, "Log"
]
headers = withAttr robotAttr . txt <$> applyWhen cheat ("ID" :) headings
Expand All @@ -642,6 +644,8 @@ robotsListWidget s = hCenter table
, padRight (Pad 1) (str $ show rInvCount)
, statusWidget
, str $ show $ robot ^. activityCounts . tangibleCommandCount
, str $ show $ robot ^. activityCounts . anyCommandCount
, str $ show $ robot ^. activityCounts . lifetimeStepCount
, txt rLog
]
idWidget = str $ show $ robot ^. robotID
Expand Down

0 comments on commit d46c986

Please sign in to comment.