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 Sep 1, 2023
1 parent 047812a commit c989488
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 9 deletions.
2 changes: 1 addition & 1 deletion data/scenarios/Testing/00-ORDER.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
1320-world-DSL
1356-portals
144-subworlds
1341-command-count
1341-command-count.yaml
1355-combustion.yaml
1379-single-world-portal-reorientation.yaml
1399-backup-command.yaml
Expand Down
12 changes: 8 additions & 4 deletions data/scenarios/Testing/1341-command-count.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ solution: |
wait 10;
x <- harvest;
if (x == "flower") {
turn back;
} {move;
turn right;
move;
grab;
} {
return "";
};
robots:
- name: base
Expand All @@ -28,15 +31,16 @@ robots:
- comparator
- hourglass
- branch predictor
known: [flower]
known: [flower, tree]
world:
default: [blank]
palette:
'.': [grass]
'f': [grass, flower]
'T': [grass, tree]
'B': [grass, null, base]
upperleft: [-1, 1]
map: |
.......
.B.fff.
.......
...T...
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
11 changes: 9 additions & 2 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 (defReqs, equippedDevices, machine, robotContext, robotLog, systemRobot, waitingUntil)
import Swarm.Game.Robot (activityCounts, anyCommandCount, defReqs, equippedDevices, lifetimeStepCount, machine, robotContext, robotLog, systemRobot, tangibleCommandCount, waitingUntil)
import Swarm.Game.Scenario (Scenario)
import Swarm.Game.State (
GameState,
Expand Down Expand Up @@ -70,7 +70,7 @@ import Swarm.Util.Yaml (decodeFileEitherE)
import System.FilePath.Posix (splitDirectories)
import System.Timeout (timeout)
import Test.Tasty (TestTree, defaultMain, testGroup)
import Test.Tasty.HUnit (Assertion, assertBool, assertFailure, testCase)
import Test.Tasty.HUnit (Assertion, assertBool, assertEqual, assertFailure, testCase)
import Witch (into)

isUnparseableTest :: (FilePath, String) -> Bool
Expand Down Expand Up @@ -339,6 +339,13 @@ testScenarioSolutions rs ui =
maybe False (view systemRobot) r2
assertBool "The third built robot should be a normal robot like base." $
maybe False (not . view systemRobot) r3
, testSolution' Default "Testing/1341-command-count" CheckForBadErrors $ \g -> case g ^. robotMap . at 0 of
Nothing -> assertFailure "No base bot!"
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
]
where
-- expectFailIf :: Bool -> String -> TestTree -> TestTree
Expand Down

0 comments on commit c989488

Please sign in to comment.