├── aima-csharp ├── App.config ├── logic │ ├── common │ │ ├── ParserTreeNode.cs │ │ ├── LogicTokenTypes.cs │ │ ├── LexerException.cs │ │ ├── ParserException.cs │ │ └── Token.cs │ ├── fol │ │ ├── StandardizeApartIndexical.cs │ │ ├── parsing │ │ │ ├── ast │ │ │ │ ├── Sentence.cs │ │ │ │ ├── Term.cs │ │ │ │ ├── AtomicSentence.cs │ │ │ │ ├── FOLNode.cs │ │ │ │ ├── Constant.cs │ │ │ │ ├── NotSentence.cs │ │ │ │ ├── Variable.cs │ │ │ │ ├── TermEquality.cs │ │ │ │ ├── ConnectedSentence.cs │ │ │ │ ├── Function.cs │ │ │ │ └── Predicate.cs │ │ │ ├── FOLVisitor.cs │ │ │ └── AbstractFOLVisitor.cs │ │ ├── inference │ │ │ ├── otter │ │ │ │ ├── ClauseSimplifier.cs │ │ │ │ ├── ClauseFilter.cs │ │ │ │ ├── defaultimpl │ │ │ │ │ ├── DefaultClauseFilter.cs │ │ │ │ │ ├── DefaultClauseSimplifier.cs │ │ │ │ │ └── DefaultLightestClauseHeuristic.cs │ │ │ │ └── LightestClauseHeuristic.cs │ │ │ ├── trace │ │ │ │ ├── FOLModelEliminationTracer.cs │ │ │ │ └── FOLTFMResolutiontracer.cs │ │ │ ├── proof │ │ │ │ ├── ProofStep.cs │ │ │ │ ├── AbstractProofStep.cs │ │ │ │ ├── Proof.cs │ │ │ │ ├── ProofStepGoal.cs │ │ │ │ ├── ProofStepPremise.cs │ │ │ │ ├── ProofStepFoChAlreadyAFact.cs │ │ │ │ ├── ProofStepRenaming.cs │ │ │ │ ├── ProofStepChainDropped.cs │ │ │ │ ├── ProofStepChainFromClause.cs │ │ │ │ ├── ProofStepClauseClausifySentence.cs │ │ │ │ ├── ProofStepChainContrapositive.cs │ │ │ │ ├── ProofStepClauseDemodulation.cs │ │ │ │ ├── ProofStepChainCancellation.cs │ │ │ │ ├── ProofStepChainReduction.cs │ │ │ │ ├── ProofStepClauseParamodulation.cs │ │ │ │ ├── ProofStepFoChAssertFact.cs │ │ │ │ ├── ProofStepClauseFactor.cs │ │ │ │ ├── ProofStepBwChGoal.cs │ │ │ │ ├── ProofPrinter.cs │ │ │ │ ├── ProofFinal.cs │ │ │ │ └── ProofStepClauseBinaryResolvent.cs │ │ │ ├── InferenceProcedure.cs │ │ │ ├── InferenceResultPrinter.cs │ │ │ └── InferenceResult.cs │ │ ├── domain │ │ │ ├── FOLDomainEvent.cs │ │ │ ├── FOLDomainListener.cs │ │ │ ├── FOLDomainAnswerLiteralAddedEvent.cs │ │ │ ├── FOLDomainSkolemConstantAddedEvent.cs │ │ │ ├── FOLDomainSkolemFunctionAddedEvent.cs │ │ │ └── DomainFactory.cs │ │ ├── Quantifiers.cs │ │ ├── Connectors.cs │ │ ├── kb │ │ │ └── data │ │ │ │ ├── ReducedLiteral.cs │ │ │ │ ├── CNF.cs │ │ │ │ └── Literal.cs │ │ ├── StandardizeApartResult.cs │ │ ├── PredicateCollector.cs │ │ ├── StandardizeApartIndexicalFactory.cs │ │ └── SubstVisitor.cs │ └── propositional │ │ ├── kb │ │ └── KnowledgeBase.cs │ │ └── parsing │ │ ├── ast │ │ └── AtomicSentence.cs │ │ └── PLVisitor.cs ├── Program.cs ├── agent │ ├── EnvironmentState.cs │ ├── EnvironmentObject.cs │ ├── Percept.cs │ ├── impl │ │ ├── DynamicState.cs │ │ ├── NoOpAction.cs │ │ ├── DynamicEnvironmentState.cs │ │ ├── aprog │ │ │ ├── simplerule │ │ │ │ ├── Condition.cs │ │ │ │ ├── NOTCondition.cs │ │ │ │ ├── EQUALCondition.cs │ │ │ │ ├── ORCondition.cs │ │ │ │ ├── ANDCondition.cs │ │ │ │ └── Rule.cs │ │ │ ├── SimpleReflexAgentProgram.cs │ │ │ └── TableDrivenAgentProgram.cs │ │ ├── SimpleEnvironmentView.cs │ │ ├── DynamicAction.cs │ │ ├── AbstractAgent.cs │ │ └── DynamicPercept.cs │ ├── EnvironmentViewNotifier.cs │ ├── Model.cs │ ├── Action.cs │ ├── State.cs │ ├── AgentProgram.cs │ ├── Agent.cs │ └── EnvironmentView.cs ├── util │ ├── Randomizer.cs │ ├── CSharpRandomizer.cs │ ├── MockRandomizer.cs │ ├── ArrayIterator.cs │ ├── Pair.cs │ ├── Point2D.cs │ ├── Triplet.cs │ └── SetOps.cs ├── environment │ ├── wumpusworld │ │ ├── AgentPosition.cs │ │ ├── ManhattanHeuristicFunction.cs │ │ ├── Room.cs │ │ └── AgentPercept.cs │ ├── map │ │ ├── MoveToAction.cs │ │ ├── StraightLineDistanceHeuristicFunction.cs │ │ ├── AdaptableHeuristicFunction.cs │ │ ├── DynAttributeNames.cs │ │ ├── MapEnvironmentState.cs │ │ ├── MapStepCostFunction.cs │ │ ├── Map.cs │ │ ├── BidirectionalMapProblem.cs │ │ ├── Scenario.cs │ │ ├── MapEnvironment.cs │ │ └── MapFunctionFactory.cs │ ├── eightpuzzle │ │ ├── EightPuzzleGoalTest.cs │ │ ├── BidirectionalEightPuzzleProblem.cs │ │ ├── ManhattanHeuristicFunction.cs │ │ ├── MisplacedTilleHeuristicFunction.cs │ │ └── EightPuzzleFunctionFactory.cs │ └── cellworld │ │ ├── CellWorldFactory.cs │ │ └── Cell.cs ├── search │ ├── framework │ │ ├── problem │ │ │ ├── BidirectionalProblem.cs │ │ │ ├── DefaultStepCostFunction.cs │ │ │ ├── DefaultGoalTest.cs │ │ │ ├── GoalTest.cs │ │ │ ├── ActionsFunction.cs │ │ │ ├── StepCostFunction.cs │ │ │ └── ResultFunction.cs │ │ ├── EvaluationFunction.cs │ │ ├── SearchForActions.cs │ │ ├── HeuristicFunction.cs │ │ ├── PathCostFunction.cs │ │ ├── CutOffIndicatorAction.cs │ │ ├── PerceptToStateFunction.cs │ │ ├── Search.cs │ │ ├── SolutionChecker.cs │ │ ├── SearchAgent.cs │ │ ├── Metrics.cs │ │ ├── qsearch │ │ │ └── TreeSearch.cs │ │ └── SearchUtils.cs │ └── Local │ │ ├── Scheduler.cs │ │ ├── FitnessFunction.cs │ │ └── Individual.cs └── Properties │ └── AssemblyInfo.cs ├── aima-csharp-unity ├── README.md └── .gitignore ├── aima-csharp.sln ├── LICENSE ├── .gitattributes └── CONTRIBUTING.md /aima-csharp/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /aima-csharp/logic/common/ParserTreeNode.cs: -------------------------------------------------------------------------------- 1 | namespace aima.core.logic.common 2 | { 3 | /** 4 | * @author Ravi Mohan 5 | * 6 | */ 7 | public interface ParseTreeNode 8 | { 9 | 10 | } 11 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/StandardizeApartIndexical.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace aima.core.logic.fol 4 | { 5 | /** 6 | * @author Ciaran O'Reilly 7 | * 8 | */ 9 | public interface StandardizeApartIndexical 10 | { 11 | String getPrefix(); 12 | 13 | int getNextIndex(); 14 | } 15 | } -------------------------------------------------------------------------------- /aima-csharp/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace aima_csharp 8 | { 9 | class Program 10 | { 11 | static void Main(string[] args) 12 | { 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /aima-csharp/logic/fol/parsing/ast/Sentence.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace aima.core.logic.fol.parsing.ast 4 | { 5 | /** 6 | * @author Ravi Mohan 7 | * @author Ciaran O'Reilly 8 | */ 9 | public interface Sentence : FOLNode 10 | { 11 | Sentence copySentence(); 12 | } 13 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/parsing/ast/Term.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace aima.core.logic.fol.parsing.ast 4 | { 5 | /** 6 | * @author Ravi Mohan 7 | * @author Ciaran O'Reilly 8 | */ 9 | public interface Term : FOLNode 10 | { 11 | List getArgs(); 12 | 13 | Term copy(); 14 | } 15 | } -------------------------------------------------------------------------------- /aima-csharp/agent/EnvironmentState.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace aima.core.agent 5 | { 6 | /** 7 | * An interface used to indicate a possible state of an Environment. 8 | * 9 | * @author Ciaran O'Reilly 10 | */ 11 | public interface EnvironmentState 12 | { 13 | 14 | } 15 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/parsing/ast/AtomicSentence.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace aima.core.logic.fol.parsing.ast 4 | { 5 | /** 6 | * @author Ciaran O'Reilly 7 | * 8 | */ 9 | public interface AtomicSentence : Sentence 10 | { 11 | List getArgs(); 12 | 13 | AtomicSentence copy(); 14 | } 15 | } -------------------------------------------------------------------------------- /aima-csharp/logic/propositional/kb/KnowledgeBase.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using aima.core.logic.propositional.parsing.ast; 3 | 4 | namespace aima.core.logic.propositional.kb 5 | { 6 | public class KnowledgeBase 7 | { 8 | internal void tell(Sentence sentence) 9 | { 10 | throw new NotImplementedException(); 11 | } 12 | } 13 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/inference/otter/ClauseSimplifier.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using aima.core.logic.fol.kb.data; 4 | 5 | namespace aima.core.logic.fol.inference.otter 6 | { 7 | /** 8 | * @author Ciaran O'Reilly 9 | * 10 | */ 11 | public interface ClauseSimplifier 12 | { 13 | Clause simplify(Clause c); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /aima-csharp/logic/fol/inference/otter/ClauseFilter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using aima.core.logic.fol.kb.data; 4 | 5 | namespace aima.core.logic.fol.inference.otter 6 | { 7 | /** 8 | * @author Ciaran O'Reilly 9 | * 10 | */ 11 | public interface ClauseFilter 12 | { 13 | HashSet filter(HashSet clauses); 14 | } 15 | } -------------------------------------------------------------------------------- /aima-csharp/util/Randomizer.cs: -------------------------------------------------------------------------------- 1 | namespace aima.core.util 2 | { 3 | /** 4 | * @author Ravi Mohan 5 | * 6 | */ 7 | public interface Randomizer 8 | { 9 | /** 10 | * 11 | * @return a double value, chosen (approximately) uniformly from the range 12 | * [0.0d, 1.0d), i.e. 0.0d (inclusive) to 1.0d (exclusive). 13 | */ 14 | double nextDouble(); 15 | } 16 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/inference/trace/FOLModelEliminationTracer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace aima.core.logic.fol.inference.trace 5 | { 6 | /** 7 | * @author Ciaran O'Reilly 8 | * 9 | */ 10 | public interface FOLModelEliminationTracer 11 | { 12 | void reset(); 13 | 14 | void increment(int depth, int noFarParents); 15 | } 16 | } -------------------------------------------------------------------------------- /aima-csharp/agent/EnvironmentObject.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace aima.core.agent 5 | { 6 | /** 7 | * An interface used to indicate any object that can belong within an 8 | * Environment. 9 | * 10 | * @author Ravi Mohan 11 | * @author Ciaran O'Reilly 12 | */ 13 | public interface EnvironmentObject 14 | { 15 | 16 | } 17 | } -------------------------------------------------------------------------------- /aima-csharp/environment/wumpusworld/AgentPosition.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace aima.core.environment.wumpusworld 4 | { 5 | internal class AgentPosition 6 | { 7 | internal int getX() 8 | { 9 | throw new NotImplementedException(); 10 | } 11 | 12 | internal int getY() 13 | { 14 | throw new NotImplementedException(); 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/domain/FOLDomainEvent.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace aima.core.logic.fol.domain 4 | { 5 | /** 6 | * @author Ciaran O'Reilly 7 | * 8 | */ 9 | public abstract class FOLDomainEvent 10 | { 11 | private static readonly long serialVersionUID = 1L; 12 | 13 | public FOLDomainEvent(Object source) 14 | { 15 | 16 | } 17 | 18 | public abstract void notifyListener(FOLDomainListener listener); 19 | } 20 | } -------------------------------------------------------------------------------- /aima-csharp/agent/Percept.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace aima.core.agent 5 | { 6 | /** 7 | * Artificial Intelligence A Modern Approach (3rd Edition): pg 34.
8 | * We use the term percept to refer the agent's perceptual inputs at any given instant. 9 | * 10 | * @author Ravi Mohan 11 | * @author Ciaran O'Reilly 12 | */ 13 | public interface Percept 14 | { 15 | 16 | } 17 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/inference/proof/ProofStep.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace aima.core.logic.fol.inference.proof 5 | { 6 | /** 7 | * @author Ciaran O'Reilly 8 | * 9 | */ 10 | public interface ProofStep 11 | { 12 | int getStepNumber(); 13 | 14 | void setStepNumber(int step); 15 | 16 | List getPredecessorSteps(); 17 | 18 | String getProof(); 19 | 20 | String getJustification(); 21 | } 22 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/domain/FOLDomainListener.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace aima.core.logic.fol.domain 4 | { 5 | /** 6 | * @author Ciaran O'Reilly 7 | * 8 | */ 9 | public interface FOLDomainListener 10 | { 11 | void skolemConstantAdded(FOLDomainSkolemConstantAddedEvent _event); 12 | 13 | void skolemFunctionAdded(FOLDomainSkolemFunctionAddedEvent _event); 14 | 15 | void answerLiteralNameAdded(FOLDomainAnswerLiteralAddedEvent _event); 16 | } 17 | } -------------------------------------------------------------------------------- /aima-csharp-unity/README.md: -------------------------------------------------------------------------------- 1 | # ![](https://github.com/aimacode/aima-java/blob/gh-pages/aima3e/images/aima3e.jpg)aima-csharp-unity 2 | ![Unity](https://madewith.unity.com/profiles/mwu/themes/custom/mwu_theme/logo.png) 3 | Unity implementation of algorithms from [Russell](http://www.cs.berkeley.edu/~russell/) And [Norvig's](http://www.norvig.com/) [Artificial Intelligence - A Modern Approach 3rd Edition](http://aima.cs.berkeley.edu/). You can use this in conjunction with a course on AI, or for study on your own. 4 | -------------------------------------------------------------------------------- /aima-csharp-unity/.gitignore: -------------------------------------------------------------------------------- 1 | /[Ll]ibrary/ 2 | /[Tt]emp/ 3 | /[Oo]bj/ 4 | /[Bb]uild/ 5 | /[Bb]uilds/ 6 | /Assets/AssetStoreTools* 7 | 8 | # Autogenerated VS/MD solution and project files 9 | ExportedObj/ 10 | *.csproj 11 | *.unityproj 12 | *.sln 13 | *.suo 14 | *.tmp 15 | *.user 16 | *.userprefs 17 | *.pidb 18 | *.booproj 19 | *.svd 20 | 21 | 22 | # Unity3D generated meta files 23 | *.pidb.meta 24 | 25 | # Unity3D Generated File On Crash Reports 26 | sysinfo.txt 27 | 28 | # Builds 29 | *.apk 30 | *.unitypackage -------------------------------------------------------------------------------- /aima-csharp/agent/impl/DynamicState.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using aima.core.agent; 4 | 5 | namespace aima.core.agent.impl 6 | { 7 | /** 8 | * @author Ciaran O'Reilly 9 | */ 10 | public class DynamicState : ObjectWithDynamicAttributes, State 11 | { 12 | public DynamicState() 13 | { 14 | 15 | } 16 | 17 | 18 | public String describeType() 19 | { 20 | return typeof(State).Name; 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /aima-csharp/logic/propositional/parsing/ast/AtomicSentence.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace aima.core.logic.propositional.parsing.ast 4 | { 5 | /** 6 | * Artificial Intelligence A Modern Approach (3rd Edition): page 244.
7 | *
8 | * The atomic sentences consist of a single proposition symbol. 9 | * 10 | * @author Ravi Mohan 11 | * @author Ciaran O'Reilly 12 | * 13 | */ 14 | public abstract class AtomicSentence : Sentence 15 | { 16 | 17 | } 18 | } -------------------------------------------------------------------------------- /aima-csharp/agent/impl/NoOpAction.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace aima.core.agent.impl 5 | { 6 | public class NoOpAction : DynamicAction 7 | { 8 | public static readonly NoOpAction NO_OP = new NoOpAction(); 9 | 10 | // START-Action 11 | public bool isNoOp() 12 | { 13 | return true; 14 | } 15 | 16 | //END-Action 17 | private NoOpAction() : base("NoOp") 18 | { 19 | 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /aima-csharp/search/framework/problem/BidirectionalProblem.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace aima.core.search.framework.problem 4 | { 5 | /** 6 | * An interface describing a problem that can be tackled from both directions at 7 | * once (i.e InitialState<->Goal). 8 | * 9 | * @author Ciaran O'Reilly 10 | * 11 | */ 12 | public interface BidirectionalProblem 13 | { 14 | Problem getOriginalProblem(); 15 | 16 | Problem getReverseProblem(); 17 | } 18 | } -------------------------------------------------------------------------------- /aima-csharp/search/framework/problem/DefaultStepCostFunction.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using aima.core.agent; 3 | 4 | namespace aima.core.search.framework.problem 5 | { 6 | /** 7 | * Returns one for every action. 8 | * 9 | * @author Ravi Mohan 10 | */ 11 | public class DefaultStepCostFunction : StepCostFunction 12 | { 13 | 14 | 15 | public double c(System.Object stateFrom, Action action, System.Object stateTo) 16 | { 17 | return 1; 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /aima-csharp/agent/EnvironmentViewNotifier.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace aima.core.agent 5 | { 6 | /** 7 | * @author Ciaran O'Reilly 8 | * 9 | */ 10 | public interface EnvironmentViewNotifier 11 | { 12 | /** 13 | * A simple notification message, to be forwarded to an Environment's 14 | * registered EnvironmentViews. 15 | * 16 | * @param msg 17 | * the message to be forwarded to the EnvironmentViews. 18 | */ 19 | void notifyViews(String msg); 20 | } 21 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/Quantifiers.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace aima.core.logic.fol 4 | { 5 | /** 6 | * @author Ciaran O'Reilly 7 | * 8 | */ 9 | public class Quantifiers 10 | { 11 | public static readonly String FORALL = "FORALL"; 12 | public static readonly String EXISTS = "EXISTS"; 13 | 14 | public static bool isFORALL(String quantifier) 15 | { 16 | return FORALL.Equals(quantifier); 17 | } 18 | 19 | public static bool isEXISTS(String quantifier) 20 | { 21 | return EXISTS.Equals(quantifier); 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/parsing/ast/FOLNode.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using aima.core.logic.common; 4 | using aima.core.logic.fol.parsing; 5 | 6 | namespace aima.core.logic.fol.parsing.ast 7 | { 8 | /** 9 | * @author Ravi Mohan 10 | * @author Ciaran O'Reilly 11 | */ 12 | public interface FOLNode : ParseTreeNode 13 | { 14 | String getSymbolicName(); 15 | 16 | bool isCompound(); 17 | 18 | List getArgs(); 19 | 20 | Object accept(FOLVisitor v, Object arg); 21 | 22 | FOLNode copy(); 23 | } 24 | } -------------------------------------------------------------------------------- /aima-csharp/search/framework/EvaluationFunction.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace aima.core.search.framework 5 | { 6 | /** 7 | * Artificial Intelligence A Modern Approach (3rd Edition): page 92.
8 | *
9 | * The evaluation function is construed as a cost estimate, so the node with the 10 | * lowest evaluation is expanded first. 11 | * 12 | * @author Ciaran O'Reilly 13 | * 14 | */ 15 | public interface EvaluationFunction 16 | { 17 | double f(Node n); 18 | } 19 | } -------------------------------------------------------------------------------- /aima-csharp/environment/map/MoveToAction.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using aima.core.agent.impl; 4 | 5 | namespace aima.core.environment.map 6 | { 7 | public class MoveToAction : DynamicAction 8 | { 9 | public const String ATTRIBUTE_MOVE_TO_LOCATION = "location"; 10 | 11 | public MoveToAction(String location) : base("moveTo") 12 | { 13 | setAttribute(ATTRIBUTE_MOVE_TO_LOCATION, location); 14 | } 15 | 16 | public String getToLocation() 17 | { 18 | return (String)getAttribute(ATTRIBUTE_MOVE_TO_LOCATION); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /aima-csharp/agent/impl/DynamicEnvironmentState.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using aima.core.agent; 4 | 5 | namespace aima.core.agent.impl 6 | { 7 | /** 8 | * @author Ravi Mohan 9 | * @author Ciaran O'Reilly 10 | */ 11 | public class DynamicEnvironmentState : ObjectWithDynamicAttributes, EnvironmentState 12 | { 13 | public DynamicEnvironmentState() 14 | { 15 | 16 | } 17 | 18 | public String describeType() 19 | { 20 | return typeof(EnvironmentState).Name; 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /aima-csharp/agent/Model.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace aima.core.agent 5 | { 6 | /** 7 | * Artificial Intelligence A Modern Approach (3rd Edition): pg 50.
8 | * 9 | * This knowledge about "how the world works" - whether implemented in simple bool circuits 10 | * or in complete scientific theories - is called a model of the world. An Agent that uses such a 11 | * model is called a model-based agent. 12 | * 13 | * @author Ciaran O'Reilly 14 | */ 15 | public interface Model 16 | { 17 | 18 | } 19 | } -------------------------------------------------------------------------------- /aima-csharp/search/framework/SearchForActions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using aima.core.agent; 4 | 5 | namespace aima.core.search.framework 6 | { 7 | /** 8 | * Interface for all search algorithms which store at least a part of the 9 | * exploration history as search tree and return a list of actions which lead 10 | * from the initial state to a goal state. 11 | * 12 | * @author Ruediger Lunde 13 | * 14 | */ 15 | public interface SearchForActions : Search 16 | { 17 | NodeExpander getNodeExpander(); 18 | } 19 | } -------------------------------------------------------------------------------- /aima-csharp/environment/eightpuzzle/EightPuzzleGoalTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using aima.core.search.framework.problem; 4 | 5 | namespace aima.core.environment.eightpuzzle 6 | { 7 | /** 8 | * @author Ravi Mohan 9 | * 10 | */ 11 | public class EightPuzzleGoalTest : GoalTest 12 | { 13 | EightPuzzleBoard goal = new EightPuzzleBoard(new int[] { 0, 1, 2, 3, 4, 5, 14 | 6, 7, 8 }); 15 | 16 | public bool isGoalState(Object state) 17 | { 18 | EightPuzzleBoard board = (EightPuzzleBoard)state; 19 | return board.Equals(goal); 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /aima-csharp/agent/Action.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace aima.core.agent 5 | { 6 | /** 7 | * Describes an Action that can or has been taken by an Agent via one of its Actuators. 8 | * 9 | * @author Ciaran O'Reilly 10 | */ 11 | public interface Action 12 | { 13 | /** 14 | * Indicates whether or not this Action is a 'No Operation'.
15 | * Note: AIMA3e - NoOp, or no operation, is the name of an assembly language 16 | * instruction that does nothing. 17 | * 18 | * @return true if this is a NoOp Action. 19 | */ 20 | bool isNoOp(); 21 | } 22 | } -------------------------------------------------------------------------------- /aima-csharp/logic/common/LogicTokenTypes.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace aima.core.logic.common 5 | { 6 | /** 7 | * @author Ravi Mohan 8 | * 9 | */ 10 | public enum LogicTokenTypes : int 11 | { 12 | SYMBOL = 1, 13 | 14 | LPAREN = 2, 15 | 16 | RPAREN = 3, 17 | 18 | COMMA = 4, 19 | 20 | CONNECTOR = 5, 21 | 22 | QUANTIFIER = 6, 23 | 24 | PREDICATE = 7, 25 | 26 | FUNCTION = 8, 27 | 28 | VARIABLE = 9, 29 | 30 | CONSTANT = 10, 31 | 32 | TRUE = 11, 33 | 34 | FALSE = 12, 35 | 36 | EQUALS = 13, 37 | 38 | WHITESPACE = 1000, 39 | 40 | EOI = 9999 // End of Input. 41 | } 42 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/inference/otter/defaultimpl/DefaultClauseFilter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using aima.core.logic.fol.inference.otter; 4 | using aima.core.logic.fol.kb.data; 5 | 6 | namespace aima.core.logic.fol.inference.otter.defaultimpl 7 | { 8 | /** 9 | * @author Ciaran O'Reilly 10 | * 11 | */ 12 | public class DefaultClauseFilter : ClauseFilter 13 | { 14 | public DefaultClauseFilter() 15 | { 16 | 17 | } 18 | 19 | // START-ClauseFilter 20 | 21 | public HashSet filter(HashSet clauses) 22 | { 23 | return clauses; 24 | } 25 | 26 | // END-ClauseFilter 27 | } 28 | } -------------------------------------------------------------------------------- /aima-csharp/search/framework/problem/DefaultGoalTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace aima.core.search.framework.problem 5 | { 6 | /** 7 | * Checks whether a given state equals an explicitly specified goal state. 8 | * 9 | * @author Ruediger Lunde 10 | */ 11 | public class DefaultGoalTest : GoalTest 12 | { 13 | 14 | private Object goalState; 15 | 16 | public DefaultGoalTest(Object goalState) 17 | { 18 | this.goalState = goalState; 19 | } 20 | 21 | public bool isGoalState(Object state) 22 | { 23 | return goalState.Equals(state); 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/inference/InferenceProcedure.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using aima.core.logic.fol.kb; 4 | using aima.core.logic.fol.parsing.ast; 5 | 6 | namespace aima.core.logic.fol.inference 7 | { 8 | /** 9 | * @author Ciaran O'Reilly 10 | * 11 | */ 12 | public interface InferenceProcedure 13 | { 14 | /** 15 | * 16 | * @param kb 17 | * the knowledge base against which the query is to be made. 18 | * @param query 19 | * the query to be answered. 20 | * @return an InferenceResult. 21 | */ 22 | InferenceResult ask(FOLKnowledgeBase kb, Sentence query); 23 | } 24 | } -------------------------------------------------------------------------------- /aima-csharp/agent/State.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace aima.core.agent 5 | { 6 | /** 7 | * Artificial Intelligence A Modern Approach (3rd Edition): pg 50.
8 | * 9 | * The most effective way to handle partial observability is for the agent to keep track of the 10 | * part of the world it can't see now. That is, the agent should maintain some sort of internal 11 | * state that depends on the percept history and thereby reflects at least some of the unobserved 12 | * aspects of the current state. 13 | * 14 | * @author Ciaran O'Reilly 15 | */ 16 | public interface State 17 | { 18 | 19 | } 20 | } -------------------------------------------------------------------------------- /aima-csharp/search/framework/HeuristicFunction.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace aima.core.search.framework 5 | { 6 | /** 7 | * Artificial Intelligence A Modern Approach (3rd Edition): page 92.
8 | *
9 | * a heuristic function, denoted h(n):
10 | * h(n) = estimated cost of the cheapest path from the state at node n to a goal 11 | * state.
12 | *
13 | * Notice that h(n) takes a node as input, but, unlike g(n) it depends only on 14 | * the state at that node. 15 | * 16 | * @author Ravi Mohan 17 | * 18 | */ 19 | public interface HeuristicFunction 20 | { 21 | double h(Object state); 22 | } 23 | } -------------------------------------------------------------------------------- /aima-csharp/search/framework/problem/GoalTest.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace aima.core.search.framework.problem 4 | { 5 | /** 6 | * Artificial Intelligence A Modern Approach (3rd Edition): page 67.
7 | *
8 | * The goal test, which determines whether a given state is a goal state. 9 | * 10 | * @author Ravi Mohan 11 | * @author Mike Stampone 12 | */ 13 | public interface GoalTest 14 | { 15 | /** 16 | * Returns true if the given state is a goal state. 17 | * 18 | * @return true if the given state is a goal state. 19 | */ 20 | bool isGoalState(System.Object state); 21 | } 22 | } -------------------------------------------------------------------------------- /aima-csharp/util/CSharpRandomizer.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System; 3 | 4 | namespace aima.core.util 5 | { 6 | /** 7 | * Implementation of the Randomizer Interface using Java's java.util.Random 8 | * class. 9 | * 10 | * @author Ravi Mohan 11 | * 12 | */ 13 | public class CSharpRandomizer : Randomizer 14 | { 15 | private static Random _r = new Random(); 16 | private Random r = null; 17 | 18 | public CSharpRandomizer(): this(_r) 19 | { 20 | 21 | } 22 | 23 | public CSharpRandomizer(Random r) 24 | { 25 | this.r = r; 26 | } 27 | 28 | // START-Randomizer 29 | public double nextDouble() 30 | { 31 | return r.NextDouble(); 32 | } 33 | 34 | // END-Randomizer 35 | } 36 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/inference/trace/FOLTFMResolutiontracer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using aima.core.logic.fol.inference; 4 | using aima.core.logic.fol.kb.data; 5 | 6 | namespace aima.core.logic.fol.inference.trace 7 | { 8 | /** 9 | * @author Ciaran O'Reilly 10 | * 11 | */ 12 | public interface FOLTFMResolutionTracer 13 | { 14 | void stepStartWhile(HashSet clauses, int totalNoClauses, 15 | int totalNoNewCandidateClauses); 16 | 17 | void stepOuterFor(Clause i); 18 | 19 | void stepInnerFor(Clause i, Clause j); 20 | 21 | void stepResolved(Clause iFactor, Clause jFactor, HashSet resolvents); 22 | 23 | void stepFinished(HashSet clauses, InferenceResult result); 24 | } 25 | } -------------------------------------------------------------------------------- /aima-csharp/environment/map/StraightLineDistanceHeuristicFunction.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using aima.core.util; 3 | 4 | namespace aima.core.environment.map 5 | { 6 | /** 7 | * @author Ruediger Lunde 8 | */ 9 | public class StraightLineDistanceHeuristicFunction : AdaptableHeuristicFunction 10 | { 11 | public StraightLineDistanceHeuristicFunction(Object goal, Map map) 12 | { 13 | this.goal = goal; 14 | this.map = map; 15 | } 16 | 17 | public double h(Object state) 18 | { 19 | Double result = 0.0; 20 | Point2D pt1 = map.getPosition((String)state); 21 | Point2D pt2 = map.getPosition((String)goal); 22 | if (pt1 != null && pt2 != null) 23 | { 24 | result = pt1.distance(pt2); 25 | } 26 | return result; 27 | } 28 | } 29 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/inference/proof/AbstractProofStep.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace aima.core.logic.fol.inference.proof 5 | { 6 | /** 7 | * @author Ciaran O'Reilly 8 | * 9 | */ 10 | public abstract class AbstractProofStep : ProofStep 11 | { 12 | private int step = 0; 13 | 14 | public AbstractProofStep() 15 | { 16 | 17 | } 18 | 19 | // START-ProofStep 20 | 21 | public int getStepNumber() 22 | { 23 | return step; 24 | } 25 | 26 | public void setStepNumber(int step) 27 | { 28 | this.step = step; 29 | } 30 | 31 | public abstract List getPredecessorSteps(); 32 | 33 | public abstract String getProof(); 34 | 35 | public abstract String getJustification(); 36 | 37 | // END-ProofStep 38 | } 39 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/domain/FOLDomainAnswerLiteralAddedEvent.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace aima.core.logic.fol.domain 4 | { 5 | /** 6 | * @author Ciaran O'Reilly 7 | * 8 | */ 9 | public class FOLDomainAnswerLiteralAddedEvent : FOLDomainEvent 10 | { 11 | private static readonly long serialVersionUID = 1L; 12 | 13 | private String answerLiteralName; 14 | 15 | public FOLDomainAnswerLiteralAddedEvent(Object source, 16 | String answerLiteralName): base(source) 17 | { 18 | this.answerLiteralName = answerLiteralName; 19 | } 20 | 21 | public String getAnswerLiteralNameName() 22 | { 23 | return answerLiteralName; 24 | } 25 | 26 | public override void notifyListener(FOLDomainListener listener) 27 | { 28 | listener.answerLiteralNameAdded(this); 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/domain/FOLDomainSkolemConstantAddedEvent.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace aima.core.logic.fol.domain 4 | { 5 | /** 6 | * @author Ciaran O'Reilly 7 | * 8 | */ 9 | public class FOLDomainSkolemConstantAddedEvent : FOLDomainEvent 10 | { 11 | private static readonly long serialVersionUID = 1L; 12 | 13 | private String skolemConstantName; 14 | 15 | public FOLDomainSkolemConstantAddedEvent(Object source, 16 | String skolemConstantName): base(source) 17 | { 18 | this.skolemConstantName = skolemConstantName; 19 | } 20 | 21 | public String getSkolemConstantName() 22 | { 23 | return skolemConstantName; 24 | } 25 | 26 | public override void notifyListener(FOLDomainListener listener) 27 | { 28 | listener.skolemConstantAdded(this); 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/domain/FOLDomainSkolemFunctionAddedEvent.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace aima.core.logic.fol.domain 4 | { 5 | /** 6 | * @author Ciaran O'Reilly 7 | * 8 | */ 9 | public class FOLDomainSkolemFunctionAddedEvent : FOLDomainEvent 10 | { 11 | private static readonly long serialVersionUID = 1L; 12 | 13 | private String skolemFunctionName; 14 | 15 | public FOLDomainSkolemFunctionAddedEvent(Object source, 16 | String skolemFunctionName): base(source) 17 | { 18 | this.skolemFunctionName = skolemFunctionName; 19 | } 20 | 21 | public String getSkolemConstantName() 22 | { 23 | return skolemFunctionName; 24 | } 25 | 26 | public override void notifyListener(FOLDomainListener listener) 27 | { 28 | listener.skolemFunctionAdded(this); 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /aima-csharp/search/framework/PathCostFunction.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace aima.core.search.framework 5 | { 6 | /** 7 | * Artificial Intelligence A Modern Approach (3rd Edition): page 78.
8 | *
9 | * 10 | * @author Ciaran O'Reilly 11 | * 12 | */ 13 | public class PathCostFunction 14 | { 15 | public PathCostFunction() 16 | { 17 | 18 | } 19 | 20 | /** 21 | * 22 | * @param n 23 | * @return the cost, traditionally denoted by g(n), of the path from the 24 | * initial state to the node, as indicated by the parent pointers. 25 | */ 26 | public double g(Node n) 27 | { 28 | return n.PathCost; 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /aima-csharp/search/framework/CutOffIndicatorAction.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using aima.core.agent.impl; 4 | 5 | namespace aima.core.search.framework 6 | { 7 | /** 8 | * A NoOp action that indicates a CutOff has occurred in a search. Used 9 | * primarily by DepthLimited and IterativeDeepening search routines. 10 | * 11 | * @author Ciaran O'Reilly 12 | */ 13 | public class CutOffIndicatorAction : DynamicAction 14 | { 15 | public static readonly CutOffIndicatorAction CUT_OFF = new CutOffIndicatorAction(); 16 | 17 | // START-Action 18 | public bool isNoOp() 19 | { 20 | return true; 21 | } 22 | 23 | // END-Action 24 | private CutOffIndicatorAction(): base("CutOff") 25 | { 26 | 27 | } 28 | } 29 | } -------------------------------------------------------------------------------- /aima-csharp/search/framework/PerceptToStateFunction.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using aima.core.agent; 4 | 5 | namespace aima.core.search.framework 6 | { 7 | /** 8 | * This interface is to define how to Map a Percept to a State representation 9 | * for a problem solver within a specific environment. This arises in the 10 | * description of the Online Search algorithms from Chapter 4. 11 | * 12 | * @author Ciaran O'Reilly 13 | * 14 | */ 15 | public interface PerceptToStateFunction 16 | { 17 | /** 18 | * Get the problem state associated with a Percept. 19 | * 20 | * @param p 21 | * the percept to be transformed to a problem state. 22 | * @return a problem state derived from the Percept p. 23 | */ 24 | Object getState(Percept p); 25 | } 26 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/parsing/FOLVisitor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using aima.core.logic.fol.parsing.ast; 4 | 5 | namespace aima.core.logic.fol.parsing 6 | { 7 | /** 8 | * @author Ravi Mohan 9 | * 10 | */ 11 | public interface FOLVisitor 12 | { 13 | Object visitPredicate(Predicate p, Object arg); 14 | 15 | Object visitTermEquality(TermEquality equality, Object arg); 16 | 17 | Object visitVariable(Variable variable, Object arg); 18 | 19 | Object visitConstant(Constant constant, Object arg); 20 | 21 | Object visitFunction(Function function, Object arg); 22 | 23 | Object visitNotSentence(NotSentence sentence, Object arg); 24 | 25 | Object visitConnectedSentence(ConnectedSentence sentence, Object arg); 26 | 27 | Object visitQuantifiedSentence(QuantifiedSentence sentence, 28 | Object arg); 29 | } 30 | } -------------------------------------------------------------------------------- /aima-csharp/environment/cellworld/CellWorldFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace aima.core.environment.cellworld 5 | { 6 | /** 7 | * 8 | * @author Ciaran O'Reilly 9 | * 10 | */ 11 | public class CellWorldFactory 12 | { 13 | /** 14 | * Create the cell world as defined in Figure 17.1 in AIMA3e. (a) A simple 4 15 | * x 3 environment that presents the agent with a sequential decision 16 | * problem. 17 | * 18 | * @return a cell world representation of Fig 17.1 in AIMA3e. 19 | */ 20 | public static CellWorld createCellWorldForFig17_1() 21 | { 22 | CellWorld cw = new CellWorld(4, 3, -0.04); 23 | 24 | cw.removeCell(2, 2); 25 | 26 | cw.getCellAt(4, 3).setContent(1.0); 27 | cw.getCellAt(4, 2).setContent(-1.0); 28 | 29 | return cw; 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /aima-csharp/agent/impl/aprog/simplerule/Condition.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using aima.core.agent.impl; 4 | 5 | namespace aima.core.agent.impl.aprog.simplerule 6 | { 7 | /** 8 | * Base abstract class for describing conditions. 9 | * 10 | * @author Ciaran O'Reilly 11 | * 12 | */ 13 | public abstract class Condition 14 | { 15 | public abstract bool evaluate(ObjectWithDynamicAttributes p); 16 | 17 | public bool Equals(Object o) 18 | { 19 | if(o == null || !(o is Condition)) 20 | { 21 | return base.Equals(o); 22 | } 23 | return (ToString().Equals(((Condition)o).ToString())); 24 | } 25 | 26 | public override int GetHashCode() 27 | { 28 | return ToString().GetHashCode(); 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /aima-csharp/search/framework/problem/ActionsFunction.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using aima.core.agent; 3 | 4 | namespace aima.core.search.framework.problem 5 | { 6 | /** 7 | * Artificial Intelligence A Modern Approach (3rd Edition): page 67.
8 | *
9 | * Given a particular state s, ACTIONS(s) returns the set of actions that can be 10 | * executed in s. We say that each of these actions is applicable in s. 11 | * 12 | * @author Ciaran O'Reilly 13 | * 14 | */ 15 | public interface ActionsFunction 16 | { 17 | /** 18 | * Given a particular state s, returns the set of actions that can be 19 | * executed in s. 20 | * 21 | * @param s 22 | * a particular state. 23 | * @return the set of actions that can be executed in s. 24 | */ 25 | HashSet actions(System.Object s); 26 | } 27 | } -------------------------------------------------------------------------------- /aima-csharp/agent/impl/SimpleEnvironmentView.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using aima.core.agent; 4 | 5 | namespace aima.core.agent.impl 6 | { 7 | /** 8 | * Simple environment view which uses the standard output stream to inform about 9 | * relevant events. 10 | * 11 | * @author Ruediger Lunde 12 | */ 13 | public class SimpleEnvironmentView : EnvironmentView 14 | { 15 | public void agentActed(Agent agent, Action action, EnvironmentState resultingState) 16 | { 17 | System.Console.WriteLine("Agent acted: " + action.ToString()); 18 | } 19 | 20 | public void agentAdded(Agent agent, EnvironmentState resultingState) 21 | { 22 | System.Console.WriteLine("Agent added."); 23 | } 24 | 25 | public void notify(string msg) 26 | { 27 | System.Console.WriteLine("Message: " + msg); 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /aima-csharp/agent/AgentProgram.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace aima.core.agent 5 | { 6 | /** 7 | * Artificial Intelligence A Modern Approach (3rd Edition): pg 35.
8 | * An agent's behavior is described by the 'agent function' that maps any given 9 | * percept sequence to an action. Internally, the agent function for an 10 | * artificial agent will be implemented by an agent program. 11 | * 12 | * @author Ravi Mohan 13 | * @author Ciaran O'Reilly 14 | */ 15 | public interface AgentProgram 16 | { 17 | /** 18 | * The Agent's program, which maps any given percept sequences to an action. 19 | * 20 | * @param percept 21 | * The current percept of a sequence perceived by the Agent. 22 | * @return the Action to be taken in response to the currently perceived 23 | * percept. 24 | */ 25 | Action execute(Percept percept); 26 | } 27 | } -------------------------------------------------------------------------------- /aima-csharp/util/MockRandomizer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace aima.core.util 4 | { 5 | /** 6 | * Mock implementation of the Randomizer interface so that the set of Random 7 | * numbers returned are in fact predefined. 8 | * 9 | * @author Ravi Mohan 10 | * 11 | */ 12 | public class MockRandomizer : Randomizer 13 | { 14 | private double[] values; 15 | private int index; 16 | 17 | /** 18 | * 19 | * @param values 20 | * the set of predetermined random values to loop over. 21 | */ 22 | public MockRandomizer(double[] values) 23 | { 24 | this.values = new double[values.Length]; 25 | System.Array.Copy(values, 0, this.values, 0, values.Length); 26 | this.index = 0; 27 | } 28 | 29 | // START-Randomizer 30 | public double nextDouble() 31 | { 32 | if(index == values.Length) 33 | { 34 | index = 0; 35 | } 36 | return values[index++]; 37 | } 38 | 39 | //END-Randomizer 40 | } 41 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/inference/proof/Proof.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using aima.core.logic.fol.parsing.ast; 4 | 5 | namespace aima.core.logic.fol.inference.proof 6 | { 7 | /** 8 | * @author Ciaran O'Reilly 9 | * 10 | */ 11 | public interface Proof 12 | { 13 | /** 14 | * 15 | * @return A list of proof steps that show how an answer was derived. 16 | */ 17 | List getSteps(); 18 | 19 | /** 20 | * 21 | * @return a Map of bindings for any variables that were in the original 22 | * query. Will be an empty Map if no variables existed in the 23 | * original query. 24 | */ 25 | Dictionary getAnswerBindings(); 26 | 27 | /** 28 | * 29 | * @param updatedBindings 30 | * allows for the bindings to be renamed. Note: should not be 31 | * used for any other reason. 32 | */ 33 | void replaceAnswerBindings(Dictionary updatedBindings); 34 | } 35 | } -------------------------------------------------------------------------------- /aima-csharp/agent/impl/aprog/simplerule/NOTCondition.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Diagnostics; 5 | using aima.core.agent.impl; 6 | 7 | namespace aima.core.agent.impl.aprog.simplerule 8 | { 9 | /** 10 | * Implementation of a NOT condition. 11 | * 12 | * @author Ciaran O'Reilly 13 | * 14 | */ 15 | public class NOTCondition : Condition 16 | { 17 | private Condition con; 18 | 19 | public NOTCondition(Condition c) 20 | { 21 | Debug.Assert(null != con); 22 | 23 | con = c; 24 | } 25 | 26 | public override bool evaluate(ObjectWithDynamicAttributes p) 27 | { 28 | return (!con.evaluate(p)); 29 | } 30 | 31 | public override String ToString() 32 | { 33 | StringBuilder sb = new StringBuilder(); 34 | 35 | return sb.Append("![").Append(con).Append("]").ToString(); 36 | } 37 | } 38 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/inference/proof/ProofStepGoal.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Collections.ObjectModel; 4 | using System.Linq; 5 | 6 | namespace aima.core.logic.fol.inference.proof 7 | { 8 | /** 9 | * @author Ciaran O'Reilly 10 | * 11 | */ 12 | public class ProofStepGoal : AbstractProofStep 13 | { 14 | private static readonly List _noPredecessors = new List(); 15 | 16 | private Object proof = ""; 17 | 18 | public ProofStepGoal(Object proof) 19 | { 20 | this.proof = proof; 21 | } 22 | 23 | // START-ProofStep 24 | 25 | public override List getPredecessorSteps() 26 | { 27 | return new ReadOnlyCollection(_noPredecessors).ToList(); 28 | } 29 | 30 | public override String getProof() 31 | { 32 | return proof.ToString(); 33 | } 34 | 35 | public override String getJustification() 36 | { 37 | return "Goal"; 38 | } 39 | 40 | // END-ProofStep 41 | } 42 | } -------------------------------------------------------------------------------- /aima-csharp/search/Local/Scheduler.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace aima.core.search.local 5 | { 6 | /** 7 | * @author Ravi Mohan 8 | * 9 | */ 10 | public class Scheduler 11 | { 12 | private int k, limit; 13 | private double lam; 14 | 15 | public Scheduler(int k, double lam, int limit) 16 | { 17 | this.k = k; 18 | this.lam = lam; 19 | this.limit = limit; 20 | } 21 | 22 | public Scheduler() 23 | { 24 | this.k = 20; 25 | this.lam = 0.045; 26 | this.limit = 100; 27 | } 28 | 29 | public double getTemp(int t) 30 | { 31 | if (t < limit) 32 | { 33 | double res = k * Math.Exp((-1) * lam * t); 34 | return res; 35 | } 36 | else 37 | { 38 | return 0.0; 39 | } 40 | } 41 | } 42 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/inference/proof/ProofStepPremise.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Collections.ObjectModel; 4 | using System.Linq; 5 | 6 | namespace aima.core.logic.fol.inference.proof 7 | { 8 | /** 9 | * @author Ciaran O'Reilly 10 | * 11 | */ 12 | public class ProofStepPremise : AbstractProofStep 13 | { 14 | private static readonly List _noPredecessors = new List(); 15 | 16 | private Object proof = ""; 17 | 18 | public ProofStepPremise(Object proof) 19 | { 20 | this.proof = proof; 21 | } 22 | 23 | // START-ProofStep 24 | 25 | public override List getPredecessorSteps() 26 | { 27 | return new ReadOnlyCollection(_noPredecessors).ToList(); 28 | } 29 | 30 | public override String getProof() 31 | { 32 | return proof.ToString(); 33 | } 34 | 35 | public override String getJustification() 36 | { 37 | return "Premise"; 38 | } 39 | 40 | // END-ProofStep 41 | } 42 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/Connectors.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace aima.core.logic.fol 4 | { 5 | /** 6 | * @author Ravi Mohan 7 | * 8 | */ 9 | public class Connectors 10 | { 11 | public static readonly String AND = "AND"; 12 | 13 | public static readonly String OR = "OR"; 14 | 15 | public static readonly String NOT = "NOT"; 16 | 17 | public static readonly String IMPLIES = "=>"; 18 | 19 | public static readonly String BICOND = "<=>"; 20 | 21 | public static bool isAND(String connector) 22 | { 23 | return AND.Equals(connector); 24 | } 25 | 26 | public static bool isOR(String connector) 27 | { 28 | return OR.Equals(connector); 29 | } 30 | 31 | public static bool isNOT(String connector) 32 | { 33 | return NOT.Equals(connector); 34 | } 35 | 36 | public static bool isIMPLIES(String connector) 37 | { 38 | return IMPLIES.Equals(connector); 39 | } 40 | 41 | public static bool isBICOND(String connector) 42 | { 43 | return BICOND.Equals(connector); 44 | } 45 | } 46 | } -------------------------------------------------------------------------------- /aima-csharp.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 14 4 | VisualStudioVersion = 14.0.25123.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "aima-csharp", "aima-csharp\aima-csharp.csproj", "{125F53D5-1CCF-4DAF-82FE-4324686CF417}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {125F53D5-1CCF-4DAF-82FE-4324686CF417}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {125F53D5-1CCF-4DAF-82FE-4324686CF417}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {125F53D5-1CCF-4DAF-82FE-4324686CF417}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {125F53D5-1CCF-4DAF-82FE-4324686CF417}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | EndGlobal 23 | -------------------------------------------------------------------------------- /aima-csharp/search/framework/Search.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using aima.core.agent; 3 | using aima.core.search.framework.problem; 4 | 5 | namespace aima.core.search.framework 6 | { 7 | 8 | /** 9 | * @author Ravi Mohan 10 | * @author Mike Stampone 11 | */ 12 | public interface Search 13 | { 14 | /** 15 | * Returns a list of actions to the goal if the goal was found, a list 16 | * containing a single NoOp Action if already at the goal, or an empty list 17 | * if the goal could not be found. 18 | * 19 | * @param p 20 | * the search problem 21 | * 22 | * @return a list of actions to the goal if the goal was found, a list 23 | * containing a single NoOp Action if already at the goal, or an 24 | * empty list if the goal could not be found. 25 | */ 26 | List search(Problem p); 27 | 28 | /** 29 | * Returns all the metrics of the search. 30 | * 31 | * @return all the metrics of the search. 32 | */ 33 | Metrics getMetrics(); 34 | } 35 | } -------------------------------------------------------------------------------- /aima-csharp/agent/impl/DynamicAction.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using aima.core.agent; 4 | 5 | namespace aima.core.agent.impl 6 | { 7 | /** 8 | * @author Ciaran O'Reilly 9 | */ 10 | public class DynamicAction : ObjectWithDynamicAttributes, 11 | Action 12 | { 13 | public const String ATTRIBUTE_NAME = "name"; 14 | 15 | public DynamicAction(String name) 16 | { 17 | this.setAttribute(ATTRIBUTE_NAME, name); 18 | } 19 | 20 | /** 21 | * Returns the value of the name attribute. 22 | * 23 | * @return the value of the name attribute. 24 | */ 25 | public String getName() 26 | { 27 | return (System.String)getAttribute(ATTRIBUTE_NAME); 28 | } 29 | 30 | // START-Action 31 | public bool isNoOp() 32 | { 33 | return false; 34 | } 35 | 36 | // END-Action 37 | public String describeType() 38 | { 39 | return this.GetType().Name; 40 | } 41 | } 42 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/inference/proof/ProofStepFoChAlreadyAFact.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Collections.ObjectModel; 5 | using aima.core.logic.fol.kb.data; 6 | 7 | namespace aima.core.logic.fol.inference.proof 8 | { 9 | /** 10 | * @author Ciaran O'Reilly 11 | * 12 | */ 13 | public class ProofStepFoChAlreadyAFact : AbstractProofStep 14 | { 15 | private readonly List _noPredecessors = new List(); 16 | private Literal fact = null; 17 | 18 | public ProofStepFoChAlreadyAFact(Literal fact) 19 | { 20 | this.fact = fact; 21 | } 22 | 23 | // START-ProofStep 24 | 25 | public override List getPredecessorSteps() 26 | { 27 | return new ReadOnlyCollection(_noPredecessors).ToList(); 28 | } 29 | 30 | public override String getProof() 31 | { 32 | return fact.ToString(); 33 | } 34 | 35 | public override String getJustification() 36 | { 37 | return "Already a known fact in the KB."; 38 | } 39 | 40 | // END-ProofStep 41 | } 42 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/inference/proof/ProofStepRenaming.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Collections.ObjectModel; 4 | using System.Linq; 5 | 6 | namespace aima.core.logic.fol.inference.proof 7 | { 8 | /** 9 | * @author Ciaran O'Reilly 10 | * 11 | */ 12 | public class ProofStepRenaming : AbstractProofStep 13 | { 14 | private List predecessors = new List(); 15 | private Object proof = ""; 16 | 17 | public ProofStepRenaming(Object proof, ProofStep predecessor) 18 | { 19 | this.proof = proof; 20 | this.predecessors.Add(predecessor); 21 | } 22 | 23 | // START-ProofStep 24 | 25 | public override List getPredecessorSteps() 26 | { 27 | return new ReadOnlyCollection(predecessors).ToList(); 28 | } 29 | 30 | public override String getProof() 31 | { 32 | return proof.ToString(); 33 | } 34 | 35 | public override String getJustification() 36 | { 37 | return "Renaming of " + predecessors[0].getStepNumber(); 38 | } 39 | 40 | // END-ProofStep 41 | } 42 | } -------------------------------------------------------------------------------- /aima-csharp/search/framework/problem/StepCostFunction.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using aima.core.agent; 3 | namespace aima.core.search.framework.problem 4 | { 5 | /** 6 | * Artificial Intelligence A Modern Approach (3rd Edition): page 68.
7 | *
8 | * The step cost of taking action a in state s to reach state s' is 9 | * denoted by c(s, a, s'). 10 | * 11 | * @author Ravi Mohan 12 | * @author Ciaran O'Reilly 13 | */ 14 | public interface StepCostFunction 15 | { 16 | /** 17 | * Calculate the step cost of taking action a in state s to reach state s'. 18 | * 19 | * @param s 20 | * the state from which action a is to be performed. 21 | * @param a 22 | * the action to be taken. 23 | * 24 | * @param sDelta 25 | * the state reached by taking the action. 26 | * @return the cost of taking action a in state s to reach state s'. 27 | */ 28 | double c(System.Object s, Action a, System.Object sDelta); 29 | } 30 | } -------------------------------------------------------------------------------- /aima-csharp/environment/map/AdaptableHeuristicFunction.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using aima.core.search.framework; 4 | 5 | namespace aima.core.environment.map 6 | { 7 | /** 8 | * This class extends heuristic functions in two ways: It maintains a goal and a 9 | * map to estimate distance to goal for states in route planning problems, and 10 | * it provides a method to adapt to different goals. 11 | * 12 | * @author Ruediger Lunde 13 | */ 14 | public abstract class AdaptableHeuristicFunction 15 | { 16 | /** The Current Goal. */ 17 | protected Object goal; 18 | /** The map to be used for distance to goal estimates. */ 19 | protected Map map; 20 | 21 | /** 22 | * Modifies goal and map information and returns the modified heuristic 23 | * function. 24 | */ 25 | public AdaptableHeuristicFunction adaptToGoal(Object goal, Map map) 26 | { 27 | this.goal = goal; 28 | this.map = map; 29 | return this; 30 | } 31 | 32 | // when subclassing: Don't forget to implement the most important method 33 | // public double h(Object state) 34 | } 35 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 aimacode 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /aima-csharp/environment/eightpuzzle/BidirectionalEightPuzzleProblem.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using aima.core.search.framework.problem; 3 | 4 | namespace aima.core.environment.eightpuzzle 5 | { 6 | /** 7 | * @author Ruediger Lunde 8 | * 9 | */ 10 | public class BidirectionalEightPuzzleProblem : Problem, BidirectionalProblem 11 | { 12 | Problem reverseProblem; 13 | 14 | public BidirectionalEightPuzzleProblem(EightPuzzleBoard initialState): base(initialState, EightPuzzleFunctionFactory.getActionsFunction(), 15 | EightPuzzleFunctionFactory.getResultFunction(), 16 | new DefaultGoalTest(new EightPuzzleBoard(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }))) 17 | { 18 | reverseProblem = new Problem(new EightPuzzleBoard(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }), 19 | EightPuzzleFunctionFactory.getActionsFunction(), EightPuzzleFunctionFactory.getResultFunction(), 20 | new DefaultGoalTest(initialState)); 21 | } 22 | 23 | public Problem getOriginalProblem() 24 | { 25 | return this; 26 | } 27 | 28 | public Problem getReverseProblem() 29 | { 30 | return reverseProblem; 31 | } 32 | } 33 | } -------------------------------------------------------------------------------- /aima-csharp/agent/impl/aprog/simplerule/EQUALCondition.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Diagnostics; 5 | using aima.core.agent.impl; 6 | 7 | namespace aima.core.agent.impl.aprog.simplerule 8 | { 9 | /** 10 | * Implementation of an EQUALity condition. 11 | * 12 | * @author Ciaran O'Reilly 13 | * 14 | */ 15 | public class EQUALCondition : Condition 16 | { 17 | private Object key; 18 | 19 | private Object value; 20 | 21 | public EQUALCondition(Object k, Object val) 22 | { 23 | Debug.Assert(null != key); 24 | Debug.Assert(null != value); 25 | 26 | key = k; 27 | value = val; 28 | } 29 | 30 | public override bool evaluate(ObjectWithDynamicAttributes p) 31 | { 32 | return value.Equals(p.getAttribute(key)); 33 | } 34 | 35 | public override String ToString() 36 | { 37 | StringBuilder sb = new StringBuilder(); 38 | 39 | return sb.Append(key).Append("==").Append(value).ToString(); 40 | } 41 | } 42 | } -------------------------------------------------------------------------------- /aima-csharp/search/Local/FitnessFunction.cs: -------------------------------------------------------------------------------- 1 | namespace aima.core.search.local 2 | { 3 | /** 4 | * Artificial Intelligence A Modern Approach (3rd Edition): page 127.
5 | *
6 | * Each state is rated by the objective function, or (in Genetic Algorithm 7 | * terminology) the fitness function. A fitness function should return higher 8 | * values for better states. 9 | *
10 | * Here, we assume that all values are greater or equal to zero. 11 | * 12 | * @author Ciaran O'Reilly 13 | * 14 | * @param 15 | * the type of the alphabet used in the representation of the 16 | * individuals in the population (this is to provide flexibility in 17 | * terms of how a problem can be encoded). 18 | */ 19 | public interface FitnessFunction 20 | { 21 | /** 22 | * 23 | * @param individual 24 | * the individual whose fitness is to be accessed. 25 | * @return the individual's fitness value (the higher the better). 26 | */ 27 | double apply(Individual individual); 28 | } 29 | } -------------------------------------------------------------------------------- /aima-csharp/search/framework/problem/ResultFunction.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using aima.core.agent; 3 | 4 | namespace aima.core.search.framework.problem 5 | { 6 | /** 7 | * Artificial Intelligence A Modern Approach (3rd Edition): page 67.
8 | *
9 | * A description of what each action does; the formal name for this is the 10 | * transition model, specified by a function RESULT(s, a) that returns the state 11 | * that results from doing action a in state s. We also use the term successor 12 | * to refer to any state reachable from a given state by a single action. 13 | * 14 | * @author Ravi Mohan 15 | * @author Ciaran O'Reilly 16 | */ 17 | public interface ResultFunction 18 | { 19 | /** 20 | * Returns the state that results from doing action a in state s 21 | * 22 | * @param s 23 | * a particular state. 24 | * @param a 25 | * an action to be performed in state s. 26 | * @return the state that results from doing action a in state s. 27 | */ 28 | System.Object result(System.Object s, Action a); 29 | } 30 | } -------------------------------------------------------------------------------- /aima-csharp/environment/map/DynAttributeNames.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace aima.core.environment.map 4 | { 5 | /** 6 | * The AIMA framework uses dynamic attributes to make implementations of agents 7 | * and environments completely independent of each other. The disadvantage of 8 | * this concept is, that it's error-prone. This set of constants is designed to 9 | * make information exchange more reliable for map agents. Two kinds of 10 | * attributes are distinguished. Percept attributes are attached to percepts. 11 | * They are generated by the environment and read by by the agent. 12 | * EnvironmentState attributes are attached to the EnvironmentState of the 13 | * Environment. 14 | * 15 | * @author Ruediger Lunde 16 | */ 17 | public class DynAttributeNames 18 | { 19 | /** 20 | * Name of a dynamic attribute, which contains the current location of the 21 | * agent. Expected value type: String. 22 | */ 23 | public static readonly String AGENT_LOCATION = "location"; 24 | /** 25 | * Name of a dynamic attribute, which tells the agent where it is. Expected 26 | * value type: String. 27 | */ 28 | public static readonly String PERCEPT_IN = "in"; 29 | } 30 | } -------------------------------------------------------------------------------- /aima-csharp/agent/impl/aprog/simplerule/ORCondition.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Diagnostics; 5 | using aima.core.agent.impl; 6 | 7 | namespace aima.core.agent.impl.aprog.simplerule 8 | { 9 | /** 10 | * Implementation of an OR condition. 11 | * 12 | * @author Ciaran O'Reilly 13 | * 14 | */ 15 | public class ORCondition : Condition 16 | { 17 | private Condition left; 18 | 19 | private Condition right; 20 | 21 | public ORCondition(Condition leftCon, Condition rightCon) 22 | { 23 | Debug.Assert(null != leftCon); 24 | Debug.Assert(null != rightCon); 25 | 26 | left = leftCon; 27 | right = rightCon; 28 | } 29 | 30 | public override bool evaluate(ObjectWithDynamicAttributes p) 31 | { 32 | return (left.evaluate(p) || right.evaluate(p)); 33 | } 34 | 35 | public override String ToString() 36 | { 37 | StringBuilder sb = new StringBuilder(); 38 | 39 | return sb.Append("[").Append(left).Append(" || ").Append(right).Append( 40 | "]").ToString(); 41 | } 42 | } 43 | } -------------------------------------------------------------------------------- /aima-csharp/agent/impl/aprog/simplerule/ANDCondition.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Diagnostics; 5 | using aima.core.agent.impl; 6 | 7 | namespace aima.core.agent.impl.aprog.simplerule 8 | { 9 | /** 10 | * Implementation of an AND condition. 11 | * 12 | * @author Ciaran O'Reilly 13 | * 14 | */ 15 | public class ANDCondition : Condition 16 | { 17 | private Condition left; 18 | 19 | private Condition right; 20 | 21 | public ANDCondition(Condition leftCon, Condition rightCon) 22 | { 23 | Debug.Assert(null != leftCon); 24 | Debug.Assert(null != rightCon); 25 | 26 | left = leftCon; 27 | right = rightCon; 28 | } 29 | 30 | public override bool evaluate(ObjectWithDynamicAttributes p) 31 | { 32 | return (left.evaluate(p) && right.evaluate(p)); 33 | } 34 | 35 | public override String ToString() 36 | { 37 | StringBuilder sb = new StringBuilder(); 38 | 39 | return sb.Append("[").Append(left).Append(" && ").Append(right).Append( 40 | "]").ToString(); 41 | } 42 | } 43 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/inference/proof/ProofStepChainDropped.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Collections.ObjectModel; 5 | using aima.core.logic.fol.kb.data; 6 | 7 | namespace aima.core.logic.fol.inference.proof 8 | { 9 | /** 10 | * @author Ciaran O'Reilly 11 | * 12 | */ 13 | public class ProofStepChainDropped : AbstractProofStep 14 | { 15 | private List predecessors = new List(); 16 | private Chain dropped = null; 17 | private Chain droppedOff = null; 18 | 19 | public ProofStepChainDropped(Chain dropped, Chain droppedOff) 20 | { 21 | this.dropped = dropped; 22 | this.droppedOff = droppedOff; 23 | this.predecessors.Add(droppedOff.getProofStep()); 24 | } 25 | 26 | // START-ProofStep 27 | 28 | public override List getPredecessorSteps() 29 | { 30 | return new ReadOnlyCollection(predecessors).ToList(); 31 | } 32 | 33 | public override String getProof() 34 | { 35 | return dropped.ToString(); 36 | } 37 | 38 | public override String getJustification() 39 | { 40 | return "Dropped: " + droppedOff.getProofStep().getStepNumber(); 41 | } 42 | 43 | // END-ProofStep 44 | } 45 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/inference/otter/LightestClauseHeuristic.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using aima.core.logic.fol.kb.data; 4 | 5 | namespace aima.core.logic.fol.inference.otter 6 | { 7 | /** 8 | * Heuristic for selecting lightest clause from SOS. To avoid recalculating the 9 | * lightest clause on every call, the interface supports defining the initial 10 | * sos and updates to that set so that it can maintain its own internal data 11 | * structures to allow for incremental re-calculation of the lightest clause. 12 | * 13 | * @author Ciaran O'Reilly 14 | * 15 | */ 16 | public interface LightestClauseHeuristic 17 | { 18 | /** 19 | * Get the lightest clause from the SOS 20 | * 21 | * @return the lightest clause. 22 | */ 23 | Clause getLightestClause(); 24 | 25 | /** 26 | * SOS life-cycle methods allowing implementations of this interface to 27 | * incrementally update the calculation of the lightest clause as opposed to 28 | * having to recalculate each time. 29 | * 30 | * @param clauses 31 | */ 32 | void initialSOS(List clauses); 33 | 34 | void addedClauseToSOS(Clause clause); 35 | 36 | void removedClauseFromSOS(Clause clause); 37 | } 38 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/inference/proof/ProofStepChainFromClause.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Collections.ObjectModel; 5 | using aima.core.logic.fol.kb.data; 6 | 7 | namespace aima.core.logic.fol.inference.proof 8 | { 9 | /** 10 | * @author Ciaran O'Reilly 11 | * 12 | */ 13 | public class ProofStepChainFromClause : AbstractProofStep 14 | { 15 | private List predecessors = new List(); 16 | private Chain chain = null; 17 | private Clause fromClause = null; 18 | 19 | public ProofStepChainFromClause(Chain chain, Clause fromClause) 20 | { 21 | this.chain = chain; 22 | this.fromClause = fromClause; 23 | this.predecessors.Add(fromClause.getProofStep()); 24 | } 25 | 26 | // START-ProofStep 27 | 28 | public override List getPredecessorSteps() 29 | { 30 | return new ReadOnlyCollection(predecessors).ToList(); 31 | } 32 | 33 | public override String getProof() 34 | { 35 | return chain.ToString(); 36 | } 37 | 38 | public override String getJustification() 39 | { 40 | return "Chain from Clause: " 41 | + fromClause.getProofStep().getStepNumber(); 42 | } 43 | // END-ProofStep 44 | } 45 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/inference/proof/ProofStepClauseClausifySentence.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Collections.ObjectModel; 5 | using aima.core.logic.fol.kb.data; 6 | using aima.core.logic.fol.parsing.ast; 7 | 8 | namespace aima.core.logic.fol.inference.proof 9 | { 10 | /** 11 | * @author Ciaran O'Reilly 12 | * 13 | */ 14 | public class ProofStepClauseClausifySentence : AbstractProofStep 15 | { 16 | private List predecessors = new List(); 17 | private Clause clausified = null; 18 | 19 | public ProofStepClauseClausifySentence(Clause clausified, 20 | Sentence origSentence) 21 | { 22 | this.clausified = clausified; 23 | this.predecessors.Add(new ProofStepPremise(origSentence)); 24 | } 25 | 26 | // START-ProofStep 27 | 28 | public override List getPredecessorSteps() 29 | { 30 | return new ReadOnlyCollection(predecessors).ToList(); 31 | } 32 | 33 | public override String getProof() 34 | { 35 | return clausified.ToString(); 36 | } 37 | 38 | public override String getJustification() 39 | { 40 | return "Clausified " + predecessors[0].getStepNumber(); 41 | } 42 | 43 | // END-ProofStep 44 | } 45 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/kb/data/ReducedLiteral.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using aima.core.logic.fol.parsing.ast; 5 | 6 | namespace aima.core.logic.fol.kb.data 7 | { 8 | /** 9 | * @see
Reduced Literal 12 | * 13 | * @author Ciaran O'Reilly 14 | * 15 | */ 16 | public class ReducedLiteral : Literal 17 | { 18 | private String strRep = null; 19 | 20 | public ReducedLiteral(AtomicSentence atom) : base(atom) 21 | { 22 | 23 | } 24 | 25 | public ReducedLiteral(AtomicSentence atom, bool negated) : base(atom, negated) 26 | { 27 | 28 | } 29 | 30 | public override Literal newInstance(AtomicSentence atom) 31 | { 32 | return new ReducedLiteral(atom, isNegativeLiteral()); 33 | } 34 | 35 | public override String ToString() 36 | { 37 | if (null == strRep) 38 | { 39 | StringBuilder sb = new StringBuilder(); 40 | sb.Append("["); 41 | if (isNegativeLiteral()) 42 | { 43 | sb.Append("~"); 44 | } 45 | sb.Append(getAtomicSentence().ToString()); 46 | sb.Append("]"); 47 | strRep = sb.ToString(); 48 | } 49 | 50 | return strRep; 51 | } 52 | } 53 | } -------------------------------------------------------------------------------- /aima-csharp/logic/common/LexerException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace aima.core.logic.common 5 | { 6 | /** 7 | * A runtime exception to be used to describe Lexer exceptions. In particular it 8 | * provides information to help in identifying where in the input character 9 | * sequence the exception occurred. 10 | * 11 | * @author Ciaran O'Reilly 12 | * 13 | */ 14 | public class LexerException : SystemException 15 | { 16 | private static readonly long serialVersionUID = 1L; 17 | 18 | private int currentPositionInInput; 19 | 20 | public LexerException(String message, int currentPositionInInput): base(message) 21 | { 22 | this.currentPositionInInput = currentPositionInInput; 23 | } 24 | 25 | public LexerException(String message, int currentPositionInInput, 26 | Exception cause): base(message, cause) 27 | { 28 | 29 | this.currentPositionInInput = currentPositionInInput; 30 | } 31 | 32 | /** 33 | * 34 | * @return the current position in the input character stream that the lexer 35 | * was at before the exception was encountered. 36 | */ 37 | public int getCurrentPositionInInputExceptionThrown() 38 | { 39 | return currentPositionInInput; 40 | } 41 | } 42 | } -------------------------------------------------------------------------------- /aima-csharp/environment/wumpusworld/ManhattanHeuristicFunction.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using aima.core.search.framework; 4 | 5 | namespace aima.core.environment.wumpusworld 6 | { 7 | /** 8 | * Heuristic for calculating the Manhattan distance between two rooms within a Wumpus World cave. 9 | * 10 | * @author Federico Baron 11 | * @author Alessandro Daniele 12 | * @author Ciaran O'Reilly 13 | */ 14 | public class ManhattanHeuristicFunction : HeuristicFunction 15 | { 16 | List goals = new List(); 17 | 18 | public ManhattanHeuristicFunction(HashSet goals) 19 | { 20 | this.goals.AddRange(goals); 21 | } 22 | 23 | public double h(Object state) 24 | { 25 | AgentPosition pos = (AgentPosition)state; 26 | int nearestGoalDist = int.MaxValue; 27 | foreach (Room g in goals) 28 | { 29 | int tmp = evaluateManhattanDistanceOf(pos.getX(), pos.getY(), g.getX(), g.getY()); 30 | 31 | if (tmp < nearestGoalDist) 32 | { 33 | nearestGoalDist = tmp; 34 | } 35 | } 36 | return nearestGoalDist; 37 | } 38 | 39 | // PRIVATE 40 | 41 | private int evaluateManhattanDistanceOf(int x1, int y1, int x2, int y2) 42 | { 43 | return Math.Abs(x1 - x2) + Math.Abs(y1 - y2); 44 | } 45 | } 46 | } -------------------------------------------------------------------------------- /aima-csharp/agent/Agent.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | 4 | namespace aima.core.agent 5 | { 6 | /** 7 | * Artificial Intelligence A Modern Approach (3rd Edition): Figure 2.1, page 35.
8 | * 9 | * Figure 2.1 Agents interact with environments through sensors and actuators. 10 | * 11 | * @author Ravi Mohan 12 | * @author Ciaran O'Reilly 13 | */ 14 | public interface Agent : EnvironmentObject 15 | { 16 | /** 17 | * Call the Agent's program, which maps any given percept sequences to an 18 | * action. 19 | * 20 | * @param percept 21 | * The current percept of a sequence perceived by the Agent. 22 | * @return the Action to be taken in response to the currently perceived 23 | * percept. 24 | */ 25 | Action execute(Percept percept); 26 | 27 | /** 28 | * Life-cycle indicator as to the liveness of an Agent. 29 | * 30 | * @return true if the Agent is to be considered alive, false otherwise. 31 | */ 32 | bool isAlive(); 33 | 34 | /** 35 | * Set the current liveness of the Agent. 36 | * 37 | * @param alive 38 | * set to true if the Agent is to be considered alive, false 39 | * otherwise. 40 | */ 41 | void setAlive(bool alive); 42 | } 43 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/kb/data/CNF.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Text; 4 | using System.Collections.Generic; 5 | using System.Collections.ObjectModel; 6 | using System.Linq; 7 | 8 | namespace aima.core.logic.fol.kb.data 9 | { 10 | /** 11 | * Conjunctive Normal Form (CNF) : a conjunction of clauses, where each clause 12 | * is a disjunction of literals. 13 | * 14 | * @author Ciaran O'Reilly 15 | * 16 | */ 17 | public class CNF 18 | { 19 | private List conjunctionOfClauses = new List(); 20 | 21 | public CNF(List conjunctionOfClauses) 22 | { 23 | this.conjunctionOfClauses.AddRange(conjunctionOfClauses); 24 | } 25 | 26 | public int getNumberOfClauses() 27 | { 28 | return conjunctionOfClauses.Count; 29 | } 30 | 31 | public List getConjunctionOfClauses() 32 | { 33 | return new ReadOnlyCollection(conjunctionOfClauses).ToList(); 34 | } 35 | 36 | public override String ToString() 37 | { 38 | StringBuilder sb = new StringBuilder(); 39 | for (int i = 0; i < conjunctionOfClauses.Count; i++) 40 | { 41 | if (i > 0) 42 | { 43 | sb.Append(","); 44 | } 45 | sb.Append(conjunctionOfClauses[i].ToString()); 46 | } 47 | 48 | return sb.ToString(); 49 | } 50 | } 51 | } -------------------------------------------------------------------------------- /aima-csharp/environment/map/MapEnvironmentState.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using aima.core.agent; 3 | using aima.core.util; 4 | 5 | namespace aima.core.environment.map 6 | { 7 | /** 8 | * @author Ciaran O'Reilly 9 | * 10 | */ 11 | public class MapEnvironmentState : EnvironmentState 12 | { 13 | private Dictionary> agentLocationAndTravelDistance = new Dictionary>(); 14 | 15 | public MapEnvironmentState() 16 | { 17 | 18 | } 19 | 20 | public string getAgentLocation(Agent a) 21 | { 22 | Pair locAndTDistance = agentLocationAndTravelDistance[a]; 23 | if (null == locAndTDistance) 24 | { 25 | return null; 26 | } 27 | return locAndTDistance.getFirst(); 28 | } 29 | 30 | public double getAgentTravelDistance(Agent a) 31 | { 32 | Pair locAndTDistance = agentLocationAndTravelDistance[a]; 33 | if (null == locAndTDistance) 34 | { 35 | return double.MinValue; 36 | } 37 | return locAndTDistance.getSecond(); 38 | } 39 | 40 | public void setAgentLocationAndTravelDistance(Agent a, string location, 41 | double travelDistance) 42 | { 43 | agentLocationAndTravelDistance.Add(a, new Pair( 44 | location, travelDistance)); 45 | } 46 | } 47 | } -------------------------------------------------------------------------------- /aima-csharp/agent/impl/AbstractAgent.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using aima.core.agent; 3 | 4 | namespace aima.core.agent.impl 5 | { 6 | /** 7 | * @author Ravi Mohan 8 | * @author Ciaran O'Reilly 9 | * @author Mike Stampone 10 | */ 11 | public abstract class AbstractAgent : Agent 12 | { 13 | protected AgentProgram program; 14 | private bool alive = true; 15 | 16 | public AbstractAgent() 17 | { 18 | 19 | } 20 | 21 | /** 22 | * Constructs an Agent with the specified AgentProgram. 23 | * 24 | * @param aProgram 25 | * the Agent's program, which maps any given percept sequences to 26 | * an action. 27 | */ 28 | public AbstractAgent(AgentProgram aProgram) 29 | { 30 | program = aProgram; 31 | } 32 | 33 | //START-Agent 34 | public virtual Action execute(Percept p) 35 | { 36 | if(null != program) 37 | { 38 | return program.execute(p); 39 | } 40 | return NoOpAction.NO_OP; 41 | } 42 | 43 | public bool isAlive() 44 | { 45 | return alive; 46 | } 47 | 48 | public void setAlive(bool alive) 49 | { 50 | this.alive = alive; 51 | } 52 | 53 | //END-Agent 54 | } 55 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/inference/proof/ProofStepChainContrapositive.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Collections.ObjectModel; 5 | using aima.core.logic.fol.kb.data; 6 | 7 | namespace aima.core.logic.fol.inference.proof 8 | { 9 | /** 10 | * @author Ciaran O'Reilly 11 | * 12 | */ 13 | public class ProofStepChainContrapositive : AbstractProofStep 14 | { 15 | private List predecessors = new List(); 16 | private Chain contrapositive = null; 17 | private Chain contrapositiveOf = null; 18 | 19 | public ProofStepChainContrapositive(Chain contrapositive, 20 | Chain contrapositiveOf) 21 | { 22 | this.contrapositive = contrapositive; 23 | this.contrapositiveOf = contrapositiveOf; 24 | this.predecessors.Add(contrapositiveOf.getProofStep()); 25 | } 26 | 27 | // START-ProofStep 28 | 29 | public override List getPredecessorSteps() 30 | { 31 | return new ReadOnlyCollection(predecessors).ToList(); 32 | } 33 | 34 | public override String getProof() 35 | { 36 | return contrapositive.ToString(); 37 | } 38 | 39 | public override String getJustification() 40 | { 41 | return "Contrapositive: " 42 | + contrapositiveOf.getProofStep().getStepNumber(); 43 | } 44 | 45 | // END-ProofStep 46 | } 47 | } -------------------------------------------------------------------------------- /aima-csharp/environment/map/MapStepCostFunction.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using aima.core.agent; 3 | using aima.core.search.framework.problem; 4 | 5 | namespace aima.core.environment.map 6 | { 7 | /** 8 | * Implementation of StepCostFunction interface that uses the distance between 9 | * locations to calculate the cost in addition to a constant cost, so that it 10 | * may be used in conjunction with a Uniform-cost search. 11 | * 12 | * @author Ciaran O'Reilly 13 | * 14 | */ 15 | public class MapStepCostFunction : StepCostFunction 16 | { 17 | private Map map = null; 18 | 19 | // Used by Uniform-cost search to ensure every step is greater than or equal 20 | // to some small positive constant 21 | private static double constantCost = 1.0; 22 | 23 | public MapStepCostFunction(Map map) 24 | { 25 | this.map = map; 26 | } 27 | 28 | // 29 | // START-StepCostFunction 30 | public double c(object fromCurrentState, Action action, object toNextState) 31 | { 32 | 33 | string fromLoc = fromCurrentState.ToString(); 34 | string toLoc = toNextState.ToString(); 35 | 36 | double distance = map.getDistance(fromLoc, toLoc); 37 | 38 | if (distance == null || distance <= 0) 39 | { 40 | return constantCost; 41 | } 42 | 43 | return distance; 44 | } 45 | 46 | // END-StepCostFunction 47 | } 48 | } -------------------------------------------------------------------------------- /aima-csharp/environment/map/Map.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using aima.core.util; 4 | 5 | namespace aima.core.environment.map 6 | { 7 | /** 8 | * Provides a general interface for maps. 9 | * 10 | * @author Ruediger Lunde 11 | */ 12 | public interface Map 13 | { 14 | /** Returns a list of all locations. */ 15 | List getLocations(); 16 | 17 | /** 18 | * Answers to the question: Where can I get, following one of the 19 | * connections starting at the specified location? 20 | */ 21 | List getPossibleNextLocations(String location); 22 | 23 | /** 24 | * Answers to the question: From where can I reach a specified location, 25 | * following one of the map connections? 26 | */ 27 | List getPossiblePrevLocations(String location); 28 | 29 | /** 30 | * Returns the travel distance between the two specified locations if they 31 | * are linked by a connection and null otherwise. 32 | */ 33 | Double getDistance(String fromLocation, String toLocation); 34 | 35 | /** 36 | * Returns the position of the specified location. The position is 37 | * represented by two coordinates, e.g. latitude and longitude values. 38 | */ 39 | Point2D getPosition(String loc); 40 | 41 | /** 42 | * Returns a location which is selected by random. 43 | */ 44 | String randomlyGenerateDestination(); 45 | } 46 | } -------------------------------------------------------------------------------- /aima-csharp/environment/map/BidirectionalMapProblem.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using aima.core.search.framework.problem; 4 | 5 | namespace aima.core.environment.map 6 | { 7 | /** 8 | * @author Ciaran O'Reilly 9 | * 10 | */ 11 | public class BidirectionalMapProblem : Problem, BidirectionalProblem 12 | { 13 | Map map; 14 | 15 | Problem reverseProblem; 16 | 17 | public BidirectionalMapProblem(Map map, String initialState, String goalState): this(map, initialState, goalState, new DefaultGoalTest(goalState)) 18 | { 19 | 20 | } 21 | 22 | public BidirectionalMapProblem(Map map, String initialState, String goalState, GoalTest goalTest) : base(initialState, MapFunctionFactory.getActionsFunction(map), MapFunctionFactory.getResultFunction(), 23 | goalTest, new MapStepCostFunction(map)) 24 | { 25 | this.map = map; 26 | 27 | reverseProblem = new Problem(goalState, MapFunctionFactory.getReverseActionsFunction(map), 28 | MapFunctionFactory.getResultFunction(), new DefaultGoalTest(initialState), 29 | new MapStepCostFunction(map)); 30 | } 31 | 32 | // START Interface BidrectionalProblem 33 | public Problem getOriginalProblem() 34 | { 35 | return this; 36 | } 37 | 38 | public Problem getReverseProblem() 39 | { 40 | return reverseProblem; 41 | } 42 | // END Interface BirectionalProblem 43 | } 44 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/StandardizeApartResult.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using aima.core.logic.fol.parsing.ast; 4 | 5 | namespace aima.core.logic.fol 6 | { 7 | /** 8 | * @author Ciaran O'Reilly 9 | * 10 | */ 11 | public class StandardizeApartResult 12 | { 13 | private Sentence originalSentence = null; 14 | private Sentence standardized = null; 15 | private Dictionary forwardSubstitution = null; 16 | private Dictionary reverseSubstitution = null; 17 | 18 | public StandardizeApartResult(Sentence originalSentence, 19 | Sentence standardized, Dictionary forwardSubstitution, 20 | Dictionary reverseSubstitution) 21 | { 22 | this.originalSentence = originalSentence; 23 | this.standardized = standardized; 24 | this.forwardSubstitution = forwardSubstitution; 25 | this.reverseSubstitution = reverseSubstitution; 26 | } 27 | 28 | public Sentence getOriginalSentence() 29 | { 30 | return originalSentence; 31 | } 32 | 33 | public Sentence getStandardized() 34 | { 35 | return standardized; 36 | } 37 | 38 | public Dictionary getForwardSubstitution() 39 | { 40 | return forwardSubstitution; 41 | } 42 | 43 | public Dictionary getReverseSubstitution() 44 | { 45 | return reverseSubstitution; 46 | } 47 | } 48 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/inference/otter/defaultimpl/DefaultClauseSimplifier.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using aima.core.logic.fol.inference; 5 | using aima.core.logic.fol.inference.otter; 6 | using aima.core.logic.fol.kb.data; 7 | using aima.core.logic.fol.parsing.ast; 8 | 9 | namespace aima.core.logic.fol.inference.otter.defaultimpl 10 | { 11 | /** 12 | * @author Ciaran O'Reilly 13 | * 14 | */ 15 | public class DefaultClauseSimplifier : ClauseSimplifier 16 | { 17 | private Demodulation demodulation = new Demodulation(); 18 | private List rewrites = new List(); 19 | 20 | public DefaultClauseSimplifier() 21 | { 22 | 23 | } 24 | 25 | public DefaultClauseSimplifier(List rewrites) 26 | { 27 | this.rewrites.AddRange(rewrites); 28 | } 29 | 30 | // START-ClauseSimplifier 31 | 32 | public Clause simplify(Clause c) 33 | { 34 | Clause simplified = c; 35 | 36 | // Apply each of the rewrite rules to 37 | // the clause 38 | foreach (TermEquality te in rewrites) 39 | { 40 | Clause dc = simplified; 41 | // Keep applying the rewrite as many times as it 42 | // can be applied before moving on to the next one. 43 | while (null != (dc = demodulation.apply(te, dc))) 44 | { 45 | simplified = dc; 46 | } 47 | } 48 | 49 | return simplified; 50 | } 51 | 52 | // END-ClauseSimplifier 53 | } 54 | } -------------------------------------------------------------------------------- /aima-csharp/search/framework/SolutionChecker.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using aima.core.agent; 3 | using aima.core.search.framework.problem; 4 | 5 | namespace aima.core.search.framework 6 | { 7 | /** 8 | * A specialization of the GoalTest interface so that it is possible to check 9 | * the solution once a Goal has been identified to determine if it is 10 | * acceptable. This allows you to continue searching for alternative solutions 11 | * without having to restart the search. 12 | * 13 | * However, care needs to be taken when doing this as it does not always make 14 | * sense to continue with a search once an initial goal is found, for example if 15 | * using a heuristic targeted at a single goal. 16 | * 17 | * @author Ciaran O'Reilly 18 | */ 19 | public interface SolutionChecker : GoalTest 20 | { 21 | /** 22 | * This method is only called if GoalTest.isGoalState() returns true. 23 | * 24 | * @param actions 25 | * the list of actions to get to the goal state. 26 | * 27 | * @param goal 28 | * the goal the list of actions will reach. 29 | * 30 | * @return true if the solution is acceptable, false otherwise, which 31 | * indicates the search should be continued. 32 | */ 33 | bool isAcceptableSolution(List actions, System.Object goal); 34 | } 35 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/inference/proof/ProofStepClauseDemodulation.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Collections.ObjectModel; 5 | using aima.core.logic.fol.kb.data; 6 | using aima.core.logic.fol.parsing.ast; 7 | 8 | namespace aima.core.logic.fol.inference.proof 9 | { 10 | /** 11 | * @author Ciaran O'Reilly 12 | * 13 | */ 14 | public class ProofStepClauseDemodulation : AbstractProofStep 15 | { 16 | private List predecessors = new List(); 17 | private Clause demodulated = null; 18 | private Clause origClause = null; 19 | private TermEquality assertion = null; 20 | 21 | public ProofStepClauseDemodulation(Clause demodulated, Clause origClause, 22 | TermEquality assertion) 23 | { 24 | this.demodulated = demodulated; 25 | this.origClause = origClause; 26 | this.assertion = assertion; 27 | this.predecessors.Add(origClause.getProofStep()); 28 | } 29 | 30 | // START-ProofStep 31 | 32 | public override List getPredecessorSteps() 33 | { 34 | return new ReadOnlyCollection(predecessors).ToList(); 35 | } 36 | 37 | public override String getProof() 38 | { 39 | return demodulated.ToString(); 40 | } 41 | 42 | public override String getJustification() 43 | { 44 | return "Demodulation: " + origClause.getProofStep().getStepNumber() 45 | + ", [" + assertion + "]"; 46 | } 47 | 48 | // END-ProofStep 49 | } 50 | } -------------------------------------------------------------------------------- /aima-csharp/logic/common/ParserException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace aima.core.logic.common 5 | { 6 | /** 7 | * A runtime exception to be used to describe Parser exceptions. In particular 8 | * it provides information to help in identifying which tokens proved 9 | * problematic in the parse. 10 | * 11 | * @author Ciaran O'Reilly 12 | * 13 | */ 14 | public class ParserException : SystemException 15 | { 16 | private static readonly long serialVersionUID = 1L; 17 | 18 | private List problematicTokens = new List(); 19 | 20 | public ParserException(String message, params Token[] problematicTokens): base(message) 21 | { 22 | if (problematicTokens != null) 23 | { 24 | foreach (Token pt in problematicTokens) 25 | { 26 | this.problematicTokens.Add(pt); 27 | } 28 | } 29 | } 30 | 31 | public ParserException(String message, Exception cause, params Token[] problematicTokens): base(message, cause) 32 | { 33 | if (problematicTokens != null) 34 | { 35 | foreach (Token pt in problematicTokens) 36 | { 37 | this.problematicTokens.Add(pt); 38 | } 39 | } 40 | } 41 | 42 | /** 43 | * 44 | * @return a list of 0 or more tokens from the input stream that are 45 | * believed to have contributed to the parse exception. 46 | */ 47 | public List getProblematicTokens() 48 | { 49 | return problematicTokens; 50 | } 51 | } 52 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/inference/proof/ProofStepChainCancellation.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Collections.ObjectModel; 5 | using aima.core.logic.fol.kb.data; 6 | using aima.core.logic.fol.parsing.ast; 7 | 8 | namespace aima.core.logic.fol.inference.proof 9 | { 10 | /** 11 | * @author Ciaran O'Reilly 12 | * 13 | */ 14 | public class ProofStepChainCancellation : AbstractProofStep 15 | { 16 | private List predecessors = new List(); 17 | private Chain cancellation = null; 18 | private Chain cancellationOf = null; 19 | private Dictionary subst = null; 20 | 21 | public ProofStepChainCancellation(Chain cancellation, Chain cancellationOf, 22 | Dictionary subst) 23 | { 24 | this.cancellation = cancellation; 25 | this.cancellationOf = cancellationOf; 26 | this.subst = subst; 27 | this.predecessors.Add(cancellationOf.getProofStep()); 28 | } 29 | 30 | // START-ProofStep 31 | 32 | public override List getPredecessorSteps() 33 | { 34 | return new ReadOnlyCollection(predecessors).ToList(); 35 | } 36 | 37 | public override String getProof() 38 | { 39 | return cancellation.ToString(); 40 | } 41 | 42 | public override String getJustification() 43 | { 44 | return "Cancellation: " + cancellationOf.getProofStep().getStepNumber() 45 | + " " + subst; 46 | } 47 | 48 | // END-ProofStep 49 | } 50 | } -------------------------------------------------------------------------------- /aima-csharp/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("aima-csharp")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("aima-csharp")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("125f53d5-1ccf-4daf-82fe-4324686cf417")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /aima-csharp/agent/EnvironmentView.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace aima.core.agent 5 | { 6 | /** 7 | * Allows external applications/logic to view the interaction of Agent(s) with 8 | * an Environment. 9 | * 10 | * @author Ravi Mohan 11 | * @author Ciaran O'Reilly 12 | * @author Mike Stampone 13 | */ 14 | public interface EnvironmentView 15 | { 16 | /** 17 | * A simple notification message from the Environment, from one of its 18 | * objects. 19 | * 20 | * @param msg 21 | * the message received. 22 | */ 23 | void notify(String msg); 24 | 25 | /** 26 | * Indicates an Agent has been added to the environment and what it 27 | * perceives initially. 28 | * 29 | * @param agent 30 | * the Agent just added to the Environment. 31 | * @param resultingState 32 | * the EnvironmentState that resulted from the Agent being added 33 | * to the Environment. 34 | */ 35 | void agentAdded(Agent agent, EnvironmentState resultingState); 36 | 37 | /** 38 | * Indicates the Environment has changed as a result of an Agent's action. 39 | * 40 | * @param agent 41 | * the Agent that performed the Action. 42 | * @param action 43 | * the Action the Agent performed. 44 | * @param resultingState 45 | * the EnvironmentState that resulted from the Agent's Action on 46 | * the Environment. 47 | */ 48 | void agentActed(Agent agent, Action action, EnvironmentState resultingState); 49 | } 50 | } -------------------------------------------------------------------------------- /aima-csharp/util/ArrayIterator.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | 5 | namespace aima.core.util 6 | { 7 | /** 8 | * Iterates efficiently through an array. 9 | * 10 | * @author Ruediger Lunde 11 | */ 12 | public class ArrayIterator : IEnumerator 13 | { 14 | T[] values; 15 | int counter; 16 | 17 | public T Current 18 | { 19 | get 20 | { 21 | return values[counter]; 22 | } 23 | } 24 | 25 | object IEnumerator.Current 26 | { 27 | get 28 | { 29 | return Current; 30 | } 31 | } 32 | 33 | public ArrayIterator(T[] values) 34 | { 35 | this.values = values; 36 | counter = 0; 37 | } 38 | 39 | public bool hasNext() 40 | { 41 | return counter < values.Length; 42 | } 43 | 44 | public T next() 45 | { 46 | return values[counter++]; 47 | } 48 | 49 | public void remove() 50 | { 51 | throw new NotSupportedException(); 52 | } 53 | 54 | public bool MoveNext() 55 | { 56 | counter++; 57 | return (counter < values.Length); 58 | } 59 | 60 | public void Dispose() 61 | { 62 | throw new NotImplementedException(); 63 | } 64 | 65 | public void Reset() 66 | { 67 | throw new NotImplementedException(); 68 | } 69 | } 70 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/inference/proof/ProofStepChainReduction.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Collections.ObjectModel; 5 | using aima.core.logic.fol.kb.data; 6 | using aima.core.logic.fol.parsing.ast; 7 | 8 | namespace aima.core.logic.fol.inference.proof 9 | { 10 | /** 11 | * @author Ciaran O'Reilly 12 | * 13 | */ 14 | public class ProofStepChainReduction : AbstractProofStep 15 | { 16 | private List predecessors = new List(); 17 | private Chain reduction = null; 18 | private Chain nearParent, farParent = null; 19 | private Dictionary subst = null; 20 | 21 | public ProofStepChainReduction(Chain reduction, Chain nearParent, 22 | Chain farParent, Dictionary subst) 23 | { 24 | this.reduction = reduction; 25 | this.nearParent = nearParent; 26 | this.farParent = farParent; 27 | this.subst = subst; 28 | this.predecessors.Add(farParent.getProofStep()); 29 | this.predecessors.Add(nearParent.getProofStep()); 30 | } 31 | 32 | // START-ProofStep 33 | 34 | public override List getPredecessorSteps() 35 | { 36 | return new ReadOnlyCollection(predecessors).ToList(); 37 | } 38 | 39 | public override String getProof() 40 | { 41 | return reduction.ToString(); 42 | } 43 | 44 | public override String getJustification() 45 | { 46 | return "Reduction: " + nearParent.getProofStep().getStepNumber() + "," 47 | + farParent.getProofStep().getStepNumber() + " " + subst; 48 | } 49 | 50 | // END-ProofStep 51 | } 52 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/inference/InferenceResultPrinter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using aima.core.logic.fol.inference.proof; 5 | 6 | namespace aima.core.logic.fol.inference 7 | { 8 | /** 9 | * @author Ciaran O'Reilly 10 | * 11 | */ 12 | public class InferenceResultPrinter 13 | { 14 | /** 15 | * Utility method for outputting InferenceResults in a formatted textual 16 | * representation. 17 | * 18 | * @param ir 19 | * an InferenceResult 20 | * @return a String representation of the InferenceResult. 21 | */ 22 | public static String printInferenceResult(InferenceResult ir) 23 | { 24 | StringBuilder sb = new StringBuilder(); 25 | 26 | sb.Append("InferenceResult.isTrue=" + ir.isTrue()); 27 | sb.Append("\n"); 28 | sb.Append("InferenceResult.isPossiblyFalse=" + ir.isPossiblyFalse()); 29 | sb.Append("\n"); 30 | sb.Append("InferenceResult.isUnknownDueToTimeout=" 31 | + ir.isUnknownDueToTimeout()); 32 | sb.Append("\n"); 33 | sb.Append("InferenceResult.isPartialResultDueToTimeout=" 34 | + ir.isPartialResultDueToTimeout()); 35 | sb.Append("\n"); 36 | sb.Append("InferenceResult.#Proofs=" + ir.getProofs().Count); 37 | sb.Append("\n"); 38 | int proofNo = 0; 39 | List proofs = ir.getProofs(); 40 | foreach (Proof p in proofs) 41 | { 42 | proofNo++; 43 | sb.Append("InferenceResult.Proof#" + proofNo + "=\n" 44 | + ProofPrinter.printProof(p)); 45 | } 46 | return sb.ToString(); 47 | } 48 | } 49 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/inference/InferenceResult.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using aima.core.logic.fol.inference.proof; 4 | 5 | namespace aima.core.logic.fol.inference 6 | { 7 | /** 8 | * @author Ciaran O'Reilly 9 | * 10 | */ 11 | public interface InferenceResult 12 | { 13 | /** 14 | * 15 | * @return true, if the query is not entailed from the premises. This just 16 | * means the query is not entailed, the query itself may be true. 17 | */ 18 | bool isPossiblyFalse(); 19 | 20 | /** 21 | * 22 | * @return true, if the query is entailed from the premises (Note: can get 23 | * partial results if the original query contains variables 24 | * indicating that there can possibly be more than 1 proof/bindings 25 | * for the query, see: isPartialResultDueToTimeout()). 26 | */ 27 | bool isTrue(); 28 | 29 | /** 30 | * 31 | * @return true, if the inference procedure ran for a length of time and 32 | * found no proof one way or the other before it timed out. 33 | */ 34 | bool isUnknownDueToTimeout(); 35 | 36 | /** 37 | * 38 | * @return true, if the inference procedure found a proof for a query 39 | * containing variables (i.e. possibly more than 1 proof can be 40 | * returned) and the inference procedure was still looking for other 41 | * possible answers before it timed out. 42 | */ 43 | bool isPartialResultDueToTimeout(); 44 | 45 | /** 46 | * 47 | * @return a list of 0 or more proofs (multiple proofs can be returned if 48 | * the original query contains variables). 49 | */ 50 | List getProofs(); 51 | } 52 | } -------------------------------------------------------------------------------- /aima-csharp/environment/eightpuzzle/ManhattanHeuristicFunction.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using aima.core.search.framework; 4 | using aima.core.util; 5 | 6 | namespace aima.core.environment.eightpuzzle 7 | { 8 | /** 9 | * @author Ravi Mohan 10 | * 11 | */ 12 | public class ManhattanHeuristicFunction : HeuristicFunction 13 | { 14 | public double h(Object state) 15 | { 16 | EightPuzzleBoard board = (EightPuzzleBoard)state; 17 | int retVal = 0; 18 | for (int i = 1; i < 9; i++) 19 | { 20 | XYLocation loc = board.getLocationOf(i); 21 | retVal += evaluateManhattanDistanceOf(i, loc); 22 | } 23 | return retVal; 24 | } 25 | 26 | public int evaluateManhattanDistanceOf(int i, XYLocation loc) 27 | { 28 | int retVal = -1; 29 | int xpos = loc.getXCoOrdinate(); 30 | int ypos = loc.getYCoOrdinate(); 31 | switch (i) 32 | { 33 | 34 | case 1: 35 | retVal = Math.Abs(xpos - 0) + Math.Abs(ypos - 1); 36 | break; 37 | case 2: 38 | retVal = Math.Abs(xpos - 0) + Math.Abs(ypos - 2); 39 | break; 40 | case 3: 41 | retVal = Math.Abs(xpos - 1) + Math.Abs(ypos - 0); 42 | break; 43 | case 4: 44 | retVal = Math.Abs(xpos - 1) + Math.Abs(ypos - 1); 45 | break; 46 | case 5: 47 | retVal = Math.Abs(xpos - 1) + Math.Abs(ypos - 2); 48 | break; 49 | case 6: 50 | retVal = Math.Abs(xpos - 2) + Math.Abs(ypos - 0); 51 | break; 52 | case 7: 53 | retVal = Math.Abs(xpos - 2) + Math.Abs(ypos - 1); 54 | break; 55 | case 8: 56 | retVal = Math.Abs(xpos - 2) + Math.Abs(ypos - 2); 57 | break; 58 | 59 | } 60 | return retVal; 61 | } 62 | } 63 | } -------------------------------------------------------------------------------- /aima-csharp/environment/map/Scenario.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace aima.core.environment.map 4 | { 5 | /** 6 | * A scenario specifies an environment, the agent's knowledge about the 7 | * environment, and the agents initial location. It can be used to specify 8 | * settings for route planning agent applications. 9 | * 10 | * @author Ruediger Lunde 11 | */ 12 | public class Scenario 13 | { 14 | /** 15 | * A map-based environment. Note that the contained map must be of type 16 | * {@link ExtendableMap}. 17 | */ 18 | private readonly MapEnvironment env; 19 | /** A map reflecting the knowledge of the agent about the environment. */ 20 | private readonly Map agentMap; 21 | /** Initial location of the agent. */ 22 | private readonly String initAgentLoc; 23 | 24 | /** 25 | * Creates a scenario. 26 | * 27 | * @param env 28 | * a map-based environment. Note that the contained map must be 29 | * of type {@link ExtendableMap} 30 | * @param agentMap 31 | * a map reflecting the knowledge of the agent about the 32 | * environment 33 | * @param agentLoc 34 | * initial location of the agent 35 | */ 36 | public Scenario(MapEnvironment env, Map agentMap, String agentLoc) 37 | { 38 | this.agentMap = agentMap; 39 | this.env = env; 40 | this.initAgentLoc = agentLoc; 41 | } 42 | 43 | public MapEnvironment getEnv() 44 | { 45 | return env; 46 | } 47 | 48 | public Map getEnvMap() 49 | { 50 | return env.getMap(); 51 | } 52 | 53 | public Map getAgentMap() 54 | { 55 | return agentMap; 56 | } 57 | 58 | public String getInitAgentLocation() 59 | { 60 | return initAgentLoc; 61 | } 62 | } 63 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/inference/proof/ProofStepClauseParamodulation.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Collections.ObjectModel; 5 | using aima.core.logic.fol.kb.data; 6 | using aima.core.logic.fol.parsing.ast; 7 | 8 | namespace aima.core.logic.fol.inference.proof 9 | { 10 | /** 11 | * @author Ciaran O'Reilly 12 | * 13 | */ 14 | public class ProofStepClauseParamodulation : AbstractProofStep 15 | { 16 | private List predecessors = new List(); 17 | private Clause paramodulated = null; 18 | private Clause topClause = null; 19 | private Clause equalityClause = null; 20 | private TermEquality assertion = null; 21 | 22 | public ProofStepClauseParamodulation(Clause paramodulated, 23 | Clause topClause, Clause equalityClause, TermEquality assertion) 24 | { 25 | this.paramodulated = paramodulated; 26 | this.topClause = topClause; 27 | this.equalityClause = equalityClause; 28 | this.assertion = assertion; 29 | this.predecessors.Add(topClause.getProofStep()); 30 | this.predecessors.Add(equalityClause.getProofStep()); 31 | } 32 | 33 | // START-ProofStep 34 | 35 | public override List getPredecessorSteps() 36 | { 37 | return new ReadOnlyCollection(predecessors).ToList(); 38 | } 39 | 40 | public override String getProof() 41 | { 42 | return paramodulated.ToString(); 43 | } 44 | 45 | public override String getJustification() 46 | { 47 | return "Paramodulation: " + topClause.getProofStep().getStepNumber() 48 | + ", " + equalityClause.getProofStep().getStepNumber() + ", [" 49 | + assertion + "]"; 50 | 51 | } 52 | 53 | // END-ProofStep 54 | } 55 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/PredicateCollector.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using aima.core.logic.fol.parsing; 4 | using aima.core.logic.fol.parsing.ast; 5 | 6 | namespace aima.core.logic.fol 7 | { 8 | /** 9 | * @author Ravi Mohan 10 | * 11 | */ 12 | public class PredicateCollector : FOLVisitor 13 | { 14 | public PredicateCollector() 15 | { 16 | 17 | } 18 | 19 | public List getPredicates(Sentence s) 20 | { 21 | return (List)s.accept(this, new List()); 22 | } 23 | 24 | public Object visitPredicate(Predicate p, Object arg) 25 | { 26 | List predicates = (List)arg; 27 | predicates.Add(p); 28 | return predicates; 29 | } 30 | 31 | public Object visitTermEquality(TermEquality equality, Object arg) 32 | { 33 | return arg; 34 | } 35 | 36 | public Object visitVariable(Variable variable, Object arg) 37 | { 38 | return arg; 39 | } 40 | 41 | public Object visitConstant(Constant constant, Object arg) 42 | { 43 | return arg; 44 | } 45 | 46 | public Object visitFunction(Function function, Object arg) 47 | { 48 | return arg; 49 | } 50 | 51 | public Object visitNotSentence(NotSentence sentence, Object arg) 52 | { 53 | sentence.getNegated().accept(this, arg); 54 | return arg; 55 | } 56 | 57 | public Object visitConnectedSentence(ConnectedSentence sentence, Object arg) 58 | { 59 | sentence.getFirst().accept(this, arg); 60 | sentence.getSecond().accept(this, arg); 61 | return arg; 62 | } 63 | 64 | public Object visitQuantifiedSentence(QuantifiedSentence sentence, 65 | Object arg) 66 | { 67 | sentence.getQuantified().accept(this, arg); 68 | return arg; 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /aima-csharp/util/Pair.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace aima.core.util 5 | { 6 | /** 7 | * @author Ravi Mohan 8 | * @author Mike Stampone 9 | * 10 | */ 11 | public class Pair 12 | { 13 | private X a; 14 | 15 | private Y b; 16 | 17 | /** 18 | * Constructs a Pair from two given elements 19 | * 20 | * @param a 21 | * the first element 22 | * @param b 23 | * the second element 24 | */ 25 | public Pair(X a, Y b) 26 | { 27 | this.a = a; 28 | this.b = b; 29 | } 30 | 31 | /** 32 | * Returns the first element of the pair 33 | * 34 | * @return the first element of the pair 35 | */ 36 | public X getFirst() 37 | { 38 | return a; 39 | } 40 | 41 | /** 42 | * Returns the second element of the pair 43 | * 44 | * @return the second element of the pair 45 | */ 46 | public Y getSecond() 47 | { 48 | return b; 49 | } 50 | 51 | 52 | public override bool Equals(Object o) 53 | { 54 | if (o is Pair) 55 | { 56 | Pair p = (Pair)o; 57 | return a.Equals(p.a) && b.Equals(p.b); 58 | } 59 | return false; 60 | } 61 | 62 | public int hashCode() 63 | { 64 | return a.GetHashCode() + 31 * b.GetHashCode(); 65 | } 66 | 67 | public override String ToString() 68 | { 69 | return "< " + getFirst().ToString() + " , " + getSecond().ToString() 70 | + " > "; 71 | } 72 | } 73 | } -------------------------------------------------------------------------------- /aima-csharp/environment/wumpusworld/Room.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace aima.core.environment.wumpusworld 4 | { 5 | /** 6 | * Artificial Intelligence A Modern Approach (3rd Edition): page 236.
7 | *
8 | * The wumpus world is a cave consisting of rooms connected by 9 | * passageways. Rooms are labeled [x,y], for example [1,1] would indicate the 10 | * room in the bottom left, and is also the room the agent always starts in. See 11 | * Figure 7.2 for an example room layout representing a wumpus's cave. 12 | * 13 | * @author Ciaran O'Reilly 14 | */ 15 | public class Room 16 | { 17 | private int x = 1; 18 | private int y = 1; 19 | 20 | /** 21 | * Constructor. 22 | * 23 | * @param x 24 | * the room's x location. 25 | * @param y 26 | * the room's y location. 27 | */ 28 | public Room(int x, int y) 29 | { 30 | this.x = x; 31 | this.y = y; 32 | } 33 | 34 | /** 35 | * 36 | * @return the room's x location. 37 | */ 38 | public int getX() 39 | { 40 | return x; 41 | } 42 | 43 | /** 44 | * 45 | * @return the room's y location. 46 | */ 47 | public int getY() 48 | { 49 | return y; 50 | } 51 | 52 | public string toString() 53 | { 54 | return "[" + x + "," + y + "]"; 55 | } 56 | 57 | public bool equals(object o) 58 | { 59 | if (o != null && o is Room) { 60 | Room r = (Room)o; 61 | if (x == r.x && y == r.y) 62 | { 63 | return true; 64 | } 65 | return false; 66 | } 67 | return false; 68 | } 69 | 70 | public int hashCode() 71 | { 72 | int result = 17; 73 | result = 37 * result + getX(); 74 | result = 43 * result + getY(); 75 | return result; 76 | } 77 | } 78 | } -------------------------------------------------------------------------------- /aima-csharp/search/framework/SearchAgent.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using aima.core.agent; 3 | using aima.core.agent.impl; 4 | using aima.core.search.framework.problem; 5 | 6 | namespace aima.core.search.framework 7 | { 8 | /** 9 | * @author Ravi Mohan 10 | * 11 | */ 12 | public class SearchAgent : AbstractAgent 13 | { 14 | protected List actionList; 15 | 16 | private List.Enumerator actionIterator; 17 | 18 | private Metrics searchMetrics; 19 | 20 | public SearchAgent(Problem p, Search search) 21 | { 22 | actionList = search.search(p); 23 | actionIterator = actionList.GetEnumerator(); 24 | searchMetrics = search.getMetrics(); 25 | } 26 | 27 | public override Action execute(Percept p) 28 | { 29 | 30 | if (actionIterator.MoveNext()) 31 | { 32 | return actionIterator.Current; 33 | } 34 | else 35 | { 36 | return NoOpAction.NO_OP; 37 | } 38 | } 39 | 40 | public bool isDone() 41 | { 42 | return null != actionIterator.Current; 43 | } 44 | 45 | public List getActions() 46 | { 47 | return actionList; 48 | } 49 | 50 | public Dictionary getInstrumentation() 51 | { 52 | Dictionary retVal = new Dictionary(); 53 | foreach (string key in searchMetrics.keySet()) 54 | { 55 | System.String value = searchMetrics.get(key); 56 | retVal.Add(key, value); 57 | } 58 | return retVal; 59 | } 60 | } 61 | } -------------------------------------------------------------------------------- /aima-csharp/environment/eightpuzzle/MisplacedTilleHeuristicFunction.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using aima.core.search.framework; 4 | using aima.core.util; 5 | 6 | namespace aima.core.environment.eightpuzzle 7 | { 8 | /** 9 | * @author Ravi Mohan 10 | * 11 | */ 12 | public class MisplacedTilleHeuristicFunction : HeuristicFunction 13 | { 14 | public double h(Object state) 15 | { 16 | EightPuzzleBoard board = (EightPuzzleBoard)state; 17 | return getNumberOfMisplacedTiles(board); 18 | } 19 | 20 | private int getNumberOfMisplacedTiles(EightPuzzleBoard board) 21 | { 22 | int numberOfMisplacedTiles = 0; 23 | if (!(board.getLocationOf(0).Equals(new XYLocation(0, 0)))) 24 | { 25 | numberOfMisplacedTiles++; 26 | } 27 | if (!(board.getLocationOf(1).Equals(new XYLocation(0, 1)))) 28 | { 29 | numberOfMisplacedTiles++; 30 | } 31 | if (!(board.getLocationOf(2).Equals(new XYLocation(0, 2)))) 32 | { 33 | numberOfMisplacedTiles++; 34 | } 35 | if (!(board.getLocationOf(3).Equals(new XYLocation(1, 0)))) 36 | { 37 | numberOfMisplacedTiles++; 38 | } 39 | if (!(board.getLocationOf(4).Equals(new XYLocation(1, 1)))) 40 | { 41 | numberOfMisplacedTiles++; 42 | } 43 | if (!(board.getLocationOf(5).Equals(new XYLocation(1, 2)))) 44 | { 45 | numberOfMisplacedTiles++; 46 | } 47 | if (!(board.getLocationOf(6).Equals(new XYLocation(2, 0)))) 48 | { 49 | numberOfMisplacedTiles++; 50 | } 51 | if (!(board.getLocationOf(7).Equals(new XYLocation(2, 1)))) 52 | { 53 | numberOfMisplacedTiles++; 54 | } 55 | if (!(board.getLocationOf(8).Equals(new XYLocation(2, 2)))) 56 | { 57 | numberOfMisplacedTiles++; 58 | } 59 | return numberOfMisplacedTiles; 60 | } 61 | } 62 | } -------------------------------------------------------------------------------- /aima-csharp/search/framework/Metrics.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace aima.core.search.framework 5 | { 6 | /** 7 | * Stores key-value pairs for efficiency analysis. 8 | * 9 | * @author Ravi Mohan 10 | * @author Ruediger Lunde 11 | */ 12 | public class Metrics 13 | { 14 | private Dictionary hash; 15 | 16 | public Metrics() 17 | { 18 | this.hash = new Dictionary(); 19 | } 20 | 21 | public void set(String name, int i) 22 | { 23 | hash[name] = i.ToString(); 24 | } 25 | 26 | public void set(String name, double d) 27 | { 28 | hash[name] = d.ToString(); 29 | } 30 | 31 | public void incrementInt(String name) 32 | { 33 | set(name, getInt(name) + 1); 34 | } 35 | 36 | public void set(String name, long l) 37 | { 38 | hash[name] = l.ToString(); 39 | } 40 | 41 | public int getInt(String name) 42 | { 43 | return int.Parse(hash[name]); 44 | } 45 | 46 | public double getDouble(String name) 47 | { 48 | return double.Parse(hash[name]); 49 | } 50 | 51 | public long getLong(String name) 52 | { 53 | return long.Parse(hash[name]); 54 | } 55 | 56 | public String get(String name) 57 | { 58 | return hash[name]; 59 | } 60 | 61 | public HashSet keySet() 62 | { 63 | return new HashSet(hash.Keys); 64 | } 65 | 66 | public String toString() 67 | { 68 | SortedDictionary map = new SortedDictionary(hash); 69 | return map.ToString(); 70 | } 71 | } 72 | } -------------------------------------------------------------------------------- /aima-csharp/util/Point2D.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace aima.core.util 5 | { 6 | /** 7 | * Simplified version of java.awt.geom.Point2D. We do not want 8 | * dependencies to presentation layer packages here. 9 | * 10 | * @author R. Lunde 11 | * @author Mike Stampone 12 | */ 13 | public class Point2D 14 | { 15 | private double x; 16 | private double y; 17 | 18 | public Point2D(double x, double y) 19 | { 20 | this.x = x; 21 | this.y = y; 22 | } 23 | 24 | /** 25 | * Returns the X coordinate of this Point2D in 26 | * double precision. 27 | * 28 | * @return the X coordinate of this Point2D. 29 | */ 30 | public double getX() 31 | { 32 | return x; 33 | } 34 | 35 | /** 36 | * Returns the Y coordinate of this Point2D in 37 | * double precision. 38 | * 39 | * @return the Y coordinate of this Point2D. 40 | */ 41 | public double getY() 42 | { 43 | return y; 44 | } 45 | 46 | /** 47 | * Returns the Euclidean distance between a specified point and this point. 48 | * 49 | * @return the Euclidean distance between a specified point and this point. 50 | */ 51 | public double distance(Point2D pt) 52 | { 53 | // Distance Between X Coordinates 54 | double x_distance = (pt.getX() - x) * (pt.getX() - x); 55 | // Distance Between Y Coordinates 56 | double y_distance = (pt.getY() - y) * (pt.getY() - y); 57 | // Distance Between 2d Points 58 | double total_distance = Math.Sqrt(x_distance + y_distance); 59 | 60 | return total_distance; 61 | } 62 | } 63 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/parsing/ast/Constant.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using aima.core.logic.fol.parsing; 4 | 5 | namespace aima.core.logic.fol.parsing.ast 6 | { 7 | /** 8 | * @author Ravi Mohan 9 | * @author Ciaran O'Reilly 10 | */ 11 | public class Constant : Term 12 | { 13 | private String value; 14 | private int hashCode = 0; 15 | 16 | public Constant(String s) 17 | { 18 | value = s; 19 | } 20 | 21 | public String getValue() 22 | { 23 | return value; 24 | } 25 | 26 | // START-Term 27 | 28 | public String getSymbolicName() 29 | { 30 | return getValue(); 31 | } 32 | 33 | public bool isCompound() 34 | { 35 | return false; 36 | } 37 | 38 | List FOLNode.getArgs() 39 | { 40 | return null; 41 | } 42 | 43 | public List getArgs() 44 | { 45 | // Is not Compound, therefore should 46 | // return null for its arguments 47 | return null; 48 | } 49 | 50 | public Object accept(FOLVisitor v, Object arg) 51 | { 52 | return v.visitConstant(this, arg); 53 | } 54 | 55 | FOLNode FOLNode.copy() 56 | { 57 | return copy(); 58 | } 59 | 60 | public Term copy() 61 | { 62 | return new Constant(value); 63 | } 64 | 65 | // END-Term 66 | 67 | public override bool Equals(Object o) 68 | { 69 | 70 | if (this == o) 71 | { 72 | return true; 73 | } 74 | if (!(o is Constant)) 75 | { 76 | return false; 77 | } 78 | Constant c = (Constant)o; 79 | return c.getValue().Equals(getValue()); 80 | 81 | } 82 | 83 | public override int GetHashCode() 84 | { 85 | if (0 == hashCode) 86 | { 87 | hashCode = 17; 88 | hashCode = 37 * hashCode + value.GetHashCode(); 89 | } 90 | return hashCode; 91 | } 92 | 93 | public override String ToString() 94 | { 95 | return value; 96 | } 97 | } 98 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/inference/proof/ProofStepFoChAssertFact.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Linq; 5 | using System.Collections.ObjectModel; 6 | using aima.core.logic.fol.kb.data; 7 | using aima.core.logic.fol.parsing.ast; 8 | 9 | namespace aima.core.logic.fol.inference.proof 10 | { 11 | /** 12 | * @author Ciaran O'Reilly 13 | * 14 | */ 15 | public class ProofStepFoChAssertFact : AbstractProofStep 16 | { 17 | private List predecessors = new List(); 18 | private Clause implication = null; 19 | private Literal fact = null; 20 | private Dictionary bindings = null; 21 | 22 | public ProofStepFoChAssertFact(Clause implication, Literal fact, 23 | Dictionary bindings, ProofStep predecessor) 24 | { 25 | this.implication = implication; 26 | this.fact = fact; 27 | this.bindings = bindings; 28 | if (null != predecessor) 29 | { 30 | predecessors.Add(predecessor); 31 | } 32 | } 33 | 34 | // START-ProofStep 35 | 36 | public override List getPredecessorSteps() 37 | { 38 | return new ReadOnlyCollection(predecessors).ToList(); 39 | } 40 | 41 | public override String getProof() 42 | { 43 | StringBuilder sb = new StringBuilder(); 44 | List nLits = implication.getNegativeLiterals(); 45 | for (int i = 0; i < implication.getNumberNegativeLiterals(); i++) 46 | { 47 | sb.Append(nLits[i].getAtomicSentence()); 48 | if (i != (implication.getNumberNegativeLiterals() - 1)) 49 | { 50 | sb.Append(" AND "); 51 | } 52 | } 53 | sb.Append(" => "); 54 | sb.Append(implication.getPositiveLiterals()[0]); 55 | return sb.ToString(); 56 | } 57 | 58 | public override String getJustification() 59 | { 60 | return "Assert fact " + fact.ToString() + ", " + bindings; 61 | } 62 | 63 | // END-ProofStep 64 | } 65 | } -------------------------------------------------------------------------------- /aima-csharp/util/Triplet.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace aima.core.util 5 | { 6 | /** 7 | * @author Ravi Mohan 8 | * @author Mike Stampone 9 | * 10 | */ 11 | public class Triplet 12 | { 13 | private X x; 14 | 15 | private Y y; 16 | 17 | private Z z; 18 | 19 | /** 20 | * Constructs a triplet with three specified elements. 21 | * 22 | * @param x 23 | * the first element of the triplet. 24 | * @param y 25 | * the second element of the triplet. 26 | * @param z 27 | * the third element of the triplet. 28 | */ 29 | public Triplet(X x, Y y, Z z) 30 | { 31 | this.x = x; 32 | this.y = y; 33 | this.z = z; 34 | } 35 | 36 | /** 37 | * Returns the second element of the triplet. 38 | * 39 | * @return the second element of the triplet. 40 | */ 41 | public Y getSecond() 42 | { 43 | return y; 44 | } 45 | 46 | /** 47 | * Returns the third element of the triplet. 48 | * 49 | * @return the third element of the triplet. 50 | */ 51 | public Z getThird() 52 | { 53 | return z; 54 | } 55 | 56 | public override bool Equals(Object o) 57 | { 58 | if (o is Triplet) 59 | { 60 | Triplet other = (Triplet)o; 61 | return (x.Equals(other.x)) && (y.Equals(other.y)) 62 | && (y.Equals(other.y)); 63 | } 64 | return false; 65 | } 66 | 67 | public int hashCode() 68 | { 69 | return x.GetHashCode() + 31 * y.GetHashCode() + 31 * z.GetHashCode(); 70 | } 71 | 72 | public String toString() 73 | { 74 | return "< " + x.ToString() + " , " + y.ToString() + " , " 75 | + z.ToString() + " >"; 76 | } 77 | } 78 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/inference/proof/ProofStepClauseFactor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Collections.ObjectModel; 5 | using aima.core.logic.fol.kb.data; 6 | using aima.core.logic.fol.parsing.ast; 7 | 8 | namespace aima.core.logic.fol.inference.proof 9 | { 10 | /** 11 | * @author Ciaran O'Reilly 12 | * 13 | */ 14 | public class ProofStepClauseFactor : AbstractProofStep 15 | { 16 | private List predecessors = new List(); 17 | private Clause factor = null; 18 | private Clause factorOf = null; 19 | private Literal lx = null; 20 | private Literal ly = null; 21 | private Dictionary subst = new Dictionary(); 22 | private Dictionary renameSubst = new Dictionary(); 23 | 24 | public ProofStepClauseFactor(Clause factor, Clause factorOf, Literal lx, 25 | Literal ly, Dictionary subst, 26 | Dictionary renameSubst) 27 | { 28 | this.factor = factor; 29 | this.factorOf = factorOf; 30 | this.lx = lx; 31 | this.ly = ly; 32 | 33 | foreach (Variable key in subst.Keys) 34 | { 35 | this.subst.Add(key, subst[key]); 36 | } 37 | 38 | foreach (Variable key in renameSubst.Keys) 39 | { 40 | this.renameSubst.Add(key, renameSubst[key]); 41 | } 42 | 43 | this.predecessors.Add(factorOf.getProofStep()); 44 | } 45 | 46 | // START-ProofStep 47 | 48 | public override List getPredecessorSteps() 49 | { 50 | return new ReadOnlyCollection(predecessors).ToList(); 51 | } 52 | 53 | public override String getProof() 54 | { 55 | return factor.ToString(); 56 | } 57 | 58 | public override String getJustification() 59 | { 60 | return "Factor of " + factorOf.getProofStep().getStepNumber() + " [" 61 | + lx + ", " + ly + "], subst=" + subst + ", renaming=" 62 | + renameSubst; 63 | } 64 | 65 | // END-ProofStep 66 | } 67 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/inference/otter/defaultimpl/DefaultLightestClauseHeuristic.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using aima.core.logic.fol.inference.otter; 5 | using aima.core.logic.fol.kb.data; 6 | 7 | namespace aima.core.logic.fol.inference.otter.defaultimpl 8 | { 9 | /** 10 | * @author Ciaran O'Reilly 11 | * 12 | */ 13 | public class DefaultLightestClauseHeuristic : LightestClauseHeuristic 14 | { 15 | private SortedSet sos; 16 | 17 | public DefaultLightestClauseHeuristic() 18 | { 19 | LightestClauseSorter c = new LightestClauseSorter(); 20 | sos = new SortedSet(c); 21 | } 22 | 23 | // START-LightestClauseHeuristic 24 | 25 | public Clause getLightestClause() 26 | { 27 | Clause lightest = null; 28 | 29 | if (sos.Count > 0) 30 | { 31 | lightest = sos.First(); 32 | } 33 | 34 | return lightest; 35 | } 36 | 37 | public void initialSOS(List clauses) 38 | { 39 | sos.Clear(); 40 | sos.UnionWith(clauses); 41 | } 42 | 43 | public void addedClauseToSOS(Clause clause) 44 | { 45 | sos.Add(clause); 46 | } 47 | 48 | public void removedClauseFromSOS(Clause clause) 49 | { 50 | sos.Remove(clause); 51 | } 52 | 53 | // END-LightestClauseHeuristic 54 | } 55 | 56 | class LightestClauseSorter : IComparer 57 | { 58 | public int Compare(Clause c1, Clause c2) 59 | { 60 | if (c1.Equals(c2)) 61 | { 62 | return 0; 63 | } 64 | int c1Val = c1.getNumberLiterals(); 65 | int c2Val = c2.getNumberLiterals(); 66 | return (c1Val < c2Val ? -1 67 | : (c1Val == c2Val ? (compareEqualityIdentities(c1, c2)) : 1)); 68 | } 69 | 70 | private int compareEqualityIdentities(Clause c1, Clause c2) 71 | { 72 | int c1Len = c1.getEqualityIdentity().Length; 73 | int c2Len = c2.getEqualityIdentity().Length; 74 | 75 | return (c1Len < c2Len ? -1 : (c1Len == c2Len ? c1.getEqualityIdentity() 76 | .CompareTo(c2.getEqualityIdentity()) : 1)); 77 | } 78 | } 79 | } -------------------------------------------------------------------------------- /aima-csharp/environment/map/MapEnvironment.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using aima.core.agent; 3 | using aima.core.agent.impl; 4 | 5 | namespace aima.core.environment.map 6 | { 7 | /** 8 | * Represents the environment a MapAgent can navigate. 9 | * 10 | * @author Ciaran O'Reilly 11 | * 12 | */ 13 | public class MapEnvironment : AbstractEnvironment 14 | { 15 | private Map map = null; 16 | private MapEnvironmentState state = new MapEnvironmentState(); 17 | 18 | public MapEnvironment(Map map) 19 | { 20 | this.map = map; 21 | } 22 | 23 | public void addAgent(Agent a, string startLocation) 24 | { 25 | // Ensure the agent state information is tracked before 26 | // adding to super, as super will notify the registered 27 | // EnvironmentViews that is was added. 28 | state.setAgentLocationAndTravelDistance(a, startLocation, 0.0); 29 | base.addAgent(a); 30 | } 31 | 32 | public string getAgentLocation(Agent a) 33 | { 34 | return state.getAgentLocation(a); 35 | } 36 | 37 | public double getAgentTravelDistance(Agent a) 38 | { 39 | return state.getAgentTravelDistance(a); 40 | } 41 | 42 | public override EnvironmentState getCurrentState() 43 | { 44 | return state; 45 | } 46 | 47 | public override EnvironmentState executeAction(Agent agent, Action a) 48 | { 49 | 50 | if (!a.isNoOp()) 51 | { 52 | MoveToAction act = (MoveToAction)a; 53 | 54 | string currLoc = getAgentLocation(agent); 55 | double distance = map.getDistance(currLoc, act.getToLocation()); 56 | if (distance != null) 57 | { 58 | double currTD = getAgentTravelDistance(agent); 59 | state.setAgentLocationAndTravelDistance(agent, 60 | act.getToLocation(), currTD + distance); 61 | } 62 | } 63 | return state; 64 | } 65 | 66 | public override Percept getPerceptSeenBy(Agent anAgent) 67 | { 68 | return new DynamicPercept(DynAttributeNames.PERCEPT_IN, 69 | getAgentLocation(anAgent)); 70 | } 71 | 72 | public Map getMap() 73 | { 74 | return map; 75 | } 76 | } 77 | } -------------------------------------------------------------------------------- /aima-csharp/agent/impl/aprog/simplerule/Rule.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Text; 3 | using System.Diagnostics; 4 | using aima.core.agent; 5 | using aima.core.agent.impl; 6 | 7 | namespace aima.core.agent.impl.aprog.simplerule 8 | { 9 | /** 10 | * A simple implementation of a "condition-action rule". 11 | * 12 | * @author Ciaran O'Reilly 13 | * @author Mike Stampone 14 | */ 15 | public class Rule 16 | { 17 | private Condition con; 18 | 19 | private Action action; 20 | 21 | 22 | /** 23 | * Constructs a condition-action rule. 24 | * 25 | * @param con 26 | * a condition 27 | * @param action 28 | * an action 29 | */ 30 | public Rule(Condition c, Action act) 31 | { 32 | Debug.Assert(null != con); 33 | Debug.Assert(null != action); 34 | 35 | con = c; 36 | action = act; 37 | } 38 | 39 | public bool evaluate(ObjectWithDynamicAttributes p) 40 | { 41 | return (con.evaluate(p)); 42 | } 43 | 44 | /** 45 | * Returns the action of this condition-action rule. 46 | * 47 | * @return the action of this condition-action rule. 48 | */ 49 | public Action getAction() 50 | { 51 | return action; 52 | } 53 | 54 | public override bool Equals(System.Object o) 55 | { 56 | if (o == null || !(o is Rule)) 57 | { 58 | return base.Equals(o); 59 | } 60 | return (ToString().Equals(((Rule)o).ToString())); 61 | } 62 | 63 | public override int GetHashCode() 64 | { 65 | return ToString().GetHashCode(); 66 | } 67 | 68 | public override System.String ToString() 69 | { 70 | StringBuilder sb = new StringBuilder(); 71 | 72 | return sb.Append("if ").Append(con).Append(" then ").Append(action) 73 | .Append(".").ToString(); 74 | } 75 | } 76 | } -------------------------------------------------------------------------------- /aima-csharp/environment/cellworld/Cell.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace aima.core.environment.cellworld 4 | { 5 | /** 6 | * Artificial Intelligence A Modern Approach (3rd Edition): page 645.
7 | *
8 | * A representation of a Cell in the environment detailed in Figure 17.1. 9 | * 10 | * @param 11 | * the content type of the cell. 12 | * 13 | * @author Ciaran O'Reilly 14 | * @author Ravi Mohan 15 | */ 16 | public class Cell 17 | { 18 | private int x = 1; 19 | private int y = 1; 20 | private C content = default(C); 21 | 22 | /** 23 | * Construct a Cell. 24 | * 25 | * @param x 26 | * the x position of the cell. 27 | * @param y 28 | * the y position of the cell. 29 | * @param content 30 | * the initial content of the cell. 31 | */ 32 | public Cell(int x, int y, C content) 33 | { 34 | this.x = x; 35 | this.y = y; 36 | this.content = content; 37 | } 38 | 39 | /** 40 | * 41 | * @return the x position of the cell. 42 | */ 43 | public int getX() 44 | { 45 | return x; 46 | } 47 | 48 | /** 49 | * 50 | * @return the y position of the cell. 51 | */ 52 | public int getY() 53 | { 54 | return y; 55 | } 56 | 57 | /** 58 | * 59 | * @return the content of the cell. 60 | */ 61 | public C getContent() 62 | { 63 | return content; 64 | } 65 | 66 | /** 67 | * Set the cell's content. 68 | * 69 | * @param content 70 | * the content to be placed in the cell. 71 | */ 72 | public void setContent(C content) 73 | { 74 | this.content = content; 75 | } 76 | 77 | public String toString() 78 | { 79 | return ""; 80 | } 81 | 82 | public bool equals(Object o) 83 | { 84 | if (o is Cell) { 85 | Cell c = (Cell ) o; 86 | return x == c.x && y == c.y && content.Equals(c.content); 87 | } 88 | return false; 89 | } 90 | 91 | public int hashCode() 92 | { 93 | return x + 23 + y + 31 * content.GetHashCode(); 94 | } 95 | } 96 | } -------------------------------------------------------------------------------- /aima-csharp/agent/impl/DynamicPercept.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using aima.core.agent; 4 | using System.Diagnostics; 5 | 6 | namespace aima.core.agent.impl 7 | { 8 | /** 9 | * @author Ravi Mohan 10 | * @author Ciaran O'Reilly 11 | */ 12 | public class DynamicPercept : ObjectWithDynamicAttributes, Percept 13 | { 14 | public DynamicPercept() 15 | { 16 | 17 | } 18 | 19 | public String describeType() 20 | { 21 | return typeof(Percept).Name; 22 | } 23 | 24 | /** 25 | * Constructs a DynamicPercept with one attribute 26 | * 27 | * @param key1 28 | * the attribute key 29 | * @param value1 30 | * the attribute value 31 | */ 32 | public DynamicPercept(Object key1, Object value1) 33 | { 34 | setAttribute(key1, value1); 35 | } 36 | 37 | 38 | /** 39 | * Constructs a DynamicPercept with two attributes 40 | * 41 | * @param key1 42 | * the first attribute key 43 | * @param value1 44 | * the first attribute value 45 | * @param key2 46 | * the second attribute key 47 | * @param value2 48 | * the second attribute value 49 | */ 50 | public DynamicPercept(Object key1, Object value1, Object key2, Object value2) 51 | { 52 | setAttribute(key1, value1); 53 | setAttribute(key2, value2); 54 | } 55 | 56 | /** 57 | * Constructs a DynamicPercept with an array of attributes 58 | * 59 | * @param keys 60 | * the array of attribute keys 61 | * @param values 62 | * the array of attribute values 63 | */ 64 | public DynamicPercept(Object[] keys, Object[] values) 65 | { 66 | Debug.Assert(keys.Length == values.Length); 67 | 68 | for(int i = 0; i < keys.Length; i++) 69 | { 70 | setAttribute(keys[i], values[i]); 71 | } 72 | } 73 | } 74 | } -------------------------------------------------------------------------------- /aima-csharp/search/Local/Individual.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace aima.core.search.local 5 | { 6 | /** 7 | * Artificial Intelligence A Modern Approach (3rd Edition): page 127.
8 | *
9 | * A state in a genetic algorithm is represented as an individual from the 10 | * population. 11 | * 12 | * @author Ciaran O'Reilly 13 | * 14 | * @param 15 | * the type of the alphabet used in the representation of the 16 | * individuals in the population (this is to provide flexibility in 17 | * terms of how a problem can be encoded). 18 | */ 19 | public class Individual 20 | { 21 | private List representation = new List(); 22 | private int descendants; // for debugging 23 | 24 | /** 25 | * Construct an individual using the provided representation. 26 | * 27 | * @param representation 28 | * the individual's representation. 29 | */ 30 | public Individual(List representation) 31 | { 32 | this.representation = representation; 33 | } 34 | 35 | /** 36 | * 37 | * @return the individual's representation. 38 | */ 39 | public List getRepresentation() 40 | { 41 | return representation; 42 | } 43 | 44 | /** 45 | * 46 | * @return the length of the individual's representation. 47 | */ 48 | public int length() 49 | { 50 | return representation.Count; 51 | } 52 | 53 | /** 54 | * Should be called by the genetic algorithm whenever the individual is 55 | * selected to produce a descendant. 56 | */ 57 | public void incDescendants() 58 | { 59 | descendants++; 60 | } 61 | 62 | // Returns the number of descendants for this individual. 63 | public int getDescendants() 64 | { 65 | return descendants; 66 | } 67 | 68 | public String toString() 69 | { 70 | return representation.ToString() + descendants; 71 | } 72 | } 73 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/StandardizeApartIndexicalFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace aima.core.logic.fol 6 | { 7 | /** 8 | * This class ensures unique standardize apart indexicals are created. 9 | * 10 | * @author Ciaran O'Reilly 11 | * 12 | */ 13 | public class StandardizeApartIndexicalFactory 14 | { 15 | private static Dictionary _assignedIndexicals = new Dictionary(); 16 | 17 | // For use in test cases, where predictable behavior is expected. 18 | public static void flush() 19 | { 20 | lock (_assignedIndexicals) 21 | { 22 | _assignedIndexicals.Clear(); 23 | } 24 | } 25 | 26 | public static StandardizeApartIndexical newStandardizeApartIndexical( 27 | Char preferredPrefix) 28 | { 29 | char ch = preferredPrefix; 30 | if (!(Char.IsLetter(ch) && Char.IsLower(ch))) 31 | { 32 | throw new ArgumentException("Preferred prefix :" 33 | + preferredPrefix + " must be a valid a lower case letter."); 34 | } 35 | 36 | StringBuilder sb = new StringBuilder(); 37 | lock (_assignedIndexicals) 38 | { 39 | int currentPrefixCnt = -1; 40 | if (!_assignedIndexicals.ContainsKey(preferredPrefix)) 41 | { 42 | currentPrefixCnt = 0; 43 | _assignedIndexicals.Add(preferredPrefix, currentPrefixCnt); 44 | } 45 | else 46 | { 47 | currentPrefixCnt += 1; 48 | _assignedIndexicals[preferredPrefix] = currentPrefixCnt; 49 | } 50 | 51 | sb.Append(preferredPrefix); 52 | for (int i = 0; i < currentPrefixCnt; i++) 53 | { 54 | sb.Append(preferredPrefix); 55 | } 56 | } 57 | 58 | return new StandardizeApartIndexicalImpl(sb.ToString()); 59 | } 60 | } 61 | 62 | class StandardizeApartIndexicalImpl : StandardizeApartIndexical 63 | { 64 | private String prefix = null; 65 | private int index = 0; 66 | 67 | public StandardizeApartIndexicalImpl(String prefix) 68 | { 69 | this.prefix = prefix; 70 | } 71 | 72 | // START-StandardizeApartIndexical 73 | public String getPrefix() 74 | { 75 | return prefix; 76 | } 77 | 78 | public int getNextIndex() 79 | { 80 | return index++; 81 | } 82 | // END-StandardizeApartIndexical 83 | } 84 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/inference/proof/ProofStepBwChGoal.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Linq; 5 | using System.Collections.ObjectModel; 6 | using aima.core.logic.fol.kb.data; 7 | using aima.core.logic.fol.parsing.ast; 8 | 9 | namespace aima.core.logic.fol.inference.proof 10 | { 11 | /** 12 | * @author Ciaran O'Reilly 13 | * 14 | */ 15 | public class ProofStepBwChGoal : AbstractProofStep 16 | { 17 | private List predecessors = new List(); 18 | private Clause toProve = null; 19 | private Literal currentGoal = null; 20 | private Dictionary bindings = new Dictionary(); 21 | 22 | public ProofStepBwChGoal(Clause toProve, Literal currentGoal, 23 | Dictionary bindings) 24 | { 25 | this.toProve = toProve; 26 | this.currentGoal = currentGoal; 27 | foreach (Variable key in bindings.Keys) 28 | { 29 | this.bindings.Add(key, bindings[key]); 30 | } 31 | } 32 | 33 | public Dictionary getBindings() 34 | { 35 | return bindings; 36 | } 37 | 38 | public void setPredecessor(ProofStep predecessor) 39 | { 40 | predecessors.Clear(); 41 | predecessors.Add(predecessor); 42 | } 43 | 44 | // START-ProofStep 45 | 46 | public override List getPredecessorSteps() 47 | { 48 | return new ReadOnlyCollection(predecessors).ToList(); 49 | } 50 | 51 | public override String getProof() 52 | { 53 | StringBuilder sb = new StringBuilder(); 54 | List nLits = toProve.getNegativeLiterals(); 55 | for (int i = 0; i < toProve.getNumberNegativeLiterals(); i++) 56 | { 57 | sb.Append(nLits[i].getAtomicSentence()); 58 | if (i != (toProve.getNumberNegativeLiterals() - 1)) 59 | { 60 | sb.Append(" AND "); 61 | } 62 | } 63 | if (toProve.getNumberNegativeLiterals() > 0) 64 | { 65 | sb.Append(" => "); 66 | } 67 | sb.Append(toProve.getPositiveLiterals()[0]); 68 | return sb.ToString(); 69 | } 70 | 71 | public override String getJustification() 72 | { 73 | return "Current Goal " + currentGoal.getAtomicSentence().ToString() 74 | + ", " + bindings; 75 | } 76 | 77 | // END-ProofStep 78 | } 79 | } -------------------------------------------------------------------------------- /aima-csharp/search/framework/qsearch/TreeSearch.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using aima.core.search.framework; 3 | using aima.core.search.framework.problem; 4 | 5 | namespace aima.core.search.framework.qsearch 6 | { 7 | /** 8 | * Artificial Intelligence A Modern Approach (3rd Edition): Figure 3.7, page 77. 9 | *
10 | * 11 | *
12 |      * function TREE-SEARCH(problem) returns a solution, or failure
13 |      *   initialize the frontier using the initial state of the problem
14 |      *   loop do
15 |      *     if the frontier is empty then return failure
16 |      *     choose a leaf node and remove it from the frontier
17 |      *     if the node contains a goal state then return the corresponding solution
18 |      *     expand the chosen node, adding the resulting nodes to the frontier
19 |      * 
20 | * 21 | * Figure 3.7 An informal description of the general tree-search algorithm. 22 | * 23 | *
24 | * This implementation is based on the template method 25 | * {@link #search(Problem, Queue)} from superclass {@link QueueSearch} and 26 | * provides implementations for the needed primitive operations. 27 | * 28 | * @author Ravi Mohan 29 | * @author Ruediger Lunde 30 | * 31 | */ 32 | public class TreeSearch : QueueSearch 33 | { 34 | public TreeSearch(): this(new NodeExpander()) 35 | { 36 | 37 | } 38 | 39 | public TreeSearch(NodeExpander nodeExpander): base(nodeExpander) 40 | { 41 | 42 | } 43 | 44 | /** 45 | * Inserts the node at the tail of the frontier. 46 | */ 47 | protected override void addToFrontier(Node node) 48 | { 49 | frontier.Enqueue(node); 50 | updateMetrics(frontier.Count); 51 | } 52 | 53 | /** 54 | * Removes and returns the node at the head of the frontier. 55 | * 56 | * @return the node at the head of the frontier. 57 | */ 58 | protected override Node removeFromFrontier() 59 | { 60 | Node result = frontier.Dequeue(); 61 | updateMetrics(frontier.Count); 62 | return result; 63 | } 64 | 65 | /** 66 | * Checks whether the frontier contains not yet expanded nodes. 67 | */ 68 | protected override bool isFrontierEmpty() 69 | { 70 | if(frontier.Count == 0) 71 | { 72 | return true; 73 | } 74 | else 75 | return false; 76 | } 77 | } 78 | } -------------------------------------------------------------------------------- /aima-csharp/logic/propositional/parsing/PLVisitor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using aima.core.logic.propositional.parsing.ast; 4 | 5 | namespace aima.core.logic.propositional.parsing 6 | { 7 | /** 8 | * Propositional Logic Visitor: A
Visitor Pattern/ for 10 | * traversing the abstract syntax tree structural representation of 11 | * propositional logic used in this library. The key difference between the 12 | * default Visitor pattern and the code here, is that in the former the visit() 13 | * methods have a void visit(ConcreteNode) signature while the visitors used 14 | * here have a Object visit(ConcreteNode, Object arg) signature. This simplifies 15 | * testing and allows some recursive code that is hard with the former . 16 | * 17 | * @author Ravi Mohan 18 | * @author Ciaran O'Reilly 19 | * 20 | * @param 21 | * the argument type to be passed to the visitor methods. 22 | * @param 23 | * the return type to be returned from the visitor methods. 24 | */ 25 | public interface PLVisitor 26 | { 27 | /** 28 | * Visit a proposition symbol (e.g A). 29 | * 30 | * @param sentence 31 | * a Sentence that is a propositional symbol. 32 | * @param arg 33 | * optional argument to be used by the visitor. 34 | * @return optional return value to be used by the visitor. 35 | */ 36 | R visitPropositionSymbol(PropositionSymbol sentence, A arg); 37 | 38 | /** 39 | * Visit a unary complex sentence (e.g. ~A). 40 | * 41 | * @param sentence 42 | * a Sentence that is a unary complex sentence. 43 | * @param arg 44 | * optional argument to be used by the visitor. 45 | * @return optional return value to be used by the visitor. 46 | */ 47 | R visitUnarySentence(ComplexSentence sentence, A arg); 48 | 49 | /** 50 | * Visit a binary complex sentence (e.g. A & B). 51 | * 52 | * @param sentence 53 | * a Sentence that is a binary complex sentence. 54 | * @param arg 55 | * optional argument to be used by the visitor. 56 | * @return optional return value to be used by the visitor. 57 | */ 58 | R visitBinarySentence(ComplexSentence sentence, A arg); 59 | } 60 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/inference/proof/ProofPrinter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace aima.core.logic.fol.inference.proof 6 | { 7 | /** 8 | * @author Ciaran O'Reilly 9 | * 10 | */ 11 | public class ProofPrinter 12 | { 13 | /** 14 | * Utility method for outputting proofs in a formatted textual 15 | * representation. 16 | * 17 | * @param proof 18 | * @return a String representation of the Proof. 19 | */ 20 | public static String printProof(Proof proof) 21 | { 22 | StringBuilder sb = new StringBuilder(); 23 | 24 | sb.Append("Proof, Answer Bindings: "); 25 | sb.Append(proof.getAnswerBindings()); 26 | sb.Append("\n"); 27 | 28 | List steps = proof.getSteps(); 29 | 30 | int maxStepWidth = "Step".Length; 31 | int maxProofWidth = "Proof".Length; 32 | int maxJustificationWidth = "Justification".Length; 33 | 34 | // Calculate the maximum width for each column in the proof 35 | foreach (ProofStep step in steps) 36 | { 37 | String sn = "" + step.getStepNumber(); 38 | if (sn.Length > maxStepWidth) 39 | { 40 | maxStepWidth = sn.Length; 41 | } 42 | if (step.getProof().Length > maxProofWidth) 43 | { 44 | maxProofWidth = step.getProof().Length; 45 | } 46 | if (step.getJustification().Length > maxJustificationWidth) 47 | { 48 | maxJustificationWidth = step.getJustification().Length; 49 | } 50 | } 51 | 52 | // Give a little extra padding 53 | maxStepWidth += 1; 54 | maxProofWidth += 1; 55 | maxJustificationWidth += 1; 56 | 57 | String f = "|%-" + maxStepWidth + "s| %-" + maxProofWidth + "s|%-" 58 | + maxJustificationWidth + "s|\n"; 59 | 60 | int barWidth = 5 + maxStepWidth + maxProofWidth + maxJustificationWidth; 61 | StringBuilder bar = new StringBuilder(); 62 | for (int i = 0; i < barWidth; i++) 63 | { 64 | bar.Append("-"); 65 | } 66 | bar.Append("\n"); 67 | 68 | sb.Append(bar); 69 | sb.Append(String.Format(f, "Step", "Proof", "Justification")); 70 | sb.Append(bar); 71 | foreach (ProofStep step in steps) 72 | { 73 | sb.Append(String.Format(f, "" + step.getStepNumber(), step 74 | .getProof(), step.getJustification())); 75 | } 76 | sb.Append(bar); 77 | return sb.ToString(); 78 | } 79 | } 80 | } -------------------------------------------------------------------------------- /aima-csharp/logic/common/Token.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace aima.core.logic.common 5 | { 6 | /** 7 | * A token generated by a lexer from a sequence of characters. 8 | * 9 | * @author Ravi Mohan 10 | * @author Ciaran O'Reilly 11 | * @author Mike Stampone 12 | */ 13 | public class Token 14 | { 15 | private int type; 16 | private String text; 17 | private int startCharPositionInInput; 18 | 19 | /** 20 | * Constructs a token from the specified token-name and attribute-value 21 | * 22 | * @param type 23 | * the token-name 24 | * @param text 25 | * the attribute-value 26 | * @param startCharPositionInInput 27 | * the position (starting from 0) at which this token 28 | * starts in the input. 29 | */ 30 | public Token(int type, String text, int startCharPositionInInput) 31 | { 32 | this.type = type; 33 | this.text = text; 34 | this.startCharPositionInInput = startCharPositionInInput; 35 | } 36 | 37 | /** 38 | * Returns the attribute-value of this token. 39 | * 40 | * @return the attribute-value of this token. 41 | */ 42 | public String getText() 43 | { 44 | return text; 45 | } 46 | 47 | /** 48 | * Returns the token-name of this token. 49 | * 50 | * @return the token-name of this token. 51 | */ 52 | public int getType() 53 | { 54 | return type; 55 | } 56 | 57 | /** 58 | * @return the position (starting from 0) at which this token starts in the 59 | * input. 60 | */ 61 | public int getStartCharPositionInInput() 62 | { 63 | return startCharPositionInInput; 64 | } 65 | 66 | public override bool Equals(Object o) 67 | { 68 | 69 | if (this == o) 70 | { 71 | return true; 72 | } 73 | if ((o == null) || !(o is Token)) 74 | { 75 | return false; 76 | } 77 | 78 | Token other = (Token)o; 79 | return ((other.type == type) && (other.text.Equals(text)) && (other.startCharPositionInInput == startCharPositionInInput)); 80 | } 81 | 82 | public override int GetHashCode() 83 | { 84 | int result = 17; 85 | result = 37 * result + type; 86 | result = 37 * result + text.GetHashCode(); 87 | result = 37 * result + startCharPositionInInput; 88 | return result; 89 | } 90 | 91 | public override String ToString() 92 | { 93 | return "[ " + type + " " + text + " " + startCharPositionInInput + " ]"; 94 | } 95 | } 96 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/parsing/ast/NotSentence.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Linq; 5 | using aima.core.logic.fol; 6 | using aima.core.logic.fol.parsing; 7 | using aima.core.logic.fol.inference.proof; 8 | using System.Collections.ObjectModel; 9 | 10 | namespace aima.core.logic.fol.parsing.ast 11 | { 12 | /** 13 | * @author Ravi Mohan 14 | * @author Ciaran O'Reilly 15 | */ 16 | public class NotSentence : Sentence 17 | { 18 | private Sentence negated; 19 | private List args = new List(); 20 | private String stringRep = null; 21 | private int hashCode = 0; 22 | 23 | public NotSentence(Sentence negated) 24 | { 25 | this.negated = negated; 26 | args.Add(negated); 27 | } 28 | 29 | public Sentence getNegated() 30 | { 31 | return negated; 32 | } 33 | 34 | // START-Sentence 35 | 36 | public String getSymbolicName() 37 | { 38 | return Connectors.NOT; 39 | } 40 | 41 | public bool isCompound() 42 | { 43 | return true; 44 | } 45 | 46 | public List getArgs() 47 | { 48 | return new ReadOnlyCollection(args).ToList(); 49 | } 50 | 51 | public Object accept(FOLVisitor v, Object arg) 52 | { 53 | return v.visitNotSentence(this, arg); 54 | } 55 | 56 | public FOLNode copy() 57 | { 58 | return new NotSentence((Sentence)negated.copy()); 59 | } 60 | 61 | public Sentence copySentence() 62 | { 63 | return null; 64 | } 65 | 66 | // END-Sentence 67 | 68 | public override bool Equals(Object o) 69 | { 70 | 71 | if (this == o) 72 | { 73 | return true; 74 | } 75 | if ((o == null) || !(o is NotSentence)) 76 | { 77 | return false; 78 | } 79 | NotSentence ns = (NotSentence)o; 80 | return (ns.negated.Equals(negated)); 81 | } 82 | 83 | public override int GetHashCode() 84 | { 85 | if (0 == hashCode) 86 | { 87 | hashCode = 17; 88 | hashCode = 37 * hashCode + negated.GetHashCode(); 89 | } 90 | return hashCode; 91 | } 92 | 93 | public override String ToString() 94 | { 95 | if (null == stringRep) 96 | { 97 | StringBuilder sb = new StringBuilder(); 98 | sb.Append("NOT("); 99 | sb.Append(negated.ToString()); 100 | sb.Append(")"); 101 | stringRep = sb.ToString(); 102 | } 103 | return stringRep; 104 | } 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /aima-csharp/util/SetOps.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | namespace aima.core.util 6 | { 7 | /** 8 | * Note: This code is based on - Java Tutorial: The Set Interface
11 | * 12 | * Using LinkedHashSet, even though slightly slower than HashSet, in order to 13 | * ensure order is always respected (i.e. if called with TreeSet or 14 | * LinkedHashSet implementations). 15 | * 16 | * @author Ciaran O'Reilly 17 | * @author Ravi Mohan 18 | */ 19 | public class SetOps 20 | { 21 | /** 22 | * 23 | * @param 24 | * @param s1 25 | * @param s2 26 | * @return the union of s1 and s2. (The union of two sets is the set 27 | * containing all of the elements contained in either set.) 28 | */ 29 | public static List union(List s1, List s2) 30 | { 31 | if(s1 == s2) 32 | { 33 | return s1; 34 | } 35 | LinkedHashSet union = new LinkedHashSet(s1); 36 | union.UnionWith(s2); 37 | return union.ToList(); 38 | } 39 | 40 | /** 41 | * 42 | * @param 43 | * @param s1 44 | * @param s2 45 | * @return the intersection of s1 and s2. (The intersection of two sets is 46 | * the set containing only the elements common to both sets.) 47 | */ 48 | public static List intersection(List s1, List s2) 49 | { 50 | if (s1 == s2) 51 | { 52 | return s1; 53 | } 54 | LinkedHashSet intersection = new LinkedHashSet(s1); 55 | intersection.IntersectWith(s2); 56 | return intersection.ToList(); 57 | } 58 | 59 | /** 60 | * 61 | * @param 62 | * @param s1 63 | * @param s2 64 | * @return the (asymmetric) set difference of s1 and s2. (For example, the 65 | * set difference of s1 minus s2 is the set containing all of the 66 | * elements found in s1 but not in s2.) 67 | */ 68 | public static List difference(List s1, List s2) 69 | { 70 | LinkedHashSet difference = new LinkedHashSet(s1); 71 | difference.ExceptWith(s2); 72 | return difference.ToList(); 73 | } 74 | } 75 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/parsing/ast/Variable.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using aima.core.logic.fol.parsing; 4 | 5 | namespace aima.core.logic.fol.parsing.ast 6 | { 7 | /** 8 | * @author Ravi Mohan 9 | * @author Ciaran O'Reilly 10 | */ 11 | public class Variable : Term 12 | { 13 | private String value; 14 | private int hashCode = 0; 15 | private int indexical = -1; 16 | 17 | public Variable(String s) 18 | { 19 | value = s.Trim(); 20 | } 21 | 22 | public Variable(String s, int idx) 23 | { 24 | value = s.Trim(); 25 | indexical = idx; 26 | } 27 | 28 | public String getValue() 29 | { 30 | return value; 31 | } 32 | 33 | // START-Term 34 | 35 | public String getSymbolicName() 36 | { 37 | return getValue(); 38 | } 39 | 40 | public bool isCompound() 41 | { 42 | return false; 43 | } 44 | 45 | List FOLNode.getArgs() 46 | { 47 | return null; 48 | } 49 | 50 | public List getArgs() 51 | { 52 | // Is not Compound, therefore should 53 | // return null for its arguments 54 | return null; 55 | } 56 | 57 | public Object accept(FOLVisitor v, Object arg) 58 | { 59 | return v.visitVariable(this, arg); 60 | } 61 | 62 | FOLNode FOLNode.copy() 63 | { 64 | return copy(); 65 | } 66 | 67 | public Term copy() 68 | { 69 | return new Variable(value, indexical); 70 | } 71 | 72 | // END-Term 73 | 74 | public int getIndexical() 75 | { 76 | return indexical; 77 | } 78 | 79 | public void setIndexical(int idx) 80 | { 81 | indexical = idx; 82 | hashCode = 0; 83 | } 84 | 85 | public String getIndexedValue() 86 | { 87 | return value + indexical; 88 | } 89 | 90 | public override bool Equals(Object o) 91 | { 92 | 93 | if (this == o) 94 | { 95 | return true; 96 | } 97 | if (!(o is Variable)) 98 | { 99 | return false; 100 | } 101 | 102 | Variable v = (Variable)o; 103 | return v.getValue().Equals(getValue()) 104 | && v.getIndexical() == getIndexical(); 105 | } 106 | 107 | public override int GetHashCode() 108 | { 109 | if (0 == hashCode) 110 | { 111 | hashCode = 17; 112 | hashCode += indexical; 113 | hashCode = 37 * hashCode + value.GetHashCode(); 114 | } 115 | 116 | return hashCode; 117 | } 118 | 119 | public override String ToString() 120 | { 121 | return value; 122 | } 123 | } 124 | } -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /aima-csharp/agent/impl/aprog/SimpleReflexAgentProgram.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using aima.core.agent; 4 | using aima.core.agent.impl; 5 | using aima.core.agent.impl.aprog.simplerule; 6 | 7 | namespace aima.core.agent.impl.aprog 8 | { 9 | /** 10 | * Artificial Intelligence A Modern Approach (3rd Edition): Figure 2.10, page 11 | * 49.
12 | *
13 | * 14 | *
15 |      * function SIMPLE-RELEX-AGENT(percept) returns an action
16 |      *   persistent: rules, a set of condition-action rules
17 |      *        
18 |      * state  <- INTERPRET-INPUT(percept);
19 |      * rule   <- RULE-MATCH(state, rules);
20 |      * action <- rule.ACTION;
21 |      * return action
22 |      * 
23 | * Figure 2.10 A simple reflex agent. It acts according to a rule whose 24 | * condition matches the current state, as defined by the percept. 25 | * 26 | * @author Ciaran O'Reilly 27 | * @author Mike Stampone 28 | * 29 | */ 30 | public class SimpleReflexAgentProgram : AgentProgram 31 | { 32 | // persistent: rules, a set of condition-action rules 33 | private HashSet rules; 34 | 35 | /** 36 | * Constructs a SimpleReflexAgentProgram with a set of condition-action 37 | * rules. 38 | * 39 | * @param ruleSet 40 | * a set of condition-action rules 41 | */ 42 | public SimpleReflexAgentProgram(HashSet ruleSet) 43 | { 44 | rules = ruleSet; 45 | } 46 | 47 | // START-AgentProgram 48 | 49 | // function SIMPLE-RELEX-AGENT(percept) returns an action 50 | public Action execute(Percept percept) 51 | { 52 | // state <- INTERPRET-INPUT(percept); 53 | ObjectWithDynamicAttributes state = interpretInput(percept); 54 | // rule <- RULE-MATCH(state, rules); 55 | Rule rule = ruleMatch(state, rules); 56 | // action <- rule.ACTION; 57 | // return action 58 | return ruleAction(rule); 59 | } 60 | 61 | // END-AgentProgram 62 | 63 | // PROTECTED METHODS 64 | 65 | protected ObjectWithDynamicAttributes interpretInput(Percept p) 66 | { 67 | return (DynamicPercept)p; 68 | } 69 | 70 | protected Rule ruleMatch(ObjectWithDynamicAttributes state, 71 | HashSet rulesSet) 72 | { 73 | foreach (Rule r in rulesSet) 74 | { 75 | if (r.evaluate(state)) 76 | { 77 | return r; 78 | } 79 | } 80 | return null; 81 | } 82 | 83 | protected Action ruleAction(Rule r) 84 | { 85 | return null == r ? NoOpAction.NO_OP : r.getAction(); 86 | } 87 | } 88 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/parsing/AbstractFOLVisitor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using aima.core.logic.fol.parsing.ast; 4 | 5 | namespace aima.core.logic.fol.parsing 6 | { 7 | /** 8 | * @author Ravi Mohan 9 | * 10 | */ 11 | public class AbstractFOLVisitor : FOLVisitor 12 | { 13 | public AbstractFOLVisitor() 14 | { 15 | } 16 | 17 | protected Sentence recreate(Object ast) 18 | { 19 | return ((Sentence)ast).copySentence(); 20 | } 21 | 22 | public virtual Object visitVariable(Variable variable, Object arg) 23 | { 24 | return variable.copy(); 25 | } 26 | 27 | public virtual Object visitQuantifiedSentence(QuantifiedSentence sentence, 28 | Object arg) 29 | { 30 | List variables = new List(); 31 | foreach (Variable var in sentence.getVariables()) 32 | { 33 | variables.Add((Variable)var.accept(this, arg)); 34 | } 35 | 36 | return new QuantifiedSentence(sentence.getQuantifier(), variables, 37 | (Sentence)sentence.getQuantified().accept(this, arg)); 38 | } 39 | 40 | public Object visitPredicate(Predicate predicate, Object arg) 41 | { 42 | List terms = predicate.getTerms(); 43 | List newTerms = new List(); 44 | for (int i = 0; i < terms.Count; i++) 45 | { 46 | Term t = terms[i]; 47 | Term subsTerm = (Term)t.accept(this, arg); 48 | newTerms.Add(subsTerm); 49 | } 50 | return new Predicate(predicate.getPredicateName(), newTerms); 51 | } 52 | 53 | public Object visitTermEquality(TermEquality equality, Object arg) 54 | { 55 | Term newTerm1 = (Term)equality.getTerm1().accept(this, arg); 56 | Term newTerm2 = (Term)equality.getTerm2().accept(this, arg); 57 | return new TermEquality(newTerm1, newTerm2); 58 | } 59 | 60 | public Object visitConstant(Constant constant, Object arg) 61 | { 62 | return constant; 63 | } 64 | 65 | public Object visitFunction(Function function, Object arg) 66 | { 67 | List terms = function.getTerms(); 68 | List newTerms = new List(); 69 | for (int i = 0; i < terms.Count; i++) 70 | { 71 | Term t = terms[i]; 72 | Term subsTerm = (Term)t.accept(this, arg); 73 | newTerms.Add(subsTerm); 74 | } 75 | return new Function(function.getFunctionName(), newTerms); 76 | } 77 | 78 | public Object visitNotSentence(NotSentence sentence, Object arg) 79 | { 80 | return new NotSentence((Sentence)sentence.getNegated().accept(this, 81 | arg)); 82 | } 83 | 84 | public Object visitConnectedSentence(ConnectedSentence sentence, Object arg) 85 | { 86 | Sentence substFirst = (Sentence)sentence.getFirst().accept(this, arg); 87 | Sentence substSecond = (Sentence)sentence.getSecond() 88 | .accept(this, arg); 89 | return new ConnectedSentence(sentence.getConnector(), substFirst, 90 | substSecond); 91 | } 92 | } 93 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/kb/data/Literal.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Text; 4 | using aima.core.logic.fol.parsing.ast; 5 | 6 | namespace aima.core.logic.fol.kb.data 7 | { 8 | /** 9 | * Artificial Intelligence A Modern Approach (3rd Edition): page 244.
10 | *
11 | * A literal is either an atomic sentence (a positive literal) or a negated 12 | * atomic sentence (a negative literal). 13 | * 14 | * @author Ciaran O'Reilly 15 | * 16 | */ 17 | public class Literal 18 | { 19 | private AtomicSentence atom = null; 20 | private bool negativeLiteral = false; 21 | private String strRep = null; 22 | private int hashCode = 0; 23 | 24 | public Literal(AtomicSentence atom) 25 | { 26 | this.atom = atom; 27 | } 28 | 29 | public Literal(AtomicSentence atom, bool negated) 30 | { 31 | this.atom = atom; 32 | this.negativeLiteral = negated; 33 | } 34 | 35 | public virtual Literal newInstance(AtomicSentence atom) 36 | { 37 | return new Literal(atom, negativeLiteral); 38 | } 39 | 40 | public bool isPositiveLiteral() 41 | { 42 | return !negativeLiteral; 43 | } 44 | 45 | public bool isNegativeLiteral() 46 | { 47 | return negativeLiteral; 48 | } 49 | 50 | public AtomicSentence getAtomicSentence() 51 | { 52 | return atom; 53 | } 54 | 55 | public override String ToString() 56 | { 57 | if (null == strRep) 58 | { 59 | StringBuilder sb = new StringBuilder(); 60 | if (isNegativeLiteral()) 61 | { 62 | sb.Append("~"); 63 | } 64 | sb.Append(getAtomicSentence().ToString()); 65 | strRep = sb.ToString(); 66 | } 67 | 68 | return strRep; 69 | } 70 | 71 | public override bool Equals(Object o) 72 | { 73 | 74 | if (this == o) 75 | { 76 | return true; 77 | } 78 | if (o.GetType() != this.GetType()) 79 | { 80 | // This prevents ReducedLiterals 81 | // being treated as equivalent to 82 | // normal Literals. 83 | return false; 84 | } 85 | if (!(o is Literal)) 86 | { 87 | return false; 88 | } 89 | Literal l = (Literal)o; 90 | return l.isPositiveLiteral() == isPositiveLiteral() 91 | && l.getAtomicSentence().getSymbolicName().Equals( 92 | atom.getSymbolicName()) 93 | && l.getAtomicSentence().getArgs().Equals(atom.getArgs()); 94 | } 95 | 96 | public override int GetHashCode() 97 | { 98 | if (0 == hashCode) 99 | { 100 | hashCode = 17; 101 | hashCode = 37 * hashCode + this.GetType().Name.GetHashCode() 102 | + (isPositiveLiteral() ? "+".GetHashCode() : "-".GetHashCode()) 103 | + atom.getSymbolicName().GetHashCode(); 104 | foreach (Term t in atom.getArgs()) 105 | { 106 | hashCode = 37 * hashCode + t.GetHashCode(); 107 | } 108 | } 109 | return hashCode; 110 | } 111 | } 112 | } -------------------------------------------------------------------------------- /aima-csharp/environment/map/MapFunctionFactory.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using aima.core.agent; 3 | using aima.core.agent.impl; 4 | using aima.core.search.framework; 5 | using aima.core.search.framework.problem; 6 | using aima.core.util; 7 | 8 | namespace aima.core.environment.map 9 | { 10 | /** 11 | * @author Ciaran O'Reilly 12 | * 13 | */ 14 | public class MapFunctionFactory 15 | { 16 | private static ResultFunction resultFunction; 17 | private static PerceptToStateFunction perceptToStateFunction; 18 | 19 | public static ActionsFunction getActionsFunction(Map map) 20 | { 21 | return new MapActionsFunction(map, false); 22 | } 23 | 24 | public static ActionsFunction getReverseActionsFunction(Map map) 25 | { 26 | return new MapActionsFunction(map, true); 27 | } 28 | 29 | public static ResultFunction getResultFunction() 30 | { 31 | if (null == resultFunction) 32 | { 33 | resultFunction = new MapResultFunction(); 34 | } 35 | return resultFunction; 36 | } 37 | 38 | private class MapActionsFunction : ActionsFunction 39 | { 40 | private Map map = null; 41 | private bool reverseMode; 42 | 43 | public MapActionsFunction(Map map, bool reverseMode) 44 | { 45 | this.map = map; 46 | this.reverseMode = reverseMode; 47 | } 48 | 49 | public HashSet actions(System.Object state) 50 | { 51 | HashSet actions = new HashSet(); 52 | System.String location = state.ToString(); 53 | 54 | List linkedLocations = reverseMode ? map.getPossiblePrevLocations(location) 55 | : map.getPossibleNextLocations(location); 56 | foreach (System.String linkLoc in linkedLocations) 57 | { 58 | actions.Add(new MoveToAction(linkLoc)); 59 | } 60 | return actions; 61 | } 62 | } 63 | 64 | public static PerceptToStateFunction getPerceptToStateFunction() 65 | { 66 | if (null == perceptToStateFunction) 67 | { 68 | perceptToStateFunction = new MapPerceptToStateFunction(); 69 | } 70 | return perceptToStateFunction; 71 | } 72 | 73 | private class MapResultFunction : ResultFunction 74 | { 75 | public MapResultFunction() 76 | { 77 | } 78 | 79 | public System.Object result(System.Object s, Action a) 80 | { 81 | 82 | if (a is MoveToAction) 83 | { 84 | MoveToAction mta = (MoveToAction)a; 85 | 86 | return mta.getToLocation(); 87 | } 88 | 89 | // The Action is not understood or is a NoOp 90 | // the result will be the current state. 91 | return s; 92 | } 93 | } 94 | 95 | private class MapPerceptToStateFunction : 96 | PerceptToStateFunction 97 | { 98 | public System.Object getState(Percept p) 99 | { 100 | return ((DynamicPercept)p) 101 | .getAttribute(DynAttributeNames.PERCEPT_IN); 102 | } 103 | } 104 | } 105 | } -------------------------------------------------------------------------------- /aima-csharp/search/framework/SearchUtils.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using aima.core.agent; 3 | using aima.core.agent.impl; 4 | using aima.core.search.framework.problem; 5 | 6 | namespace aima.core.search.framework 7 | { 8 | /** 9 | * Provides several useful static methods for implementing search. 10 | * 11 | * @author Ravi Mohan 12 | * @author Ruediger Lunde 13 | * 14 | */ 15 | public class SearchUtils 16 | { 17 | /** 18 | * Returns the list of actions corresponding to the complete path to the 19 | * given node or NoOp if path length is one. 20 | */ 21 | public static List getSequenceOfActions(Node node) 22 | { 23 | List nodes = node.GetPathFromRoot(); 24 | List actions = new List(); 25 | 26 | if(nodes.Count == 1) 27 | { 28 | // I'm at the root node, this indicates I started at the 29 | // Goal node, therefore just return a NoOp 30 | actions.Add(NoOpAction.NO_OP); 31 | } 32 | else 33 | { 34 | // ignore the root node this has no action 35 | // hence index starts from 1 not zero 36 | for (int i = 1; i < nodes.Count; i++) 37 | { 38 | Node node_temp = nodes[i]; 39 | actions.Add(node_temp.Action); 40 | } 41 | } 42 | return actions; 43 | } 44 | 45 | /** Returns an empty action list. */ 46 | public static List failure() 47 | { 48 | return new List(); 49 | } 50 | 51 | /** Checks whether a list of actions is empty. */ 52 | public static bool isFailure(List actions) 53 | { 54 | if(actions.Count == 0) 55 | { 56 | return true; 57 | } 58 | else 59 | { 60 | return false; 61 | } 62 | 63 | } 64 | 65 | 66 | /** 67 | * Calls the goal test of the problem and - if the goal test is effectively 68 | * a {@link SolutionChecker} - additionally checks, whether the solution is 69 | * acceptable. Solution checkers can be used to analyze several or all 70 | * solutions with only one search run. 71 | */ 72 | public static bool isGoalState(Problem p, Node n) 73 | { 74 | bool isGoal = false; 75 | GoalTest gt = p.getGoalTest(); 76 | if (gt.isGoalState(n.State)) 77 | { 78 | if (gt is SolutionChecker) 79 | { 80 | isGoal = ((SolutionChecker)gt).isAcceptableSolution( 81 | getSequenceOfActions(n), n.State); 82 | } 83 | else 84 | { 85 | isGoal = true; 86 | } 87 | } 88 | return isGoal; 89 | } 90 | } 91 | } -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | How to Contribute to aima-csharp 2 | ========================== 3 | 4 | Thanks for considering contributing to `aima-csharp`! Whether you are an aspiring [Google Summer of Code](https://summerofcode.withgoogle.com/organizations/5663121491361792/) student, or an independent contributor, here is a guide to how you can help: 5 | 6 | ## Read the Code and Start on an Issue 7 | 8 | - First, read and understand the code to get a feel for the extent and the style. 9 | - Look at the [issues](https://github.com/aimacode/aima-csharp/issues) and pick one to work on. 10 | - One of the issues is that some algorithms are missing from the [list of algorithms](/README.md#index-of-implemented-algorithms). 11 | 12 | ## New and Improved Algorithms 13 | 14 | - Implement functions that were in the third edition of the book but were not yet implemented in the code. Check the [list of pseudocode algorithms (pdf)](https://github.com/aimacode/pseudocode/blob/master/aima3e-algorithms.pdf) to see what's missing. 15 | - As we finish chapters for the new fourth edition, we will share the new pseudocode in the [`aima-pseudocode`](https://github.com/aimacode/aima-pseudocode) repository, and describe what changes are necessary. 16 | We hope to have a `algorithm-name.md` file for each algorithm, eventually; it would be great if contributors could add some for the existing algorithms. 17 | 18 | 19 | Contributing a Patch 20 | ==================== 21 | 22 | 1. Submit an issue describing your proposed change to the repo in question (or work on an existing issue). 23 | 1. The repo owner will respond to your issue promptly. 24 | 1. Fork the desired repo, develop and test your code changes. 25 | 1. Submit a pull request. 26 | 27 | Reporting Issues 28 | ================ 29 | 30 | - Under which versions of Visual Studio does this happen? 31 | 32 | - Is anybody working on this? 33 | 34 | # Choice of Programming Languages 35 | 36 | Are we right to concentrate on Java and Python versions of the code? I think so; both languages are popular; Java is 37 | fast enough for our purposes, and has reasonable type declarations (but can be verbose); Python is popular and has a very direct mapping to the pseudocode in the book (but lacks type declarations and can be slow). The [TIOBE Index](http://www.tiobe.com/tiobe_index) says the top seven most popular languages, in order, are: 38 | 39 | Java, C, C++, C#, Python, PHP, Javascript 40 | 41 | So it might be reasonable to also support C++/C# at some point in the future. It might also be reasonable to support a language that combines the terse readability of Python with the type safety and speed of Java; perhaps Go or Julia. I see no reason to support PHP. Javascript is the language of the browser; it would be nice to have code that runs in the browser without need for any downloads; this would be in Javascript or a variant such as Typescript. 42 | 43 | There is also a `aima-lisp` project; in 1995 when we wrote the first edition of the book, Lisp was the right choice, but today it is less popular (currently #31 on the TIOBE index). 44 | -------------------------------------------------------------------------------- /aima-csharp/logic/fol/inference/proof/ProofFinal.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using aima.core.logic.fol.parsing.ast; 4 | 5 | namespace aima.core.logic.fol.inference.proof 6 | { 7 | /** 8 | * @author Ciaran O'Reilly 9 | * 10 | */ 11 | public class ProofFinal : Proof 12 | { 13 | private Dictionary answerBindings = new Dictionary(); 14 | private ProofStep finalStep = null; 15 | private List proofSteps = null; 16 | 17 | public ProofFinal(ProofStep finalStep, Dictionary answerBindings) 18 | { 19 | this.finalStep = finalStep; 20 | this.answerBindings = answerBindings; 21 | } 22 | 23 | // START-Proof 24 | 25 | public List getSteps() 26 | { 27 | // Only calculate if the proof steps are actually requested. 28 | if (null == proofSteps) 29 | { 30 | calculateProofSteps(); 31 | } 32 | return proofSteps; 33 | } 34 | 35 | public Dictionary getAnswerBindings() 36 | { 37 | return answerBindings; 38 | } 39 | 40 | public void replaceAnswerBindings(Dictionary updatedBindings) 41 | { 42 | answerBindings.Clear(); 43 | answerBindings = updatedBindings; 44 | } 45 | 46 | // END-Proof 47 | 48 | public override String ToString() 49 | { 50 | return answerBindings.ToString(); 51 | } 52 | 53 | 54 | // PRIVATE METHODS 55 | 56 | private void calculateProofSteps() 57 | { 58 | proofSteps = new List(); 59 | addToProofSteps(finalStep); 60 | 61 | // Move all premises to the front of the 62 | // list of steps 63 | int to = 0; 64 | for (int i = 0; i < proofSteps.Count; i++) 65 | { 66 | if (proofSteps[i] is ProofStepPremise) 67 | { 68 | ProofStep m = proofSteps[i]; 69 | proofSteps.RemoveAt(i); 70 | proofSteps.Insert(to, m); 71 | to++; 72 | } 73 | } 74 | 75 | // Move the Goals after the premises 76 | for (int i = 0; i < proofSteps.Count; i++) 77 | { 78 | if (proofSteps[i] is ProofStepGoal) 79 | { 80 | ProofStep m = proofSteps[i]; 81 | proofSteps.RemoveAt(i); 82 | proofSteps.Insert(to, m); 83 | to++; 84 | } 85 | } 86 | 87 | // Assign the step #s now that all the proof 88 | // steps have been unwound 89 | for (int i = 0; i < proofSteps.Count; i++) 90 | { 91 | proofSteps[i].setStepNumber(i + 1); 92 | } 93 | } 94 | 95 | private void addToProofSteps(ProofStep step) 96 | { 97 | if (!proofSteps.Contains(step)) 98 | { 99 | proofSteps.Insert(0, step); 100 | } 101 | else 102 | { 103 | proofSteps.Remove(step); 104 | proofSteps.Insert(0, step); 105 | } 106 | List predecessors = step.getPredecessorSteps(); 107 | for (int i = predecessors.Count - 1; i >= 0; i--) 108 | { 109 | addToProofSteps(predecessors[i]); 110 | } 111 | } 112 | } 113 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/parsing/ast/TermEquality.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Linq; 5 | using System.Collections.ObjectModel; 6 | using aima.core.logic.fol.parsing; 7 | 8 | namespace aima.core.logic.fol.parsing.ast 9 | { 10 | /** 11 | * @author Ravi Mohan 12 | * @author Ciaran O'Reilly 13 | */ 14 | public class TermEquality : AtomicSentence 15 | { 16 | private Term term1, term2; 17 | private List terms = new List(); 18 | private String stringRep = null; 19 | private int hashCode = 0; 20 | 21 | public static String getEqualitySynbol() 22 | { 23 | return "="; 24 | } 25 | 26 | public TermEquality(Term term1, Term term2) 27 | { 28 | this.term1 = term1; 29 | this.term2 = term2; 30 | terms.Add(term1); 31 | terms.Add(term2); 32 | } 33 | 34 | public Term getTerm1() 35 | { 36 | return term1; 37 | } 38 | 39 | public Term getTerm2() 40 | { 41 | return term2; 42 | } 43 | 44 | // START-AtomicSentence 45 | 46 | public String getSymbolicName() 47 | { 48 | return getEqualitySynbol(); 49 | } 50 | 51 | public bool isCompound() 52 | { 53 | return true; 54 | } 55 | 56 | List AtomicSentence.getArgs() 57 | { 58 | return null; 59 | } 60 | 61 | AtomicSentence AtomicSentence.copy() 62 | { 63 | return null; 64 | } 65 | 66 | public List getArgs() 67 | { 68 | return new ReadOnlyCollection(terms).ToList(); 69 | } 70 | 71 | public Object accept(FOLVisitor v, Object arg) 72 | { 73 | return v.visitTermEquality(this, arg); 74 | } 75 | 76 | public FOLNode copy() 77 | { 78 | return new TermEquality((Term)term1.copy(), (Term)term2.copy()); 79 | } 80 | 81 | public Sentence copySentence() 82 | { 83 | return null; 84 | } 85 | 86 | // END-AtomicSentence 87 | 88 | public override bool Equals(Object o) 89 | { 90 | 91 | if (this == o) 92 | { 93 | return true; 94 | } 95 | if ((o == null) || !(o is TermEquality)) 96 | { 97 | return false; 98 | } 99 | TermEquality te = (TermEquality)o; 100 | 101 | return te.getTerm1().Equals(term1) && te.getTerm2().Equals(term2); 102 | } 103 | 104 | public override int GetHashCode() 105 | { 106 | 107 | if (0 == hashCode) 108 | { 109 | hashCode = 17; 110 | hashCode = 37 * hashCode + getTerm1().GetHashCode(); 111 | hashCode = 37 * hashCode + getTerm2().GetHashCode(); 112 | } 113 | return hashCode; 114 | } 115 | 116 | public override String ToString() 117 | { 118 | if (null == stringRep) 119 | { 120 | StringBuilder sb = new StringBuilder(); 121 | sb.Append(term1.ToString()); 122 | sb.Append(" = "); 123 | sb.Append(term2.ToString()); 124 | stringRep = sb.ToString(); 125 | } 126 | return stringRep; 127 | } 128 | } 129 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/inference/proof/ProofStepClauseBinaryResolvent.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Collections.ObjectModel; 5 | using aima.core.logic.fol.kb.data; 6 | using aima.core.logic.fol.parsing.ast; 7 | 8 | namespace aima.core.logic.fol.inference.proof 9 | { 10 | /** 11 | * @author Ciaran O'Reilly 12 | * 13 | */ 14 | public class ProofStepClauseBinaryResolvent : AbstractProofStep 15 | { 16 | private List predecessors = new List(); 17 | private Clause resolvent = null; 18 | private Literal posLiteral = null; 19 | private Literal negLiteral = null; 20 | private Clause parent1, parent2 = null; 21 | private Dictionary subst = new Dictionary(); 22 | private Dictionary renameSubst = new Dictionary(); 23 | private Clause c; 24 | private Clause clause; 25 | private Clause othC; 26 | private Dictionary copyRBindings; 27 | private Dictionary renameSubstitituon; 28 | 29 | public ProofStepClauseBinaryResolvent(Clause resolvent, Literal pl, 30 | Literal nl, Clause parent1, Clause parent2, 31 | Dictionary subst, Dictionary renameSubst) 32 | { 33 | this.resolvent = resolvent; 34 | this.posLiteral = pl; 35 | this.negLiteral = nl; 36 | this.parent1 = parent1; 37 | this.parent2 = parent2; 38 | 39 | foreach (Variable key in subst.Keys) 40 | { 41 | this.subst.Add(key, subst[key]); 42 | } 43 | 44 | foreach (Variable key in renameSubst.Keys) 45 | { 46 | this.renameSubst.Add(key, renameSubst[key]); 47 | } 48 | 49 | this.predecessors.Add(parent1.getProofStep()); 50 | this.predecessors.Add(parent2.getProofStep()); 51 | } 52 | 53 | public ProofStepClauseBinaryResolvent(Clause c, Clause clause, Clause othC, Dictionary copyRBindings, Dictionary renameSubstitituon) 54 | { 55 | this.c = c; 56 | this.clause = clause; 57 | this.othC = othC; 58 | this.copyRBindings = copyRBindings; 59 | this.renameSubstitituon = renameSubstitituon; 60 | } 61 | 62 | // START-ProofStep 63 | 64 | public override List getPredecessorSteps() 65 | { 66 | return new ReadOnlyCollection(predecessors).ToList(); 67 | } 68 | 69 | public override String getProof() 70 | { 71 | return resolvent.ToString(); 72 | } 73 | 74 | public override String getJustification() 75 | { 76 | int lowStep = parent1.getProofStep().getStepNumber(); 77 | int highStep = parent2.getProofStep().getStepNumber(); 78 | 79 | if (lowStep > highStep) 80 | { 81 | lowStep = highStep; 82 | highStep = parent1.getProofStep().getStepNumber(); 83 | } 84 | 85 | return "Resolution: " + lowStep + ", " + highStep + " [" + posLiteral 86 | + ", " + negLiteral + "], subst=" + subst + ", renaming=" 87 | + renameSubst; 88 | } 89 | 90 | // END-ProofStep 91 | } 92 | } -------------------------------------------------------------------------------- /aima-csharp/environment/eightpuzzle/EightPuzzleFunctionFactory.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using aima.core.agent; 3 | using aima.core.search.framework.problem; 4 | using aima.core.util; 5 | 6 | namespace aima.core.environment.eightpuzzle 7 | { 8 | /** 9 | * @author Ravi Mohan 10 | * @author Ciaran O'Reilly 11 | */ 12 | public class EightPuzzleFunctionFactory 13 | { 14 | private static ActionsFunction _actionsFunction = null; 15 | private static ResultFunction _resultFunction = null; 16 | 17 | public static ActionsFunction getActionsFunction() 18 | { 19 | if (null == _actionsFunction) 20 | { 21 | _actionsFunction = new EPActionsFunction(); 22 | } 23 | return _actionsFunction; 24 | } 25 | 26 | public static ResultFunction getResultFunction() 27 | { 28 | if (null == _resultFunction) 29 | { 30 | _resultFunction = new EPResultFunction(); 31 | } 32 | return _resultFunction; 33 | } 34 | 35 | private class EPActionsFunction : ActionsFunction 36 | { 37 | public HashSet actions(System.Object state) 38 | { 39 | EightPuzzleBoard board = (EightPuzzleBoard)state; 40 | 41 | HashSet actions = new HashSet(); 42 | 43 | if (board.canMoveGap(EightPuzzleBoard.UP)) 44 | { 45 | actions.Add(EightPuzzleBoard.UP); 46 | } 47 | if (board.canMoveGap(EightPuzzleBoard.DOWN)) 48 | { 49 | actions.Add(EightPuzzleBoard.DOWN); 50 | } 51 | if (board.canMoveGap(EightPuzzleBoard.LEFT)) 52 | { 53 | actions.Add(EightPuzzleBoard.LEFT); 54 | } 55 | if (board.canMoveGap(EightPuzzleBoard.RIGHT)) 56 | { 57 | actions.Add(EightPuzzleBoard.RIGHT); 58 | } 59 | 60 | return actions; 61 | } 62 | } 63 | 64 | private class EPResultFunction : ResultFunction 65 | { 66 | public System.Object result(System.Object s, Action a) 67 | { 68 | EightPuzzleBoard board = (EightPuzzleBoard) s; 69 | 70 | if (EightPuzzleBoard.UP.Equals(a) 71 | && board.canMoveGap(EightPuzzleBoard.UP)) 72 | { 73 | EightPuzzleBoard newBoard = new EightPuzzleBoard(board); 74 | newBoard.moveGapUp(); 75 | return newBoard; 76 | } 77 | else if (EightPuzzleBoard.DOWN.Equals(a) 78 | && board.canMoveGap(EightPuzzleBoard.DOWN)) 79 | { 80 | EightPuzzleBoard newBoard = new EightPuzzleBoard(board); 81 | newBoard.moveGapDown(); 82 | return newBoard; 83 | } 84 | else if (EightPuzzleBoard.LEFT.Equals(a) 85 | && board.canMoveGap(EightPuzzleBoard.LEFT)) 86 | { 87 | EightPuzzleBoard newBoard = new EightPuzzleBoard(board); 88 | newBoard.moveGapLeft(); 89 | return newBoard; 90 | } 91 | else if (EightPuzzleBoard.RIGHT.Equals(a) 92 | && board.canMoveGap(EightPuzzleBoard.RIGHT)) 93 | { 94 | EightPuzzleBoard newBoard = new EightPuzzleBoard(board); 95 | newBoard.moveGapRight(); 96 | return newBoard; 97 | } 98 | 99 | // The Action is not understood or is a NoOp 100 | // the result will be the current state. 101 | return s; 102 | } 103 | } 104 | } 105 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/SubstVisitor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using aima.core.logic.fol.kb.data; 4 | using aima.core.logic.fol.parsing; 5 | using aima.core.logic.fol.parsing.ast; 6 | 7 | namespace aima.core.logic.fol 8 | { 9 | /** 10 | * @author Ravi Mohan 11 | * @author Ciaran O'Reilly 12 | */ 13 | public class SubstVisitor : AbstractFOLVisitor 14 | { 15 | public SubstVisitor() 16 | { 17 | 18 | } 19 | 20 | /** 21 | * Note: Refer to Artificial Intelligence A Modern Approach (3rd Edition): 22 | * page 323. 23 | * 24 | * @param theta 25 | * a substitution. 26 | * @param sentence 27 | * the substitution has been applied to. 28 | * @return a new Sentence representing the result of applying the 29 | * substitution theta to aSentence. 30 | * 31 | */ 32 | public Sentence subst(Dictionary theta, Sentence sentence) 33 | { 34 | return (Sentence)sentence.accept(this, theta); 35 | } 36 | 37 | public Term subst(Dictionary theta, Term aTerm) 38 | { 39 | return (Term)aTerm.accept(this, theta); 40 | } 41 | 42 | public Function subst(Dictionary theta, Function function) 43 | { 44 | return (Function)function.accept(this, theta); 45 | } 46 | 47 | public Literal subst(Dictionary theta, Literal literal) 48 | { 49 | return literal.newInstance((AtomicSentence)literal 50 | .getAtomicSentence().accept(this, theta)); 51 | } 52 | 53 | public override Object visitVariable(Variable variable, Object arg) 54 | { 55 | Dictionary substitution = (Dictionary)arg; 56 | if (substitution.ContainsKey(variable)) 57 | { 58 | return substitution[variable].copy(); 59 | } 60 | return variable.copy(); 61 | } 62 | 63 | public override Object visitQuantifiedSentence(QuantifiedSentence sentence, 64 | Object arg) 65 | { 66 | 67 | Dictionary substitution = (Dictionary)arg; 68 | 69 | Sentence quantified = sentence.getQuantified(); 70 | Sentence quantifiedAfterSubs = (Sentence)quantified.accept(this, arg); 71 | 72 | List variables = new List(); 73 | foreach (Variable v in sentence.getVariables()) 74 | { 75 | if (substitution.ContainsKey(v)) 76 | { 77 | Term st = substitution[v]; 78 | if (st is Variable) 79 | { 80 | // Only if it is a variable to I replace it, otherwise 81 | // I drop it. 82 | variables.Add((Variable)st.copy()); 83 | } 84 | } 85 | else 86 | { 87 | // No substitution for the quantified variable, so 88 | // keep it. 89 | variables.Add((Variable)v.copy()); 90 | } 91 | } 92 | 93 | // If not variables remaining on the quantifier, then drop it 94 | if (variables.Count == 0) 95 | { 96 | return quantifiedAfterSubs; 97 | } 98 | 99 | return new QuantifiedSentence(sentence.getQuantifier(), variables, 100 | quantifiedAfterSubs); 101 | } 102 | } 103 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/parsing/ast/ConnectedSentence.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using aima.core.logic.fol.parsing; 5 | 6 | namespace aima.core.logic.fol.parsing.ast 7 | { 8 | /** 9 | * @author Ravi Mohan 10 | * @author Ciaran O'Reilly 11 | */ 12 | public class ConnectedSentence : Sentence 13 | { 14 | private String connector; 15 | private Sentence first, second; 16 | private List args = new List(); 17 | private String stringRep = null; 18 | private int hashCode = 0; 19 | 20 | public ConnectedSentence(String connector, Sentence first, Sentence second) 21 | { 22 | this.connector = connector; 23 | this.first = first; 24 | this.second = second; 25 | args.Add(first); 26 | args.Add(second); 27 | } 28 | 29 | public String getConnector() 30 | { 31 | return connector; 32 | } 33 | 34 | public Sentence getFirst() 35 | { 36 | return first; 37 | } 38 | 39 | public Sentence getSecond() 40 | { 41 | return second; 42 | } 43 | 44 | // START-Sentence 45 | 46 | public String getSymbolicName() 47 | { 48 | return getConnector(); 49 | } 50 | 51 | public bool isCompound() 52 | { 53 | return true; 54 | } 55 | 56 | public List getArgs() 57 | { 58 | Sentence[] copy = new Sentence[args.Count]; 59 | args.CopyTo(copy); 60 | return new List(copy); 61 | } 62 | 63 | public Object accept(FOLVisitor v, Object arg) 64 | { 65 | return v.visitConnectedSentence(this, arg); 66 | } 67 | 68 | public FOLNode copy() 69 | { 70 | return null; 71 | } 72 | 73 | public Sentence copySentence() 74 | { 75 | return new ConnectedSentence(connector, first.copySentence(), second.copySentence()); 76 | } 77 | 78 | // END-Sentence 79 | 80 | public override bool Equals(Object o) 81 | { 82 | 83 | if (this == o) 84 | { 85 | return true; 86 | } 87 | if ((o == null) || !(o is ConnectedSentence)) 88 | { 89 | return false; 90 | } 91 | ConnectedSentence cs = (ConnectedSentence)o; 92 | return cs.getConnector().Equals(getConnector()) 93 | && cs.getFirst().Equals(getFirst()) 94 | && cs.getSecond().Equals(getSecond()); 95 | } 96 | 97 | public override int GetHashCode() 98 | { 99 | if (0 == hashCode) 100 | { 101 | hashCode = 17; 102 | hashCode = 37 * hashCode + getConnector().GetHashCode(); 103 | hashCode = 37 * hashCode + getFirst().GetHashCode(); 104 | hashCode = 37 * hashCode + getSecond().GetHashCode(); 105 | } 106 | return hashCode; 107 | } 108 | 109 | public override String ToString() 110 | { 111 | if (null == stringRep) 112 | { 113 | StringBuilder sb = new StringBuilder(); 114 | sb.Append("("); 115 | sb.Append(first.ToString()); 116 | sb.Append(" "); 117 | sb.Append(connector); 118 | sb.Append(" "); 119 | sb.Append(second.ToString()); 120 | sb.Append(")"); 121 | stringRep = sb.ToString(); 122 | } 123 | return stringRep; 124 | } 125 | } 126 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/parsing/ast/Function.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using aima.core.logic.fol.parsing; 5 | 6 | namespace aima.core.logic.fol.parsing.ast 7 | { 8 | /** 9 | * @author Ravi Mohan 10 | * @author Ciaran O'Reilly 11 | */ 12 | public class Function : Term 13 | { 14 | private String functionName; 15 | private List terms = new List(); 16 | private String stringRep = null; 17 | private int hashCode = 0; 18 | 19 | public Function(String functionName, List terms) 20 | { 21 | this.functionName = functionName; 22 | this.terms.AddRange(terms); 23 | } 24 | 25 | public String getFunctionName() 26 | { 27 | return functionName; 28 | } 29 | 30 | public List getTerms() 31 | { 32 | Term[] copy = new Term[terms.Count]; 33 | terms.CopyTo(copy); 34 | return new List(copy); 35 | } 36 | 37 | // START-Term 38 | 39 | public String getSymbolicName() 40 | { 41 | return getFunctionName(); 42 | } 43 | 44 | public bool isCompound() 45 | { 46 | return true; 47 | } 48 | 49 | List FOLNode.getArgs() 50 | { 51 | return null; 52 | } 53 | 54 | public List getArgs() 55 | { 56 | return getTerms(); 57 | } 58 | 59 | public Object accept(FOLVisitor v, Object arg) 60 | { 61 | return v.visitFunction(this, arg); 62 | } 63 | 64 | FOLNode FOLNode.copy() 65 | { 66 | return copy(); 67 | } 68 | 69 | public Term copy() 70 | { 71 | List copyTerms = new List(); 72 | foreach (Term t in terms) 73 | { 74 | copyTerms.Add(t.copy()); 75 | } 76 | return new Function(functionName, copyTerms); 77 | } 78 | 79 | // END-Term 80 | 81 | public override bool Equals(Object o) 82 | { 83 | 84 | if (this == o) 85 | { 86 | return true; 87 | } 88 | if (!(o is Function)) 89 | { 90 | return false; 91 | } 92 | 93 | Function f = (Function)o; 94 | 95 | return f.getFunctionName().Equals(getFunctionName()) 96 | && f.getTerms().Equals(getTerms()); 97 | } 98 | 99 | public override int GetHashCode() 100 | { 101 | if (0 == hashCode) 102 | { 103 | hashCode = 17; 104 | hashCode = 37 * hashCode + functionName.GetHashCode(); 105 | foreach (Term t in terms) 106 | { 107 | hashCode = 37 * hashCode + t.GetHashCode(); 108 | } 109 | } 110 | return hashCode; 111 | } 112 | 113 | public override String ToString() 114 | { 115 | if (null == stringRep) 116 | { 117 | StringBuilder sb = new StringBuilder(); 118 | sb.Append(functionName); 119 | sb.Append("("); 120 | 121 | bool first = true; 122 | foreach (Term t in terms) 123 | { 124 | if (first) 125 | { 126 | first = false; 127 | } 128 | else 129 | { 130 | sb.Append(","); 131 | } 132 | sb.Append(t.ToString()); 133 | } 134 | 135 | sb.Append(")"); 136 | 137 | stringRep = sb.ToString(); 138 | } 139 | return stringRep; 140 | } 141 | } 142 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/domain/DomainFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace aima.core.logic.fol.domain 4 | { 5 | /** 6 | * @author Ravi Mohan 7 | * 8 | */ 9 | public class DomainFactory 10 | { 11 | public static FOLDomain crusadesDomain() 12 | { 13 | FOLDomain domain = new FOLDomain(); 14 | domain.addConstant("John"); 15 | domain.addConstant("Richard"); 16 | domain.addConstant("England"); 17 | domain.addConstant("Saladin"); 18 | domain.addConstant("Crown"); 19 | 20 | domain.addFunction("LeftLegOf"); 21 | domain.addFunction("BrotherOf"); 22 | domain.addFunction("EnemyOf"); 23 | domain.addFunction("LegsOf"); 24 | 25 | domain.addPredicate("King"); 26 | return domain; 27 | } 28 | 29 | public static FOLDomain knowsDomain() 30 | { 31 | FOLDomain domain = new FOLDomain(); 32 | domain.addConstant("John"); 33 | domain.addConstant("Jane"); 34 | domain.addConstant("Bill"); 35 | domain.addConstant("Elizabeth"); 36 | domain.addFunction("Mother"); 37 | domain.addPredicate("Knows"); 38 | 39 | return domain; 40 | } 41 | 42 | public static FOLDomain weaponsDomain() 43 | { 44 | 45 | FOLDomain domain = new FOLDomain(); 46 | domain.addConstant("West"); 47 | domain.addConstant("America"); 48 | domain.addConstant("M1"); 49 | domain.addConstant("Nono"); 50 | domain.addPredicate("American"); 51 | domain.addPredicate("Weapon"); 52 | domain.addPredicate("Sells"); 53 | domain.addPredicate("Hostile"); 54 | domain.addPredicate("Criminal"); 55 | domain.addPredicate("Missile"); 56 | domain.addPredicate("Owns"); 57 | domain.addPredicate("Enemy"); 58 | 59 | return domain; 60 | } 61 | 62 | public static FOLDomain kingsDomain() 63 | { 64 | FOLDomain domain = new FOLDomain(); 65 | domain.addConstant("John"); 66 | domain.addConstant("Richard"); 67 | domain.addPredicate("King"); 68 | domain.addPredicate("Greedy"); 69 | domain.addPredicate("Evil"); 70 | 71 | return domain; 72 | } 73 | 74 | public static FOLDomain lovesAnimalDomain() 75 | { 76 | FOLDomain domain = new FOLDomain(); 77 | domain.addPredicate("Animal"); 78 | domain.addPredicate("Loves"); 79 | domain.addPredicate("Kills"); 80 | domain.addPredicate("Cat"); 81 | domain.addConstant("Jack"); 82 | domain.addConstant("Tuna"); 83 | domain.addConstant("Curiosity"); 84 | 85 | return domain; 86 | } 87 | 88 | public static FOLDomain ringOfThievesDomain() 89 | { 90 | FOLDomain domain = new FOLDomain(); 91 | domain.addPredicate("Parent"); 92 | domain.addPredicate("Caught"); 93 | domain.addPredicate("Friend"); 94 | domain.addPredicate("Skis"); 95 | domain.addConstant("Mike"); 96 | domain.addConstant("Joe"); 97 | domain.addConstant("Janet"); 98 | domain.addConstant("Nancy"); 99 | domain.addConstant("Ernie"); 100 | domain.addConstant("Bert"); 101 | domain.addConstant("Red"); 102 | domain.addConstant("Drew"); 103 | 104 | return domain; 105 | } 106 | } 107 | } -------------------------------------------------------------------------------- /aima-csharp/environment/wumpusworld/AgentPercept.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using aima.core.agent; 3 | 4 | namespace aima.core.environment.wumpusworld 5 | { 6 | /** 7 | * Artificial Intelligence A Modern Approach (3rd Edition): page 237.
8 | *
9 | * The agent has five sensors, each of which gives a single bit of information: 10 | *
    11 | *
  • In the square containing the wumpus and in the directly (not diagonally) 12 | * adjacent squares, the agent will perceive a Stench.
  • 13 | *
  • In the squares directly adjacent to a pit, the agent will perceive a 14 | * Breeze.
  • 15 | *
  • In the square where the gold is, the agent will perceive a Glitter.
  • 16 | *
  • When an agent walks into a wall, it will perceive a Bump.
  • 17 | *
  • When the wumpus is killed, it emits a woeful Scream that can be perceived 18 | * anywhere in the cave.
  • 19 | *
20 | * 21 | * @author Federico Baron 22 | * @author Alessandro Daniele 23 | * @author Ciaran O'Reilly 24 | */ 25 | public class AgentPercept : Percept 26 | { 27 | private bool stench; 28 | private bool breeze; 29 | private bool glitter; 30 | private bool bump; 31 | private bool scream; 32 | 33 | /** 34 | * Default Constructor. All sensor inputs are considered false. 35 | */ 36 | public AgentPercept() 37 | { 38 | setStench(false); 39 | setBreeze(false); 40 | setGlitter(false); 41 | setBump(false); 42 | setScream(false); 43 | } 44 | 45 | /** 46 | * Constructor with all 5 sensor inputs explicitly set. 47 | * 48 | * @param stench 49 | * @param breeze 50 | * @param glitter 51 | * @param bump 52 | * @param scream 53 | */ 54 | public AgentPercept(bool stench, bool breeze, bool glitter, 55 | bool bump, bool scream) 56 | { 57 | setStench(stench); 58 | setBreeze(breeze); 59 | setGlitter(glitter); 60 | setBump(bump); 61 | setScream(scream); 62 | } 63 | 64 | public bool isStench() 65 | { 66 | return stench; 67 | } 68 | 69 | public void setStench(bool stench) 70 | { 71 | this.stench = stench; 72 | } 73 | 74 | public bool isBreeze() 75 | { 76 | return breeze; 77 | } 78 | 79 | public void setBreeze(bool breeze) 80 | { 81 | this.breeze = breeze; 82 | } 83 | 84 | public bool isGlitter() 85 | { 86 | return glitter; 87 | } 88 | 89 | public void setGlitter(bool glitter) 90 | { 91 | this.glitter = glitter; 92 | } 93 | 94 | public bool isBump() 95 | { 96 | return bump; 97 | } 98 | 99 | public void setBump(bool bump) 100 | { 101 | this.bump = bump; 102 | } 103 | 104 | public bool isScream() 105 | { 106 | return scream; 107 | } 108 | 109 | public void setScream(bool scream) 110 | { 111 | this.scream = scream; 112 | } 113 | 114 | public string toString() 115 | { 116 | return "[" + ((stench) ? "Stench" : "None") + ", " 117 | + ((breeze) ? "Breeze" : "None") + ", " 118 | + ((glitter) ? "Glitter" : "None") + ", " 119 | + ((bump) ? "Bump" : "None") + ", " 120 | + ((scream) ? "Scream" : "None") + "]"; 121 | } 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /aima-csharp/agent/impl/aprog/TableDrivenAgentProgram.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using aima.core.agent; 3 | using aima.core.agent.impl; 4 | using aima.core.util; 5 | 6 | namespace aima.core.agent.impl.aprog 7 | { 8 | /** 9 | * Artificial Intelligence A Modern Approach (3rd Edition): Figure 2.7, page 47.
10 | *
11 | * 12 | *
13 |      * function TABLE-DRIVEN-AGENT(percept) returns an action
14 |      *   persistent: percepts, a sequence, initially empty
15 |      *               table, a table of actions, indexed by percept sequences, initially fully specified
16 |      *           
17 |      *   append percept to end of percepts
18 |      *   action <- LOOKUP(percepts, table)
19 |      *   return action
20 |      * 
21 | * 22 | * Figure 2.7 The TABLE-DRIVEN-AGENT program is invoked for each new percept and 23 | * returns an action each time. It retains the complete percept sequence in 24 | * memory. 25 | * 26 | * @author Ciaran O'Reilly 27 | * @author Mike Stampone 28 | * 29 | */ 30 | public class TableDrivenAgentProgram : AgentProgram 31 | { 32 | private List percepts = new List(); 33 | 34 | private Table, System.String, Action> table; 35 | 36 | private const System.String ACTION = "action"; 37 | 38 | // persistent: percepts, a sequence, initially empty 39 | // table, a table of actions, indexed by percept sequences, initially fully 40 | // specified 41 | /** 42 | * Constructs a TableDrivenAgentProgram with a table of actions, indexed by 43 | * percept sequences. 44 | * 45 | * @param perceptSequenceActions 46 | * a table of actions, indexed by percept sequences 47 | */ 48 | public TableDrivenAgentProgram(Dictionary, Action> perceptSequenceActions) 49 | { 50 | List> rowHeaders = new List>(perceptSequenceActions.Keys); 51 | 52 | List colHeaders = new List(); 53 | colHeaders.Add(ACTION); 54 | 55 | table = new Table, System.String, Action>(rowHeaders, colHeaders); 56 | 57 | foreach (List row in rowHeaders) 58 | { 59 | table.set(row, ACTION, perceptSequenceActions[row]); 60 | } 61 | } 62 | 63 | // START-AgentProgram 64 | 65 | // function TABLE-DRIVEN-AGENT(percept) returns an action 66 | public Action execute(Percept percept) 67 | { 68 | // append percept to end of percepts 69 | percepts.Add(percept); 70 | 71 | // action <- LOOKUP(percepts, table) 72 | // return action 73 | return lookupCurrentAction(); 74 | } 75 | 76 | //END-AgentProgram 77 | 78 | //PRIVATE METHODS 79 | private Action lookupCurrentAction() 80 | { 81 | Action action = null; 82 | 83 | action = table.get(percepts, ACTION); 84 | if (null == action) 85 | { 86 | action = NoOpAction.NO_OP; 87 | } 88 | 89 | return action; 90 | } 91 | } 92 | } -------------------------------------------------------------------------------- /aima-csharp/logic/fol/parsing/ast/Predicate.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Linq; 5 | using aima.core.logic.fol.parsing; 6 | 7 | namespace aima.core.logic.fol.parsing.ast 8 | { 9 | /** 10 | * @author Ravi Mohan 11 | * @author Ciaran O'Reilly 12 | */ 13 | public class Predicate : AtomicSentence 14 | { 15 | private String predicateName; 16 | private List terms = new List(); 17 | private String stringRep = null; 18 | private int hashCode = 0; 19 | 20 | public Predicate(String predicateName, List terms) 21 | { 22 | this.predicateName = predicateName; 23 | this.terms.AddRange(terms); 24 | } 25 | 26 | public String getPredicateName() 27 | { 28 | return predicateName; 29 | } 30 | 31 | public List getTerms() 32 | { 33 | return terms.AsReadOnly().ToList(); 34 | } 35 | 36 | // START-AtomicSentence 37 | 38 | public String getSymbolicName() 39 | { 40 | return getPredicateName(); 41 | } 42 | 43 | public bool isCompound() 44 | { 45 | return true; 46 | } 47 | 48 | List FOLNode.getArgs() 49 | { 50 | return null; 51 | } 52 | 53 | AtomicSentence AtomicSentence.copy() 54 | { 55 | return null; 56 | } 57 | 58 | public List getArgs() 59 | { 60 | return getTerms(); 61 | } 62 | 63 | public Object accept(FOLVisitor v, Object arg) 64 | { 65 | return v.visitPredicate(this, arg); 66 | } 67 | 68 | public FOLNode copy() 69 | { 70 | List copyTerms = new List(); 71 | foreach (Term t in terms) 72 | { 73 | copyTerms.Add(t.copy()); 74 | } 75 | return new Predicate(predicateName, copyTerms); 76 | } 77 | 78 | public Sentence copySentence() 79 | { 80 | return null; 81 | } 82 | 83 | // END-AtomicSentence 84 | 85 | public override bool Equals(Object o) 86 | { 87 | 88 | if (this == o) 89 | { 90 | return true; 91 | } 92 | if (!(o is Predicate)) 93 | { 94 | return false; 95 | } 96 | Predicate p = (Predicate)o; 97 | return p.getPredicateName().Equals(getPredicateName()) 98 | && p.getTerms().Equals(getTerms()); 99 | } 100 | 101 | public override int GetHashCode() 102 | { 103 | if (0 == hashCode) 104 | { 105 | hashCode = 17; 106 | hashCode = 37 * hashCode + predicateName.GetHashCode(); 107 | foreach (Term t in terms) 108 | { 109 | hashCode = 37 * hashCode + t.GetHashCode(); 110 | } 111 | } 112 | return hashCode; 113 | } 114 | 115 | public override String ToString() 116 | { 117 | if (null == stringRep) 118 | { 119 | StringBuilder sb = new StringBuilder(); 120 | sb.Append(predicateName); 121 | sb.Append("("); 122 | 123 | bool first = true; 124 | foreach (Term t in terms) 125 | { 126 | if (first) 127 | { 128 | first = false; 129 | } 130 | else 131 | { 132 | sb.Append(","); 133 | } 134 | sb.Append(t.ToString()); 135 | } 136 | 137 | sb.Append(")"); 138 | stringRep = sb.ToString(); 139 | } 140 | return stringRep; 141 | } 142 | } 143 | } --------------------------------------------------------------------------------