Author Topic: Army Ants Recruit Training (lisp challenge)  (Read 7666 times)

0 Members and 1 Guest are viewing this topic.

pkohut

  • Guest
Army Ants Recruit Training (lisp challenge)
« on: June 19, 2009, 12:12:18 AM »
(updated 3:45am PST, 6-20-2009
Due to possible time constrants and non-availability to a computer, the source code for this projects is released early.
See bottom of this message for the attachment
:-)

(revision: updated 5:25am PST, 6-19-2009
Latest version now CADArmyAnts-V0.10b.zip,  modified section on ACP code and input parameteres)


Welcome Recruits to your basic training of being an ant!  As recruits you will learn how to most
effectively locate food and bring it back to your ant hill.  This will be your only goal during basic
training.  Lower scores mean more effective food collection.

During training you will write an Ant Controller Program (ACP) using lisp.  The simulator will call
your ACP for each ant that is at the proving grounds.  Ants have limited visibility of 2 squares
(grid units) around them.  They can see other ants, food, and obstacles, and smell scent trails
that other ants may have left behind.  Using only the local knowledge provided to the ACP,
the ACP will order the ant to carry out an action.

ACP Data Input
The environment data sent to the ACP, about the current ant, is contained in 8 lists.  Each list is a 5 x 5
matrix with sublist rows
, and your current ant is located in position 3 x 3.  The lists and possible values are:
Main Map1 = Location of your ant hills
-1 = Location of opponents ant hills
-99 = Impassible cell (rocks)
0 = Open space
Food MapLocation of food items, number will represent amount
My Ant MapLocation of your ants, there will always be one at 3,3
Opponent's Ant MapLocation of opponents ants
My Scent MapWhere and how much scent your ants have deposited.  The number in each cell is decremented by one every cycle.
Opponent's Scent MapWhere and how much scent your opponents have deposited.  The number in each cell decrements by one each cycle
My Death MapWhere and how much death scent has been deposited by your ants.  The number in each cell decrements by one each cycle
Opponent's Death MapWhere and how much death scent has been deposited by the opponent's ants.  The number in each cell is decremented by one every cycle.

** While in basic training lists about opponents is irrelevant and are only meaningful in future training exercises. **

ACP Data Output
Using just the information provided during the current round your ACP will return an order to the simulation in
in the form of a list:
    (list Row Col Action Mark)
The meaning and possible values are:
Row-1 = Up one (north)
0 = No move (north)
1 = down one (south)
Col-1 = Left one (west)
0 = No move (south)
1 = Right one (east)
Action-1 = Attack
0 = No action
1 = Ant will attempt to carry a food item
Mark0 true 100 = Amount of scent to leave behind
** Note: Ants can only attack if it is not moving.  Orders that have both move and attack will be carried
out as a move only **

** Note: In basic training, orders to attack will in effect be a no action - action **


Local ACP Input Grid Sample
OOOOO
S=15ORF=4O
OOA=2OA=6
OF=10OOR
AHOOOS=5
The grid layout shows 15 scent units at 1,2; a rock at 3,2; 4 food units at 4,2;
2 ants at 3,3 (one is the current ant, the other a teammate); 6 team ants at 5,3;
10 food units at 2,4; a rock at 5,4; your teams ant hill at 1,5;and 5 scent units at 5,5

