Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

naive implementation #2

Open
wants to merge 1 commit into
base: logging-computation
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions core/src/main/scala/aima/core/agent/Simulation.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package aima.core.agent

import aima.core.agent.Simulation._

import scala.collection.immutable.Map

/**
* @author Damien Favre
*/
final case class Simulation[ENVIRONMENT, PERCEPT, ACTION](
environment: ENVIRONMENT,
agents: List[Agent[ENVIRONMENT, PERCEPT, ACTION]],
performanceMeasure: PerformanceMeasure[ENVIRONMENT, PERCEPT, ACTION]
) {
def run(): (Simulation[ENVIRONMENT, PERCEPT, ACTION], SimulationRunSummary[ENVIRONMENT, PERCEPT, ACTION]) = {
val (newEnv, simulationRunSummary) =
agents.foldLeft((environment, emptySimulationRunSummary[ENVIRONMENT, PERCEPT, ACTION])) {
case ((env, simulationRunSummary), agent) =>
val (newEnv, agentRunSummary) = agent.run(env)
val agentPerformance = performanceMeasure(newEnv, agent)
val agentSimulationRunSummary: AgentSimulationRunSummary[ENVIRONMENT, PERCEPT, ACTION] =
AgentSimulationRunSummary(agentRunSummary, agentPerformance)
(newEnv, simulationRunSummary + (agent -> agentSimulationRunSummary))
}
(
Simulation(newEnv, agents, performanceMeasure),
simulationRunSummary
)
}

}

object Simulation {

type PerformanceMeasure[ENVIRONMENT, PERCEPT, ACTION] =
(ENVIRONMENT, Agent[ENVIRONMENT, PERCEPT, ACTION]) => Double
type SimulationRunSummary[ENVIRONMENT, PERCEPT, ACTION] =
Map[Agent[ENVIRONMENT, PERCEPT, ACTION], AgentSimulationRunSummary[ENVIRONMENT, PERCEPT, ACTION]]
def emptySimulationRunSummary[ENVIRONMENT, PERCEPT, ACTION]: SimulationRunSummary[ENVIRONMENT, PERCEPT, ACTION] =
Map.empty
}

final case class AgentSimulationRunSummary[ENVIRONMENT, PERCEPT, ACTION](
agentRunSummary: AgentRunSummary[PERCEPT, ACTION],
performance: Double
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package aima.core.environment.vacuum

import aima.core.agent.Agent
import aima.core.agent.Simulation.PerformanceMeasure

object PerformanceMeasure {
//This measure only cares about the state of the vacuum world
val cleanliness: PerformanceMeasure[VacuumEnvironment, VacuumPercept, VacuumAction] =
(vacuumEnvironment: VacuumEnvironment, _: Agent[VacuumEnvironment, VacuumPercept, VacuumAction]) =>
vacuumEnvironment.map.nodes.collect(_.dirtStatus == CleanPercept).size
}