(FILECREATED "26-Mar-85 19:47:25" {INDIGO}<LOOPS>SOURCES>TRUCKIN>TRAVELER.;2 16736  

      changes to:  (METHODS Traveler.TakeTurn Traveler.BuyGas Traveler.FindStoppingPlace 
			    Traveler.GoToStoppingPlace)
		   (FNS Traveler.TakeTurn Traveler.BuyGas Traveler.FindStoppingPlace 
			Traveler.GoToStoppingPlace)

      previous date: "16-NOV-83 21:44:11" {INDIGO}<LOOPS>SOURCES>TRUCKIN>TRAVELER.;1)


(* Copyright (c)  by NIL. All rights reserved.)

(PRETTYCOMPRINT TRAVELERCOMS)

(RPAQQ TRAVELERCOMS ((* Copyright (c)
			1983 by Xerox Corporation.)
		     (* * File for a Traveler. -- A very simple automatic player for TRUCKIN.)
		     (* TRUCKIN is a mini-expert system for teaching knowledge representation 
			techniques in the Loops programming system. Truckin provides a simple 
			simulation environment for novice Loops users in which small bodies of 
			knowledge can be created and tested interactively. Knowledge in Truckin is in 
			the form of rules for controlling a game piece to "maximize profit.")
		     (CLASSES * TRAVELERCLASSES)
		     (METHODS Traveler.TakeTurn Traveler.BuyGas Traveler.FindStoppingPlace 
			      Traveler.GoToStoppingPlace)
		     (FNS BuyGasTravelerRules FindStoppingPlaceTravelerRules 
			  GoToStoppingPlaceTravelerRules TakeTurnTravelerRules)
		     (INSTANCES BuyGasTravelerRules FindStoppingPlaceTravelerRules 
				GoToStoppingPlaceTravelerRules TakeTurnTravelerRules)))



(* Copyright (c) 1983 by Xerox Corporation.)

(* * File for a Traveler. -- A very simple automatic player for TRUCKIN.)




(* TRUCKIN is a mini-expert system for teaching knowledge representation techniques in the 
Loops programming system. Truckin provides a simple simulation environment for novice Loops 
users in which small bodies of knowledge can be created and tested interactively. Knowledge in 
Truckin is in the form of rules for controlling a game piece to "maximize profit.")


(RPAQQ TRAVELERCLASSES (Traveler))
(DEFCLASSES Traveler)
[DEFCLASS Traveler
   (MetaClass PlayerMeta doc 

          (* * A Traveler is a player who tours back and forth between UnionHall and AlicesRestaurant, buying gasoline and 
	  getting weighed as needed. Example of a very simple automated playerthat does not compete very knowledgeably to 
	  win.)


	      Edited:                                        (* mjs: "18-FEB-83 10:53"))
   (Supers Player)
   (InstanceVariables (destination NIL doc                   (* Current destination. Either AlicesRestaurant or the 
							     UnionHall.))
		      (direction NIL doc                     (* Forward or Backward direction of travel on the 
							     highway.))
		      (stoppingPlace NIL doc                 (* Place to stop enroute to destination.)
				     ))]

[METH Traveler  TakeTurn NIL
      NIL (method Traveler.TakeTurn RuleSet TakeTurnTravelerRules)]


[METH Traveler  BuyGas NIL
      (* RuleSet BuyGasTravelerRules is installed as the selector BuyGas of the class Traveler)
      (method Traveler.BuyGas RuleSet BuyGasTravelerRules)]


[METH Traveler  FindStoppingPlace NIL
      (* RuleSet FindStoppingPlaceTravelerRules is installed as the selector FindStoppingPlace of the 
	 class Traveler)
      (method Traveler.FindStoppingPlace RuleSet FindStoppingPlaceTravelerRules)]


[METH Traveler  GoToStoppingPlace NIL
      (* RuleSet GoToStoppingPlaceTravelerRules is installed as the selector GoToStoppingPlace of the 
	 class Traveler)
      (method Traveler.GoToStoppingPlace RuleSet GoToStoppingPlaceTravelerRules)]


(DEFINEQ

(Traveler.TakeTurn
  (Method ((Traveler TakeTurn)
	   self)
	  (TakeTurnTravelerRules self)))

(Traveler.BuyGas
  (Method ((Traveler BuyGas)
	   self)
	  (BuyGasTravelerRules self)))

(Traveler.FindStoppingPlace
  (Method ((Traveler FindStoppingPlace)
	   self)
	  (FindStoppingPlaceTravelerRules self)))

(Traveler.GoToStoppingPlace
  (Method ((Traveler GoToStoppingPlace)
	   self)
	  (GoToStoppingPlaceTravelerRules self)))
)
(DEFINEQ

(BuyGasTravelerRules
  [LAMBDA (self)
    (PROG (↑value spaceInTank fuelAffordable fuelAvailable qty)
          [SETQ ↑value (PROGN [SETQ spaceInTank (DIFFERENCE (GetClassValue (GetValue self
										     (QUOTE truck))
									   (QUOTE MaxFuel))
							    (GetValue (GetValue self (QUOTE truck))
								      (QUOTE fuel]
			      (SETQ fuelAvailable (GetValue (GetValue (GetValue self (QUOTE truck))
								      (QUOTE location))
							    (QUOTE qty)))
			      [SETQ fuelAffordable (QUOTIENT (GetValue (GetValue self (QUOTE truck))
								       (QUOTE cashBox))
							     (PricePerUnit
							       (GetValue (GetValue self (QUOTE truck))
									 (QUOTE location]
			      (SETQ qty (MIN spaceInTank fuelAffordable fuelAvailable]
          [COND
	    ((GREATERP qty 0)
	      (SETQ ↑value (← PlayerInterface Buy self qty]
      QUIT(RETURN ↑value])

(FindStoppingPlaceTravelerRules
  [LAMBDA (self)
    (PROG (↑value wStation gasStation)
          [COND
	    [(EQ (GetValue (GetValue self (QUOTE truck))
			   (QUOTE fuel))
		 0)
	      (SETQ ↑value (PROGN (PROGN                     (* Rule Tracing Code)
					 (TraceRHS (QUOTE FindStoppingPlaceTravelerRules)
						   NIL 1)
					 (← (GetObjFromUID "NPS0.lVC1.<7;.9")
					    Print))
				  (PutValue self (QUOTE stoppingPlace)
					    (AnyRoadStop ($ UnionHall]
	    [[AND [SETQ wStation (NearestRoadStop (RoadStops ($ WeighStation)
							     (← self Range1)
							     (GetValue self (QUOTE direction))
							     (QUOTE Room]
		  (LESSP (Distance wStation)
			 (Distance (GetValue self (QUOTE destination]
	      (SETQ ↑value (PROGN (PROGN                     (* Rule Tracing Code)
					 (TraceRHS (QUOTE FindStoppingPlaceTravelerRules)
						   NIL 2)
					 (← (GetObjFromUID "NPS0.lVC1.<7;.10")
					    Print))
				  (PutValue self (QUOTE stoppingPlace)
					    wStation]
	    [[AND [SETQ gasStation (FurthestRoadStop (RoadStops ($ GasStation)
								(← self Range1)
								(GetValue self (QUOTE direction))
								(QUOTE Room]
		  (LESSP (Distance gasStation)
			 (Distance (GetValue self (QUOTE destination]
	      (SETQ ↑value (PROGN (PROGN                     (* Rule Tracing Code)
					 (TraceRHS (QUOTE FindStoppingPlaceTravelerRules)
						   NIL 3)
					 (← (GetObjFromUID "NPS0.lVC1.<7;.11")
					    Print))
				  (PutValue self (QUOTE stoppingPlace)
					    gasStation]
	    [[AND (LEQ (Distance (GetValue self (QUOTE destination)))
		       (← self Range1))
		  (RoomToParkP (GetValue self (QUOTE destination]
	      (SETQ ↑value (PROGN (PROGN                     (* Rule Tracing Code)
					 (TraceRHS (QUOTE FindStoppingPlaceTravelerRules)
						   NIL 4)
					 (← (GetObjFromUID "NPS0.lVC1.<7;.12")
					    Print))
				  (PutValue self (QUOTE stoppingPlace)
					    (GetValue self (QUOTE destination]
	    ((AND)
	      (SETQ ↑value (PROGN (PROGN                     (* Rule Tracing Code)
					 (TraceRHS (QUOTE FindStoppingPlaceTravelerRules)
						   NIL 5)
					 (← (GetObjFromUID "NPS0.lVC1.<7;.13")
					    Print))
				  (PutValue self (QUOTE stoppingPlace)
					    (NthRoadStop (GetValue self (QUOTE maxMove))
							 (GetValue self (QUOTE direction))
							 NIL
							 (QUOTE Room]
      QUIT(RETURN ↑value])

(GoToStoppingPlaceTravelerRules
  [LAMBDA (self)
    (PROG (↑value)
          [SETQ ↑value (PROGN (← self FindStoppingPlace)
			      (← PlayerInterface Move self (GetValue self (QUOTE stoppingPlace]
          [COND
	    ((ISA (GetValue (GetValue self (QUOTE truck))
			    (QUOTE location))
		  ($ GasStation))
	      (SETQ ↑value (← self BuyGas]
      QUIT(RETURN ↑value])

(TakeTurnTravelerRules
  [LAMBDA (self)
    (PROG (↑value)
          [COND
	    ((NOT (GetValue self (QUOTE destination)))
	      (SETQ ↑value (PROGN (PutValue self (QUOTE destination)
					    (AnyRoadStop ($ AlicesRestaurant)))
				  (PutValue self (QUOTE direction)
					    (QUOTE Forward]
          [COND
	    ((← (GetValue (GetValue self (QUOTE truck))
			  (QUOTE location))
		InstOf
		($ UnionHall))
	      (SETQ ↑value (PROGN (PutValue self (QUOTE destination)
					    (AnyRoadStop ($ AlicesRestaurant)))
				  (PutValue self (QUOTE direction)
					    (QUOTE Forward]
          [COND
	    ((← (GetValue (GetValue self (QUOTE truck))
			  (QUOTE location))
		InstOf
		($ AlicesRestaurant))
	      (SETQ ↑value (PROGN (PutValue self (QUOTE destination)
					    (AnyRoadStop ($ UnionHall)))
				  (PutValue self (QUOTE direction)
					    (QUOTE Backward]
          (SETQ ↑value (← self GoToStoppingPlace))
      QUIT(RETURN ↑value])
)
[METH NIL  NIL NIL
      NIL (method ? methodClass RuleSet UID "JZS@@A>N>")
	  (perspectiveNode #&(RuleSetNode "JZS@@A>N@"))
	  (name #(BuyGasTravelerRules NIL RememberName))
	  (compiledRules BuyGasTravelerRules)
	  (workSpace Traveler)
	  (tempVars (spaceInTank fuelAffordable fuelAvailable qty))
	  (taskVars NIL)
	  (debugVars NIL)
	  (numRules 2)
	  (controlStructure DOALL)
	  (whileCondition NIL)
	  (compilerOptions NIL)
	  (auditClass NIL)
	  (metaAssignments NIL)
	  (ruleClass NIL)
	  (taskClass)
	  (arguments)]


[METH NIL  NIL NIL
      NIL (method ? methodClass RuleSet UID "JZS@@A>N?")
	  (perspectiveNode #&(RuleSetNode "JZS@@A>NA"))
	  (name #(FindStoppingPlaceTravelerRules NIL RememberName))
	  (compiledRules FindStoppingPlaceTravelerRules)
	  (workSpace Traveler)
	  (tempVars (wStation gasStation))
	  (taskVars NIL)
	  (debugVars NIL)
	  (numRules 5)
	  (controlStructure DO1)
	  (whileCondition NIL)
	  (compilerOptions (T))
	  (auditClass NIL)
	  (metaAssignments NIL)
	  (ruleClass NIL)
	  (taskClass)
	  (arguments)]


[METH NIL  NIL NIL
      NIL (method ? methodClass RuleSet UID "JZS@@A>M|")
	  (perspectiveNode #&(RuleSetNode "JZS@@A>NB"))
	  (name #(GoToStoppingPlaceTravelerRules NIL RememberName))
	  (compiledRules GoToStoppingPlaceTravelerRules)
	  (workSpace Traveler)
	  (tempVars NIL)
	  (taskVars NIL)
	  (debugVars NIL)
	  (numRules 2)
	  (controlStructure DOALL)
	  (whileCondition NIL)
	  (compilerOptions NIL)
	  (auditClass NIL)
	  (metaAssignments NIL)
	  (ruleClass NIL)
	  (taskClass)
	  (arguments)]


[METH NIL  NIL NIL
      NIL (method ? methodClass RuleSet UID "JZS@@A>M{")
	  (perspectiveNode #&(RuleSetNode "JZS@@A>NC"))
	  (name #(TakeTurnTravelerRules NIL RememberName))
	  (compiledRules TakeTurnTravelerRules)
	  (workSpace Traveler)
	  (tempVars NIL)
	  (taskVars NIL)
	  (debugVars NIL)
	  (numRules 4)
	  (controlStructure DOALL)
	  (whileCondition NIL)
	  (compilerOptions NIL)
	  (auditClass NIL)
	  (metaAssignments NIL)
	  (ruleClass NIL)
	  (taskClass)
	  (arguments)]


[DEFINST RuleSetNode ("JZS@@A>N@")
         (perspectives ? RuleSet #&(RuleSet "JZS@@A>N>") Source #&(RuleSetSource "JZS@@A>ND"))]


[DEFINST RuleSetNode ("JZS@@A>NA")
         (perspectives ? RuleSet #&(RuleSet "JZS@@A>N?") Source #&(RuleSetSource "JZS@@A>NE"))]


[DEFINST RuleSetNode ("JZS@@A>NB")
         (perspectives ? RuleSet #&(RuleSet "JZS@@A>M|") Source #&(RuleSetSource "JZS@@A>NF"))]


[DEFINST RuleSetNode ("JZS@@A>NC")
         (perspectives ? RuleSet #&(RuleSet "JZS@@A>M{") Source #&(RuleSetSource "JZS@@A>NG"))]


[DEFINST RuleSetSource ("JZS@@A>ND")
         (indexedVars ((#&(Rule "NPS0.lVC1.IeB.2"))
		       (#&(Rule "NPS0.lVC1.IeB.3"))))
	 (perspectiveNode #&(RuleSetNode "JZS@@A>N@"))
	 (created "27-JAN-83 00:31:21")
	 (creator STEFIK)
	 (edited "16-NOV-83 14:57:18")
	 (editor BOBROW)]


[DEFINST RuleSetSource ("JZS@@A>NE")
         (indexedVars ((#&(Rule "NPS0.lVC1.<7;.9"))
		       (#&(Rule "NPS0.lVC1.<7;.10"))
		       (#&(Rule "NPS0.lVC1.<7;.11"))
		       (#&(Rule "NPS0.lVC1.<7;.12"))
		       (#&(Rule "NPS0.lVC1.<7;.13"))))
	 (perspectiveNode #&(RuleSetNode "JZS@@A>NA"))
	 (created "27-JAN-83 00:34:46")
	 (creator STEFIK)
	 (edited "16-NOV-83 15:11:37")
	 (editor BOBROW)]


[DEFINST RuleSetSource ("JZS@@A>NF")
         (indexedVars ((#&(Rule "NPS0.lVC1.IeB.4"))
		       (#&(Rule "NPS0.lVC1.IeB.5"))))
	 (perspectiveNode #&(RuleSetNode "JZS@@A>NB"))
	 (created "27-JAN-83 00:02:00")
	 (creator STEFIK)
	 (edited "16-NOV-83 14:57:51")
	 (editor BOBROW)]


[DEFINST RuleSetSource ("JZS@@A>NG")
         (indexedVars ((#&(Rule "LYS.0.5349.124.36446.244"))
		       (#&(Rule "LYS.0.5349.124.36446.245"))
		       (#&(Rule "LYS.0.5349.124.36446.246"))
		       (#&(Rule "LYS.0.5349.124.36446.247"))))
	 (perspectiveNode #&(RuleSetNode "JZS@@A>NC"))
	 (created "26-JAN-83 23:15:35")
	 (creator STEFIK)
	 (edited "25-JUL-83 10:28:04")
	 (editor STEFIK)]


[DEFINST Rule ("NPS0.lVC1.IeB.2")
         (source 
"(* Buy gasoline as limited by various constraints.)

     THEN spaceInTank←truck::MaxFuel-truck:fuel
	
          fuelAvailable←truck:location:qty

          fuelAffordable←truck:cashBox/(PricePerUnit truck:location)

          qty←(MIN spaceInTank fuelAffordable fuelAvailable);")
	 (edited "16-NOV-83 14:57:18")
	 (editor BOBROW)
	 (ruleNumber 1)
	 (ruleSet #&(RuleSet "JZS@@A>N>"))]


[DEFINST Rule ("NPS0.lVC1.IeB.3")
         (source 
"(* Now appropriate quantity has been computed)

    IF qty>0
     THEN  (← \PlayerInterface Buy self qty );")
	 (edited "16-NOV-83 14:57:18")
	 (editor BOBROW)
	 (ruleNumber 2)
	 (ruleSet #&(RuleSet "JZS@@A>N>"))]


[DEFINST Rule ("NPS0.lVC1.<7;.9")
         (source 
"(* Return as a value the next place to stop enroute to destination.)

IF truck:fuel=0
 THEN stoppingPlace←(AnyRoadStop $UnionHall);")
	 (edited "16-NOV-83 15:11:37")
	 (editor BOBROW)
	 (ruleNumber 1)
	 (ruleSet #&(RuleSet "JZS@@A>N?"))]


[DEFINST Rule ("NPS0.lVC1.<7;.10")
         (source 
"IF wStation←(NearestRoadStop (RoadStops $WeighStation .Range1 direction 'Room))
   (Distance wStation)<(Distance destination)
 THEN stoppingPlace←wStation;")
	 (edited "16-NOV-83 15:11:37")
	 (editor BOBROW)
	 (ruleNumber 2)
	 (ruleSet #&(RuleSet "JZS@@A>N?"))]


[DEFINST Rule ("NPS0.lVC1.<7;.11")
         (source 
" 
IF gasStation←(FurthestRoadStop (RoadStops $GasStation .Range1 direction 'Room))
  (Distance gasStation)<(Distance destination)
THEN stoppingPlace←gasStation;")
	 (edited "16-NOV-83 15:11:37")
	 (editor BOBROW)
	 (ruleNumber 3)
	 (ruleSet #&(RuleSet "JZS@@A>N?"))]


[DEFINST Rule ("NPS0.lVC1.<7;.12")
         (source 
  "IF (Distance destination)<=.Range1 (RoomToParkP destination)
 THEN stoppingPlace←destination;")
	 (edited "16-NOV-83 15:11:37")
	 (editor BOBROW)
	 (ruleNumber 4)
	 (ruleSet #&(RuleSet "JZS@@A>N?"))]


[DEFINST Rule ("NPS0.lVC1.<7;.13")
         (source "-> stoppingPlace←(NthRoadStop maxMove direction NIL 'Room);")
	 (edited "16-NOV-83 15:11:37")
	 (editor BOBROW)
	 (ruleNumber 5)
	 (ruleSet #&(RuleSet "JZS@@A>N?"))]


[DEFINST Rule ("NPS0.lVC1.IeB.4")
         (source 
"(* Ask to go to a stopping place returned by FindStoppingPlace)

	-> .FindStoppingPlace (← \PlayerInterface Move self stoppingPlace);")
	 (edited "16-NOV-83 14:57:51")
	 (editor BOBROW)
	 (ruleNumber 1)
	 (ruleSet #&(RuleSet "JZS@@A>M|"))]


[DEFINST Rule ("NPS0.lVC1.IeB.5")
         (source 
" 
(* Check if real truck location is a gas station.  Don't  
   use stoppingPlace, since chosen it may not have been reached  
   because of cops or robbers)

	IF (ISA truck:location $GasStation)
	 THEN .BuyGas;")
	 (edited "16-NOV-83 14:57:51")
	 (editor BOBROW)
	 (ruleNumber 2)
	 (ruleSet #&(RuleSet "JZS@@A>M|"))]


[DEFINST Rule ("LYS.0.5349.124.36446.244")
         (source 
"(* Top Level RuleSet for Traveler.
   Travel between UnionHall a randomly chosen AlicesRestaurant)

	IF ~destination
	 THEN destination←(AnyRoadStop $AlicesRestaurant)
	     direction←'Forward;")
	 (edited "25-JUL-83 10:28:04")
	 (editor STEFIK)
	 (ruleNumber 1)
	 (ruleSet #&(RuleSet "JZS@@A>M{"))]


[DEFINST Rule ("LYS.0.5349.124.36446.245")
         (source 
"  	(* No destination if at start)

	IF (← truck:location InstOf $UnionHall)
	 THEN destination←(AnyRoadStop $AlicesRestaurant)
	     direction←'Forward;")
	 (edited "25-JUL-83 10:28:04")
	 (editor STEFIK)
	 (ruleNumber 2)
	 (ruleSet #&(RuleSet "JZS@@A>M{"))]


[DEFINST Rule ("LYS.0.5349.124.36446.246")
         (source 
"	(* Choose an Alices to go to.)

	IF (← truck:location InstOf $AlicesRestaurant)
	 THEN destination←(AnyRoadStop $UnionHall)
	     direction←'Backward;")
	 (edited "25-JUL-83 10:28:04")
	 (editor STEFIK)
	 (ruleNumber 3)
	 (ruleSet #&(RuleSet "JZS@@A>M{"))]


[DEFINST Rule ("LYS.0.5349.124.36446.247")
         (source "	(* Set destination to UnionHall)

	 THEN .GoToStoppingPlace;")
	 (edited "25-JUL-83 10:28:04")
	 (editor STEFIK)
	 (ruleNumber 4)
	 (ruleSet #&(RuleSet "JZS@@A>M{"))]


(DECLARE: DONTCOPY
  (FILEMAP (NIL (3593 4063 (Traveler.TakeTurn 3603 . 3704) (Traveler.BuyGas 3706 . 3801) (
Traveler.FindStoppingPlace 3803 . 3931) (Traveler.GoToStoppingPlace 3933 . 4061)) (4064 8655 (
BuyGasTravelerRules 4074 . 4946) (FindStoppingPlaceTravelerRules 4948 . 7325) (
GoToStoppingPlaceTravelerRules 7327 . 7705) (TakeTurnTravelerRules 7707 . 8653)))))
STOP