ACP Code
This a about as simple as an ACP you can have.  ACP's must be written to accept
8 parameters, otherwise the simulator will halt.
Code: [Select]
(defun c:MyAntController (mainMap foodMap myAntMap opAntMap
                            myScentMap opScentMap mydeathMap opDeathMap / )
(setq row 0 col 0 action 0 mark 0)
(list row col action mark)
)
Using the parameter decoding information and the local grid sample, the list would look
as follows:
mainMap( (0 0 0 0 0) (0 0 -99 0 0) (0 0 0 0 0) (0 0 0 0 -99) (1 0 0 0 0) )
foodMap( (0 0 0 0 0) (0 0 0 4 0) (0 0 0 0 0) (0 10 0 0 0) (0 0 0 0 0) )
myAntMap( (0 0 0 0 0) (0 0 0 0 0) (0 0 2 0 6) (0 0 0 0 0) (0 0 0 0 0) )
opAntMap( (0 0 0 0 0) (0 0 0 0 0) (0 0 0 0 0) (0 0 0 0 0) (0 0 0 0 0) )
myScentMap( (0 0 0 0 0) (15 0 0 0 0) (0 0 0 0 0) (0 0 0 0 0) (0 0 0 0 5) )
opScentMap( (0 0 0 0 0) (0 0 0 0 0) (0 0 0 0 0) (0 0 0 0 0) (0 0 0 0 0) )
myDeathMap( (0 0 0 0 0) (0 0 0 0 0) (0 0 0 0 0) (0 0 0 0 0) (0 0 0 0 0) )
opDeathMap( (0 0 0 0 0) (0 0 0 0 0) (0 0 0 0 0) (0 0 0 0 0) (0 0 0 0 0) )
** Again for basic training information about opponents can be ignored **
NOTE: Table updated for version 0.10b

