RetroForum Welcome, Guest. Please login or register.
March 12, 2010, 06:38:35 AM
Home Help Search Calendar Login Register

RetroForth Discussion  |  Main  |  Discussion  |  Topic: help with GP in retroforth « previous next »
Pages: 1 Go Down Print
Author Topic: help with GP in retroforth  (Read 2406 times)
clacker
Newbie
*

Karma: +0/-0
Offline Offline

Posts: 3


View Profile
help with GP in retroforth
« on: October 12, 2007, 05:59:57 PM »

I am trying to write a simple genetic programming framework in retroforth.  I have the basics down as far as what I want to do, but I am having trouble with one part.

I know that using the ' word returns the xt of the string that follows it.  So if my gentic function used + - and * as elements and a variable x1, I could store them to a data structure like this:

Code:
: x1 4 ;
create myCommands 4 cells allot
' + myCommands !
' - myCommands cell+ !
' * myCommands 2 cells + !
' x1 myCommands 3 cells + !

I left out the details in the data structure that handle how many stack elements a function requires and how many it leaves on the stack for simplicity.  I can then put a series of xt elements into a data structure one after the other, keeping track of the number of elements on the stack after each xt so that I never underflow and I end with one element on the stack. 

I could use a for next loop and execute each xt element in turn.  It works but I'd like to speed it up.  What I would like to do is set a pointer to the first xt, and have retroforth execute each element in turn and then return at the end.  Is there a clean way to do this?
Logged
lsa
Newbie
*

Karma: +0/-0
Offline Offline

Posts: 3


View Profile
Re: help with GP in retroforth
« Reply #1 on: October 16, 2007, 12:31:12 AM »

Hello clacker

There are many solutions as you write.
However i think the best is to compile the instructions from your command list, then execute the compiled instructions.
I came up with the code below.


;============================================;
: execute invoke ;

variable x1

create mycommands 5 cells allot
' + mycommands !
' - mycommands cell+ !
' * mycommands 2 cells + !
' x1 mycommands 3 cells + !
0 mycommands 4 cells + !

: >class >entry :class @ ;
: cc dup @ 0; dup literal, >class compile cell+ cc ;
: compile-cmds here mycommands cc drop ['] ; compile ;

10 50 10 20
compile-cmds execute
( GIVES: 200 addr-of-x1 )
Logged
clacker
Newbie
*

Karma: +0/-0
Offline Offline

Posts: 3


View Profile
Re: help with GP in retroforth
« Reply #2 on: October 16, 2007, 03:08:33 AM »

lsa, thanks, that's exactly what I needed.  I can't see why I need to use the >class instead of just ' yet, but it works great.  Is there a reason why you used >class instead of just using the xt?  I was also able to set aside some memory on the heap (using create and allot) and then setting h0 temporarily to that spot so compile compiles the new commands there.

Thanks again.
Logged
lsa
Newbie
*

Karma: +0/-0
Offline Offline

Posts: 3


View Profile
Re: help with GP in retroforth
« Reply #3 on: October 16, 2007, 10:15:42 AM »

Well i haven`t used retroforth for some time but i think this is how it is
implemented at the current version:

XT`s only point to the code, each word has a class, which is the action to
take on the XT.
With variables you can`t execute the XT (since it contains the value), when executing the class it leaves the XT.

You don`t need to use >class if you create variables as ": x1 4 ;"
but variables created by "variable x1" would not be able to execute with "' x1 execute".
Quickly stated: with classes you get the correct action to words.



Logged
Charles Childers
Administrator
Sr. Member
*****

Karma: +2/-0
Offline Offline

Posts: 745


View Profile WWW
Re: help with GP in retroforth
« Reply #4 on: October 16, 2007, 05:18:34 PM »

Two minor clarifications:

Code:
: x1 4 ;

This would actually be a constant, not a variable.

The second is that the XT field in a variable is a pointer to the value, not the value itself.

Aside from that your explaination is correct Smiley
Logged
clacker
Newbie
*

Karma: +0/-0
Offline Offline

Posts: 3


View Profile
Re: help with GP in retroforth
« Reply #5 on: October 17, 2007, 11:37:42 PM »

Thanks, this was exactly what I needed.  The reason I was thinking along the lines of : x1 4 was because I would like to test the functions against a set of data, and I thought that the easiest way would be to use:

variable currentX1
4 currentX1 !

: x1 currentX1 @ ;

Then by changin the variable currentX1, I don't need to change anything else.

As a side note, can I change the value of a constant?  That is probably closer to what I really want but I couldn't tell how to do that.
Logged
Charles Childers
Administrator
Sr. Member
*****

Karma: +2/-0
Offline Offline

Posts: 745


View Profile WWW
Re: help with GP in retroforth
« Reply #6 on: October 18, 2007, 03:07:56 PM »

Quote
As a side note, can I change the value of a constant?  That is probably closer to what I really want but I couldn't tell how to do that.

This can be done with values (not part of the core RetroForth language, but easy enough to add):

Code:
section:
 variable flag
expose
 : .value flag @ if ! flag off ;then @ ;
 : to flag on ;
 : value create 0 , as .value ;
;section

If you don't have section:, expose, or ;section, the code for them is:

Code:
loc:
  variable last-visible   variable last-hidden
  : find-last-visible  last repeat @ dup @ last-hidden @ =if ;then again ;
  here ] ( section: ) last @ last-visible ! ;
  here ] ( expose ) last @ last-hidden ! ;
  here ] ( ;section ) last-visible @ find-last-visible ! ;
;loc  alias ;section  alias expose  alias section:
Logged
lsa
Newbie
*

Karma: +0/-0
Offline Offline

Posts: 3


View Profile
Re: help with GP in retroforth
« Reply #7 on: October 19, 2007, 12:27:51 AM »

If you use:
: x1 20 ;

as a constant value.
You may change it by:

: const! 5 + ! ;
50 ' x1 const!

the "5 +" advances past the "call dolit" which is compiled into the word.

The "values" solution may be cleaner, however.
Logged
Pages: 1 Go Up Print 
RetroForth Discussion  |  Main  |  Discussion  |  Topic: help with GP in retroforth « previous next »
Jump to:  


Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1 RC2 | SMF © 2001-2005, Lewis Media Valid XHTML 1.0! Valid CSS!