| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| {-# LANGUAGE DeriveGeneric #-} |
|
|
| module AgentGoals where |
|
|
| import Data.List (intercalate) |
| import GHC.Generics (Generic) |
| import qualified Data.Map.Strict as Map |
| import SpacetimeAgent |
| ( Agent(..), Observation(..), Frame(..), Goal(..), Subgoal(..) |
| , AgentId, RegionOfInterest(..), AnomalyType(..) |
| ) |
|
|
| |
| |
|
|
| data GoalProgress = GoalProgress |
| { progressGoalId :: Int |
| , progressType :: GoalType |
| , progressStatus :: ProgressStatus |
| , progressMetric :: Double |
| , progressObservations :: Int |
| , progressUpdatedAt :: Int |
| } deriving (Show, Eq, Generic) |
|
|
| data GoalType |
| = GoalExplore |
| | GoalMap |
| | GoalDetect |
| | GoalCollaborate |
| deriving (Show, Eq, Generic) |
|
|
| data ProgressStatus |
| = Active |
| | Paused |
| | Completed |
| | Failed |
| deriving (Show, Eq, Generic) |
|
|
| |
| |
|
|
| selectGoalForFrame :: Frame -> Observation -> Goal |
| selectGoalForFrame frame obs = |
| case frame of |
| Gravity -> |
| Map (LocalRegion 2.0) |
|
|
| Relativity -> |
| Detect TemporalAnomaly |
|
|
| Quantum -> |
| Detect SuperpositionCollapse |
|
|
| Wormhole -> |
| Explore FindConnections |
|
|
| Horizon -> |
| Detect CurvatureSpike |
|
|
| Unknown -> |
| Explore SampleRegion |
|
|
| |
| |
|
|
| inheritGoal :: Agent -> AgentId -> Goal -> Agent |
| inheritGoal agent sourceAgent newGoal = |
| let oldDecision = decision agent |
| newDecision = oldDecision { agentGoal = newGoal } |
| in agent { decision = newDecision } |
|
|
| |
|
|
| data GoalTermination |
| = SuccessfulCompletion |
| | BudgetExhausted |
| | NoProgressAfterN Int |
| | DetectedImpossible |
| deriving (Show, Eq, Generic) |
|
|
| |
| shouldAbandonGoal :: Agent -> GoalProgress -> Bool |
| shouldAbandonGoal agent progress = |
| let budget = resourceBudget (decision agent) |
| usage = currentUsage budget |
| |
| noBudget = movementUsed usage >= movementBudget budget |
| && observationUsed usage >= observationBudget budget |
| noProgress = progressObservations progress == 0 |
| in noBudget || noProgress |
|
|
| |
|
|
| data GoalTransition = GoalTransition |
| { fromGoal :: Goal |
| , toGoal :: Goal |
| , reason :: TransitionReason |
| , decidedAt :: Int |
| } deriving (Show, Eq, Generic) |
|
|
| data TransitionReason |
| = FrameChange |
| | GoalCompleted |
| | ResourceLimited |
| | PeerSuggestion AgentId |
| | OptimalitySwitch |
| deriving (Show, Eq, Generic) |
|
|
| |
| shouldTransitionGoal :: Agent -> Observation -> GoalProgress -> Maybe TransitionReason |
| shouldTransitionGoal agent obs progress = |
| let currentFrame = observerFrame (position agent) |
| selectedFrame = detectFrame obs |
| |
| frameChanged = currentFrame /= selectedFrame |
| |
| isStuck = progressMetric progress < 0.1 && progressObservations progress > 20 |
| |
| budgetLow = remainingMovement (resourceBudget (decision agent)) < 5 |
| in case () of |
| _ | frameChanged -> Just FrameChange |
| _ | isStuck -> Just OptimalitySwitch |
| _ | budgetLow -> Just ResourceLimited |
| _ -> Nothing |
|
|
| |
| detectFrame :: Observation -> Frame |
| detectFrame obs |
| | curvatureDetected obs = Gravity |
| | timeScalingDetected obs = Relativity |
| | probabilisticState obs = Quantum |
| | alternatePathsDetected obs = Wormhole |
| | eventHorizonNear obs = Horizon |
| | otherwise = Unknown |
|
|
| |
| curvatureDetected :: Observation -> Bool |
| curvatureDetected obs = |
| case Map.lookup "curvature" (obsMeasurements obs) of |
| Just v -> v > 0.1 |
| Nothing -> False |
|
|
| timeScalingDetected :: Observation -> Bool |
| timeScalingDetected obs = |
| case Map.lookup "time_scale" (obsMeasurements obs) of |
| Just v -> v /= 1.0 |
| Nothing -> False |
|
|
| probabilisticState :: Observation -> Bool |
| probabilisticState obs = |
| case Map.lookup "entropy" (obsMeasurements obs) of |
| Just v -> v > 0.3 |
| Nothing -> False |
|
|
| alternatePathsDetected :: Observation -> Bool |
| alternatePathsDetected obs = |
| case Map.lookup "paths" (obsMeasurements obs) of |
| Just v -> v > 1.0 |
| Nothing -> False |
|
|
| eventHorizonNear :: Observation -> Bool |
| eventHorizonNear obs = |
| case Map.lookup "horizon_distance" (obsMeasurements obs) of |
| Just v -> v < 1.0 |
| Nothing -> False |
|
|
| |
| remainingMovement :: ResourceBudget -> Int |
| remainingMovement b = max 0 (movementBudget b - movementUsed (currentUsage b)) |
|
|
| |
| |
|
|
| data GoalCoalignment = GoalCoalignment |
| { agentsAligned :: [AgentId] |
| , alignedGoal :: Goal |
| , synergyValue :: Double |
| , communicationBudget :: Int |
| } deriving (Show, Eq, Generic) |
|
|
| |
| computeSynergy :: [Agent] -> Goal -> Double |
| computeSynergy agents sharedGoal = |
| let matchCount = length $ filter (\a -> agentGoal (decision a) == sharedGoal) agents |
| coordCost = if matchCount > 1 then 0.1 else 0.0 |
| in max 0.0 (0.5 + fromIntegral matchCount * 0.2 - coordCost) |
|
|
| |
|
|
| data GoalHistory = GoalHistory |
| { historyAgentId :: AgentId |
| , historyGoals :: [(Int, Goal)] |
| , historyTransitions :: [GoalTransition] |
| , historySuccess :: Int |
| , historyAbandoned :: Int |
| } deriving (Show, Eq, Generic) |
|
|
| |
| recordGoalAdoption :: GoalHistory -> Int -> Goal -> GoalHistory |
| recordGoalAdoption hist timestamp goal = |
| hist { historyGoals = historyGoals hist ++ [(timestamp, goal)] } |
|
|
| |
| recordGoalCompletion :: GoalHistory -> GoalHistory |
| recordGoalCompletion hist = hist { historySuccess = historySuccess hist + 1 } |
|
|
| |
| recordGoalAbandonment :: GoalHistory -> GoalHistory |
| recordGoalAbandonment hist = hist { historyAbandoned = historyAbandoned hist + 1 } |
|
|
| |
| |
|
|
| goalAnnouncement :: Agent -> String |
| goalAnnouncement agent = |
| let aid = agentIdentity agent |
| goal = agentGoal (decision agent) |
| conf = confidenceLevel (decision agent) |
| pos = coordinates (position agent) |
| in intercalate " | " |
| [ "GOAL_ANNOUNCE:" ++ aid |
| , "goal=" ++ show goal |
| , "confidence=" ++ show conf |
| , "position=" ++ show pos |
| ] |
|
|
| |
|
|
| isGoalSatisfied :: Agent -> GoalProgress -> Bool |
| isGoalSatisfied agent progress = |
| progressMetric progress >= 0.9 |
| && progressObservations progress > 5 |
|
|
| isGoalFailing :: Agent -> GoalProgress -> Bool |
| isGoalFailing agent progress = |
| progressObservations progress > 30 |
| && progressMetric progress < 0.3 |
|
|
| |
|
|
| goalDiversity :: [Agent] -> Double |
| goalDiversity agents = |
| if null agents |
| then 0.0 |
| else |
| let goals = map (agentGoal . decision) agents |
| uniqueGoals = length $ filter (\g -> length (filter (== g) goals) == 1) goals |
| in fromIntegral uniqueGoals / fromIntegral (length agents) |
|
|
| |
| goalsReport :: [Agent] -> String |
| goalsReport agents = |
| let header = "βββ Swarm Goal Report βββ\n" |
| byGoal = countGoalsByType agents |
| diversity = goalDiversity agents |
| stats = "Total Agents: " ++ show (length agents) |
| ++ " | Goal Diversity: " ++ show (roundTo 2 diversity) ++ "\n" |
| breakdown = intercalate "\n" |
| [ " " ++ show gt ++ ": " ++ show count |
| | (gt, count) <- byGoal |
| ] |
| in header ++ stats ++ "\nBreakdown:\n" ++ breakdown |
|
|
| countGoalsByType :: [Agent] -> [(GoalType, Int)] |
| countGoalsByType agents = |
| let goals = map (agentGoal . decision) agents |
| explore = length $ filter isExploreGoal goals |
| mapGoal = length $ filter isMapGoal goals |
| detect = length $ filter isDetectGoal goals |
| collab = length $ filter isCollabGoal goals |
| in [ (GoalExplore, explore) |
| , (GoalMap, mapGoal) |
| , (GoalDetect, detect) |
| , (GoalCollaborate, collab) |
| ] |
|
|
| isExploreGoal :: Goal -> Bool |
| isExploreGoal (Explore _) = True |
| isExploreGoal _ = False |
|
|
| isMapGoal :: Goal -> Bool |
| isMapGoal (Map _) = True |
| isMapGoal _ = False |
|
|
| isDetectGoal :: Goal -> Bool |
| isDetectGoal (Detect _) = True |
| isDetectGoal _ = False |
|
|
| isCollabGoal :: Goal -> Bool |
| isCollabGoal (Collaborate _) = True |
| isCollabGoal _ = False |
|
|
| |
| roundTo :: Int -> Double -> Double |
| roundTo n x = fromIntegral (round (x * 10^n) :: Integer) / 10^n |
|
|