Registering Your ACP With the Simulator
To register the above sample with the simulator is a two step process, and only needs
to be done with a new drawing or when a drawing is opened.  First the function must be
made into an exported function symbol:
  (vl-acad-defun 'myAntController)
This allows your ACP to be callable with the simulator, but it still needs to be registered
with the simulator:
  (ants_register_controller "c:myAntController")

Now you are ready to run a simulation using the ants_run command.

Running a Simulation
The command to run a simulation is:
  (ants_run [map] [realtimeGraphics] [cycles] [sleep] [runAllCycles] [serverMode])
The parameters in brackets are optional, with the most useful near the front.  The current
simulator support 5 built in maps (1 - 5) and 1 user map (0).  To run a simulation with
map 3, reatimegraphics on, 500 cycles, and a 1/4 second pause between cycles is:
  (ants_run 3 1 500 250)
I'll refer you to the "CAD_Army_Ants-r5.pdf" documentation and the "antsSample.lsp" file enclosed
with the download for full details on all available commands and their options.

Submitting a Code Entry to the Forum
To make it as easy as possible for forum members to run and test code submissions the code
should have the following form: Only 1 global ACP function.  Any subroutines needed by
the ACP should be local in scope to the ACP function.  The code should be commented to
describe it behavior, and your name or forum handle.  Along with the ACP function the
code will also define the exported function symbol and automatically register it with the
simulator.  Here is a full working example:
Code: [Select]
;;; xyzMyAntController, about the lazest ants around. They don't do a thing
(defun c:xyzMyAntController (mainMap foodMap myAntMap opAntMap
                            myScentMap opScentMap mydeathMap opDeathMap / )
(defun myLocalScopeFunction ( / )
(princ))
(setq row 0 col 0 action 0 mark 0)
(list row col action mark)
)

;;; automatically export myAntController as a function symbol
(vl-acad-defun 'myAntController)

;;; automatically register with the simulator when the lisp file is loaded
(ants_register_controller "c:myAntController")

Code Submission and Modifying Existing Submitted Code
Submit your code as a new post to this thread.  Wrap the code in forum
Code: [Select]
blocks to make it stand out better.  Also submit the simulation result list, printed
at the command line after a simulation, wrapped in its own code block as well.  Users can
then look at this value to see how efficient the ACP is. The format of the returned
list is:
   (Map# #Teams Team1Name Team1Score #CyclesToRun #ActualCyclesRan)
   (Map# #Teams Team1Name Team1Score #CyclesToRun #ActualCyclesRan TotalFood Team1CollectedFood)
Note: this was revised for version 0.9b of CADArmyAnts.ARX
  
Do not edit submitted code and results once posted.  If you have improvements to the
code then submit it as a new message, and make sure the ACP function name is unique.
Anyone can view and copy code that is already submitted.  If you make improvements to
code that is already submit, then you are the author of the code for scoring purposes.
You are encouraged to optimized existing entries.

Scoring
Scoring is based on moving food items to your ant hill(s).  At the end of each cycle
the total Euclidean distance is calculated between all food items to the nearest team
ant hill.  The overall current running score will be the average of the end of previous cycle
results, and averaged.  Simply put, the lower your score the closer all food items are
to you hill
.

Guidelines
For those that plan to submit ACP results and code to the forum I'd like to offer some
guidelines for everyone to follow.
  • Your ACP will only use the simulation data provided to it during the current cycle, previous
    cycle data and results will not be retained and used in future cycles.
  • The ACP function should be the only global function, if your ACP needs extra functions, like
    an RNG or something, then make them local to the ACP function.
  • Make your ACP function names unique so they are identifiable.  The initials of your name will
    probably be good enough.  Just need to keep them identifiable for scoring purposes.
  • When submitting ACP simulation results to the group use the returned list printed at the command line.
    Just copy and paste it to a new message.  These can then be collected and made into some sort of
    score board or something.  The returned list meaning is:
    (Map# #Teams Team1Name Team1Score #CyclesToRun #ActualCyclesRan)
    (Map# #Teams Team1Name Team1Score #CyclesToRun #ActualCyclesRan TotalFood Team1CollectedFood)
    Note: revised for verion 0.9b of CADArmyAnts.zip
  • If you submit runtime results, then submit the code as well.  This will benifit the colony.
    See "Code Submission and Modifying Existing Submitted Code" section for details to improve its visiblity in the forum.
  • To qualify for placement standings, results submitted need to be run with a cycle count of 1000
  • Lets let this run until June 28, 2009 with complete code sharing and collaboration with others, using
    maps 1-5.  At midnight of the 29th, a single new map will be posted to the forum and for the next
    24 hours post your code, results, and optimizations.  The ACP with the lowest score after 24
    is the proclaimed winner.


Starting the Simulator
The simulator is an ARX application.  Do an "appload" and find the ArmyAnts.ARX that will run in your
version of AutoCAD.  If you are running AutoCAD 2004 - 2006 then use the binary located in
\CADArmyAnts\Bin\A2004\ArmyAnts.ARX
and if your using AutoCAD 2007 - 2009 use
\CADArmyAnts\Bin\A2007\ArmyAnts.ARX

In the \CADArmyAnts\ directory is the "antsSample.lsp" to help you get started.  Also, "SampleMapBuild.lsp"
can get you started with building your own maps.  Pretty much everything you want to know about
the program, functions, and parameters can be found in "CAD_Army_Ants-r5.pdf".

Sorry, but I don't have any AutoCAD 2010 binaries to distribute.  If someone would like to compile the
simulator for that version, I'll be more than happy to send the source code.


Finding and Reporting Bugs
Hate to say it, but you might find one or two.  If you do, shoot me a personal message as
as soon as possible.  Include the version of AutoCAD your running, a description of the bug, and what
might have triggered it, and if it is something with messed up graphics while the simulator is running
then send me the dwg file as well.  I'll fix it as fast as I can and post revised binaries to this message.

Source Code
The source code for the simulator will be posted on June 22nd.  This will give enough time for any bugs
to be found and fixed, keeping the number of source revisions low.

What's the Deal With Enemy Ants
That's for after basic training.  The next round will put team against team.  It's already built into the
simulator.  ^-^

Have fun and TIA.
Paul
« Last Edit: June 20, 2009, 06:52:30 AM by pkohut »

pkohut

  • Guest
Re: Army Ants Recruit Training (lisp challenge)
« Reply #1 on: June 19, 2009, 12:13:08 AM »
This space reserved for reporting standings and updated information. Probably will be updated a couple times a day throughout the week.

« Last Edit: June 19, 2009, 06:15:34 AM by pkohut »

pkohut

  • Guest
Re: Army Ants Recruit Training (lisp challenge)
« Reply #2 on: June 19, 2009, 01:33:05 AM »
To get things started here are the results from the sample code "c:rpkAimlessGather"
Each list was returned by the simulator after a completed run.  The format is
Map# #Teams ACPFunctionname Score #CyclesToRun #ActualCyclesRan

Code: [Select]
(1 1 "c:rpkAimlessGather" 1910.23 1000 1000)
(2 1 "c:rpkAimlessGather" 733.807 1000 1000)
(3 1 "c:rpkAimlessGather" 1157.53 1000 1000)
(4 1 "c:rpkAimlessGather" 3955.45 1000 1000)
(5 1 "c:rpkAimlessGather" 911.193 1000 1000)

and here is the source code for the ACP

Code: [Select]
;;; Aimless gather
;;; These ants have only 1 purpose - to gather food, but their
;;; not very good at it, carry the food all over the board
;;; until they just happen upon one of their ant hills.
(defun c:rpkAimlessGather (mainMap foodMap      myAntMap
   opAntMap myScentMap   opScentMap
   myDeathMap opDeathMap   /
   row col action mark)
(defun rand (/) (setq SeedRand (+ (* (fix SeedRand) 214013) 2531011)) (boole 1 (/ SeedRand 65536) 32767))

(defun rand1 (minVal maxVal /)
  (setq rVal (rand)) (setq range (+ 1 (- maxVal minVal)))
  (setq quotient (/ rVal range)) (setq remainder (- rVal (* quotient range)))
  (setq result (+ minVal remainder)))

  (setq row (- (rand1 0 2) 1) col (- (rand1 0 2) 1))
  (list row col 1 0)
)

pkohut

  • Guest
Re: Army Ants Recruit Training (lisp challenge)
« Reply #3 on: June 19, 2009, 06:15:58 AM »
Update June 19, 2009, 3:07am PST.

Question for the lisp experts.  The simulator currently is sending
data as an array which represents a 5 x 5 matrix, like so:
Quote
mainMap (0 0 0 0 0 0 0 -99 0 0 0 0 0 0 0 0 0 0 0 -99 1 0 0 0 0)
foodMap (0 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 10 0 0 0 0 0 0 0 0)

Would it be better for to send it as nested list like so:?
Quote
mainMap ((0 0 0 0 0) (0 0 -99 0 0) (0 0 0 0 0) (0 0 0 0 -99) (1 0 0 0 0) )
foodMap ((0 0 0 0 0) (0 0 0 4 0) (0 0 0 0 0) (0 10 0 0 0) (0 0 0 0 0) )

I'm no lisp expert, but am trying to write a controller with more brains than
the samples that are provided, and I'm having a heck of a time doing any
sort of matrix math with the data as supplied.

I also think that if someone had some good matrix routines they could supply
for the good of the group, then they could be part of the core library available
to all the ACP code.  Matrix addition, subtraction, multiplication, division,
transpose, flip left, and flip right
, should just about cover all the needs.

Again TIA,
Paul


gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Army Ants Recruit Training (lisp challenge)
« Reply #4 on: June 19, 2009, 06:57:35 AM »
Hi,

By my side I'd rather use a list of sublist (rows) than a flatten list for matrices.

Here're some vector/matrix calculus routines:

Code: [Select]
;;=========================== VECTOR ===========================;;

;; VXS (gile)
;; Returns the product of a vector by a scalar
;;
;; Arguments : a vector and a real
;; return : a scaled vector

(defun vxs (v s) (mapcar '(lambda (x) (* x s)) v))

;; VXV (gile)
;; Returns the dot product of two vectors (real)
;;
;; Arguments : two vectors
;; return : a real number

(defun vxv (v1 v2) (apply '+ (mapcar '* v1 v2)))

;; V^V (gile)
;; Returns the cross product of two vectors
;;
;; Arguments : two vectors
;; return : a vector perpendicular to both

(defun v^v (v1 v2)
  (list (- (* (cadr v1) (caddr v2)) (* (caddr v1) (cadr v2)))
(- (* (caddr v1) (car v2)) (* (car v1) (caddr v2)))
(- (* (car v1) (cadr v2)) (* (cadr v1) (car v2)))
  )
)


;;=========================== MATRIX ===========================;;

;; M+M (gile)
;; Adds two matrices
;;
;; Arguments : two matrices
;; return : a matrix

(defun m+m (m1 m2)
  (mapcar '(lambda (v1 v2) (mapcar '+ v1 v2)) m1 m2)
)

;; MXS (gile)
;; Multiplies a matrix by a number
;;
;; Arguments : a matrix and a real
;; return : a matrix

(defun mxs (m s) (mapcar '(lambda (v) (vxs v s)) m))

;; TRP
;; transposes a matrix -Doug Wilson-
;;
;; Argument : a matrix
;; return : a matrix

(defun trp (m) (apply 'mapcar (cons 'list m)))

;; MXV
;; Applies a transformation matrix to a vector  -Vladimir Nesterovsky-
;;
;; Arguments : une matrice et un vecteur
;; return : a vector

(defun mxv (m v)
  (mapcar '(lambda (r) (vxv r v)) m)
)

;; MXM
;; Multiplies (combinates) two matrices -Vladimir Nesterovsky-
;;
;; Arguments : deux matrices
;; return : a matrix

(defun mxm (m q)
  (mapcar '(lambda (r) (mxv (trp q) r)) m)
)

;; IMAT (gile)
;; Creates a n dimension identity matrix
;;
;; Argument
;; n : the matrix dimension
;; return : a matrix

(defun Imat (d / i n r m)
  (setq i d)
  (while (<= 0 (setq i (1- i)))
    (setq n d r nil)
    (while (<= 0 (setq n (1- n)))
      (setq r (cons (if (= i n) 1.0 0.0) r))
    )
    (setq m (cons r m))
  )
)

;; INVERSEMATRIX (gile)
;; Inverses a square matrix (Gauss-Jordan method)
;;
;; Argument: the matrix
;; Return : the inverse matrix or nil (if non reversible)

(defun InverseMatrix (mat / col piv row res)
  (setq mat (mapcar '(lambda (x1 x2) (append x1 x2)) mat (Imat (length mat))))
  (while mat
    (setq col (mapcar '(lambda (x) (abs (car x))) mat))
    (repeat (vl-position (apply 'max col) col)
      (setq mat (append (cdr mat) (list (car mat))))
    )
    (if (equal (setq piv (caar mat)) 0.0 1e-14)
      (setq mat nil
    res nil
      )
      (setq piv (/ 1.0 piv)
    row (mapcar '(lambda (x) (* x piv)) (car mat))
    mat (mapcar
  '(lambda (r / e)
     (setq e (car r))
     (cdr (mapcar '(lambda (x n) (- x (* n e))) r row))
   )
  (cdr mat)
)
    res (cons
  (cdr row)
  (mapcar
    '(lambda (r / e)
       (setq e (car r))
       (cdr (mapcar '(lambda (x n) (- x (* n e))) r row))
     )
    res
  )
)
      )
    )
  )
  (reverse res)
)
Speaking English as a French Frog

pkohut

  • Guest
Re: Army Ants Recruit Training (lisp challenge)
« Reply #5 on: June 19, 2009, 06:59:39 AM »
The question was asked if this contest is for work or play, it's all for play.  Since most of
us like coding challenges I thought it would be fun to do something like this.  The idea is
completely borrowed from a contest MatLab put on, just needed to build the framework.

For those that are interested check out MatLab's web page.  I tried to follow what's
presented there as closely as possible.  The other day I implemented the single team
mode as a way for people to test their code without worry about attacking ants.

http://www.mathworks.com/contest/armyants/rules.html
and here is some video comentary for a "king of the hill" match analysis.
http://blogs.mathworks.com/videos/2008/11/10/contest-commentary-first-five-kings-of-the-hill/
MatLab also did a single team ants contest, here's the link.
http://www.mathworks.com/contest/ants/home.html

Paul  :-)

pkohut

  • Guest
Re: Army Ants Recruit Training (lisp challenge)
« Reply #6 on: June 19, 2009, 07:08:45 AM »
Hi,

By my side I'd rather use a list of sublist (rows) than a flatten list for matrices.

Here're some vector/matrix calculus routines:

Nice Gile! Those will be very helpful.  I'm also leaning towards the sublist rows,
it just seems like it would be so much easier to work with.  Pondering... ... ...
since it's still early and only 11 or so downloads, I'll make the change now and
post the update.

Thanks,
Paul

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Army Ants Recruit Training (lisp challenge)
« Reply #7 on: June 19, 2009, 08:27:27 PM »
Docs printed ...
Looks like fun Paul.

Hope I can make some time to play  :?
// kdub


added:
I'll like to register kdub as a command/function/variable prefix :)
« Last Edit: June 19, 2009, 08:38:28 PM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

pkohut

  • Guest
Re: Army Ants Recruit Training (lisp challenge)
« Reply #8 on: June 19, 2009, 09:00:20 PM »
Thanks Kerry.  kdub is noted.

I'm finding my lack a lisp knowledge to be a real bear with this contest. I've
got 7 helper routines written to help with list manipulation.  Thank goodness
the VLIDE is interpretive, cause it has been total trial and error writing the
routines.  Struggle on  :-)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Army Ants Recruit Training (lisp challenge)
« Reply #9 on: June 19, 2009, 09:29:50 PM »

I'll just glanced through the docs

Can you confirm :
How much food can the ant carry ?
How does the ant know it has food ?

Because no data can be saved ( the ant can ONLY use the instance of data transmitted [ ultimate tabula rasa ] ),
I assume the ant will never know when it's 'time cycle' will expire .. so any logic governing what to do next will be based on instant gratification ??
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

pkohut

  • Guest
Re: Army Ants Recruit Training (lisp challenge)
« Reply #10 on: June 19, 2009, 09:31:34 PM »
I've been struggling with the matrix list manipulations for a while now.  Maybe
you guys can help shed some light on these example problems:

Given a mainMap list:
((-99 -99 -99 -99 -99) (-99 -99 -99 -99 -99) (-99 -99 1 0 0) (-99 -99 0 0 0) (-99 -99 0 0 0))

Create a new list of just blocked areas (-99's)
Code: [Select]
(defun buildBinaryList (lst val / e outLst)
    (foreach e lst (setq outLst (append outLst (list (mapcar '(lambda (x) (if (= x val) 1 0)) e)))))
    outLst
    )
Calling (buildBinaryList mainMap -99) produces
((1 1 1 1 1) (1 1 1 1 1) (1 1 0 0 0) (1 1 0 0 0) (1 1 0 0 0))
I'd say easily 2 hours to figure out that code.

The next two routines extract sub elements out of a list returing an array list:

Code: [Select]
;; Extracts the items from row y1 to y2 in column x1
;; returns a list.
(defun rows ( lst x1 y1 y2 / lst1 outLst i )
  (setq lst1 (mapcar '(lambda (x) (nth x1 x)) lst))
  (setq i y1
outLst nil)
  (while (<= i y2)
    (setq outLst (append outLst (list (nth i lst1))))
    (setq i (1+ i))
  )
  outLst
  )

  ;; Extracts the items from col x1 to col x2, in row y1
  (defun cols (lst y1 x1 x2 / lst1 outLst i)
  (setq lst1 (nth y1 lst)
i x1 outLst nil)
  (while (<= i x2)
    (setq outLst (append outLst (list (nth i lst1))))
    (setq i (1+ i))
    )
  outLst
  )

Now the one I'm working on is to stuff a subset of an array into parts of a matrix
The calling parameters: lst1 = input matrix, y1 = row, x1 = col start, x2 = col end,
lst2 = input array, x3 = start position
Surprisingly I think this took maybe an hour to figure out.
Code: [Select]
(defun setCols (lst1 y1 x1 x2 lst2 x3 / outLst i j l1 q1)
  (setq i 0
j 0
  )
  (setq outLst nil)
  (while (< j (length lst1))
    (setq l1 (nth j lst1))
    (setq i 0)
    (setq l2 nil)
    (while (< i (length l2))
      (if (= y1 j)
(if (and
      (>= i x1)
      (<= i x2)
    )
  (setq l2 (append l2 (list (nth (+ i x3) lst2))))
  (setq l2 (append l2 (list (nth i l1))))
)
(setq l2 (append l2 (list (nth i l1))))
      )
      (setq i (1+ i))
      )
    (setq outlst (append outLst (list l2)))
    (setq j (1+ j))
    )
  outLst
)

And now I'm working on the setRows version of the above function.

 
Oh darn, just saw the MXM routine Gile posted.  Here's my version and again probably 2 hours
to figure out:

Code: [Select]
(defun MultiplyLists (lst1 lst2 / i outLst)
  (setq i 0 outLst nil)
  (while (< i (length lst1))
    (setq outLst (append outLst (list (mapcar '* (nth i lst1) (nth i lst2)))))
    (setq i (1+ i))
    )
  outLst
  )

TIA for ANY help  :-D

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Army Ants Recruit Training (lisp challenge)
« Reply #11 on: June 19, 2009, 09:37:13 PM »

Paul,

The first thing I'd do is give your variables meaningfull names. ( self documenting)

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

pkohut

  • Guest
Re: Army Ants Recruit Training (lisp challenge)
« Reply #12 on: June 19, 2009, 09:38:41 PM »

I'll just glanced through the docs

Can you confirm :
How much food can the ant carry ?
How does the ant know it has food ?

Because no data can be saved ( the ant can ONLY use the instance of data transmitted [ ultimate tabula rasa ] ),
I assume the ant will never know when it's 'time cycle' will expire .. so any logic governing what to do next will be based on instant gratification ??

It is instant gratification, from cycle to cycle the ant itself does not now if it was or was not carring food.  If food happens to be
available it can choose to carry or not.  As far as the time cycle, ya, the ant knows absolutly nothing except what its told about
about the suroundings.

Ant can only carry 1 food item.

pkohut

  • Guest
Re: Army Ants Recruit Training (lisp challenge)
« Reply #13 on: June 19, 2009, 09:42:43 PM »

Paul,

The first thing I'd do is give your variables meaningfull names. ( self documenting)



 :-D  Normally very good at that and it started out that way, but this excercise has
been completely at the mercy of the vlide command line, retyping over, and over, and
over, and and the names just kept getting shorter and shorter.  :roll:

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Army Ants Recruit Training (lisp challenge)
« Reply #14 on: June 20, 2009, 12:43:42 AM »
Couple of quickies, after midnight so off to bed.
Code: [Select]
(defun c:test()
  (print
  (BBL
    '((-99 -99 -99 0 -99) (-99 0 -99 -99 -99) (-99 -99 1 0 -99) (-99 -99 0 0 0) (-99 -99 0 0 0))
    -99)
  )
  (princ)
  )

(defun BBL(lst val / result)
    (if (atom (car lst))
     (mapcar
       (function(lambda(x) (setq result (cons  (if (eq x val) 1 0) result)))) lst)
     (mapcar
       (function (lambda(x) (setq result (cons (BBL x val)  result)))) lst)
    )
  (reverse result)
)


Code: [Select]
(defun c:test2()
  (print
    ;;  return column 2 of rows 0 & 2
  (GetArray '(0 2) ; rows
            '(2)   ; columns
    '(((1 2 3 4) (5 6 7 8)(9 10 11 12)) ; row 1
      ((21 22 23 24) (25 26 27 28)(29 30 31 32))
      ((-1 -2 -3 -4) (-5 -6 -7 -8)(-9 -10 -11 -12))
    )
            )
  )
  (princ)
  )

;; no error checking, base zero
(defun GetArray (rows cols lst / data)
  (foreach row rows
    (foreach col cols
      (setq data (cons (nth col (nth row lst)) data))
    )
  )
  (reverse data)
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

pkohut

  • Guest
Re: Army Ants Recruit Training (lisp challenge)
« Reply #15 on: June 20, 2009, 06:01:29 AM »
Thanks Cab, those updates look very helpful.  The setCols function that was posted
is now cleaned up and renamed to setMxColsFromArry and there is now
a setMxRowFromArray function.  Test1 and test2 drives some test data for the
functions.

Thanks,
Paul

Code: [Select]
(defun c:test1 ( / mx lst m)
  (setq mx (list (list 1 2 3 4 5) (list 6 7 8 9 10) (list 11 12 13 14 15) (list 16 17 18 19 20) (list 21 22 23 24 25)))
  (setq lst (list 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116))
 
  (setq m (setMxColsFromArray mx 0 0 4 lst 0))
  (setq m (setMxColsFromArray mx 1 0 2 lst 5))
  (setq m (setMxColsFromArray mx 2 2 4 lst 5))
  (setq m (setMxColsFromArray mx 3 1 3 lst 4))
  (princ)
)

(defun c:test2 ( / mx lst m)
  (setq mx (list (list 1 2 3 4 5) (list 6 7 8 9 10) (list 11 12 13 14 15) (list 16 17 18 19 20) (list 21 22 23 24 25)))
  (setq lst (list 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116))
 
  (setq m (setMxRowsFromArray mx 0 0 4 lst 0))
  (setq m (setMxRowsFromArray mx 1 0 2 lst 5))
  (setq m (setMxRowsFromArray mx 2 2 4 lst 5))
  (setq m (setMxRowsFromArray mx 3 1 3 lst 4))
  (princ)
)

;;; Substitutes input matrix items on row# along fromColumn to toColumn, from items in input array
;;; beginning at lstStartPos (position).
;;; Returns modified input matrix.
;;; zero based, no bounds or error checking
(defun setMxColsFromArray (mxIn row fromColumn toColumn arrayIn lstStartPos / i j mxNew lstTmp lstMxRow)
  (setq i 0
j 0
mxNew nil
  )
  (while (< j (length mxIn))
    (setq lstMxRow (nth j mxIn)
  i  0
  lstTmp nil
    )
    (while (< i (length lstMxRow))
      (if (= row j)
(if (and
      (>= i fromColumn)
      (<= i toColumn)
      )
  (setq lstTmp (append lstTmp (list (nth (+ i lstStartPos) arrayIn))))
  (setq lstTmp (append lstTmp (list (nth i lstMxRow))))
)
(setq lstTmp (append lstTmp (list (nth i lstMxRow))))
      )
      (setq i (1+ i))
    )
    (setq mxNew (append mxNew (list lstTmp)))
    (setq j (1+ j))
  )
  mxNew
)

;;; Substitutes input matrix items at column# in rows fromRow to toRow, from items in input array
;;; beginning at lstStartPos (position).
;;; Returns modified input matrix.
;;; zero based, no bounds or error checking
(defun setMxRowsFromArray (mxIn column fromRow toRow arrayIn lstStartPos / i j k mxNew lstTmp lstMxRow)
  (setq i 0
j 0
mxNew nil
k 0
  )
  (while (< j (length mxIn))
    (setq lstMxRow (nth j mxIn)
  i  0
  lstTmp nil
    )
    (while (< i (length lstMxRow))
      (if (= column i)
  (if (and
(>= j fromRow)
(<= j toRow)
      )
    (setq lstTmp (append lstTmp (list (nth (+ k lstStartPos) arrayIn)))
  k (1+ k))
    (setq lstTmp (append lstTmp (list (nth i lstMxRow)))
  k (1+ k))
  )
  (setq lstTmp (append lstTmp (list (nth i lstMxRow))))
)
      (setq i (1+ i))
    )
    (setq mxNew (append mxNew (list lstTmp)))
    (setq j (1+ j))
  )
  mxNew
)