Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.22 KB | None | 0 0
  1. public interface IState{
  2.     bool isGoal();
  3. }
  4.  
  5. public interface IOperator{
  6.     bool isApplicable(IState state);
  7.     IState Apply(IState state);
  8. }
  9.  
  10. public interface Problem{
  11.     IState StartState();
  12.     List<IOperator> Operators();
  13. }
  14.  
  15. class Node{
  16.     IState state;
  17.     public Node(IState state){
  18.         this.state = state;
  19.     }
  20. }
  21.  
  22. class Algorithm{
  23.    
  24.     private Problem problem;
  25.    
  26.     public Algorithm(Problem p){
  27.         this.problem = p;
  28.     }
  29.    
  30.     Node actualNode = null;
  31.    
  32.     public bool run(){
  33.        
  34.         actualNode = new Node(problem.StartState());
  35.         Random rnd = new Random();
  36.        
  37.         while(true){
  38.            
  39.             if( actualNode.state.isGoal() ) break;
  40.            
  41.             List<IOperator> ops = new List<IOperator>;
  42.            
  43.             foreach( var o in problem.Operators() )
  44.                 if( o.isApplicable( actualNode.state ) )
  45.                     ops.Add(o);
  46.            
  47.             if( ops.count == 0 ) break;
  48.            
  49.             IOperator operator = ops[ rnd.Next(0, ops.Count - 1) ];
  50.            
  51.             IState newState = operator.Appy( actualNode.state );
  52.            
  53.             actualNode = new Node(newState);
  54.            
  55.         }
  56.        
  57.         if( actualNode.state.isGoal() ) Console.WriteLine( "Van megoldas" );
  58.                                     else Console.WriteLine( "Nincs Megoldas" );
  59.        
  60.     }
  61.    
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement