[comp.sys.handhelds] HP48 Goal Seeking Engine

akcs.joehorn@hpcvbbs.UUCP (Joseph K. Horn) (05/02/91)

This is being posted for a friend.

-------------------------------------

GSE1 Directory - Goal Seeking Engine version 1 - 4/91
Copyright 1991, Jeff Duncombe

 [Note: Included on EduCALC Goodies Disk #3 with Jeff's permission.
 -jkh-]

What if one program could find out the fewest number of
floppies to copy your enormous number of files onto?  Or maybe
balance your checkbook?  What about making perfectly timed audio
cassettes?

GSE is a utility for finding what combination of a group of
numbers will total to a goal number (within a certain accuracy).
There are many applications that are tailor-made to this type of
an engine; three are provided for example.

The basic stack setup is as follows:

3:[Vector of numbers]
2:Goal=====>
1:Accuracy1:[Answer Vector]

For example, assume there are a group of numbers: 6, 4, 3, 2.
What combination of these numbers will equal 7?

Load the stack as follows:
3: [6 4 3 2]yields
2: 7=====>
1: 01: [0 4 3 0]

This means that of the four item list, the combination of 4 and 3
will yield the goal of 7.

Not very exciting, you say?  Here's a better one: you have a
directory of 8 files (100K, 230K, 310K, 130K, 170K, 50K, 260K, 190K).
Assume that you want to put these onto the fewest number of 360K
floppies.  If you total them (put the vector in level 1 and CNRM),
you will find they total 1440K, or 4 x 360K disks.  But if you use
the standard COPY command, they will end up taking 5 diskettes, as
follows:

100K   230K |  310K  | 130K   170K |  50K   260K |   190K
  Disk 1    | Disk 2 | Disk 3    |Disk 4 |  Disk 5

This is a problem for GSE! it will find you a way using only 4 disks!
(Hint: It is a good idea to have the original vector in 3 and 4, so
a copy will still be available).

Load the stack:
4:[100 230 310 130 170 50 260 190]
3:[100 230 310 130 170 50 260 190]
2:360=====>2:[100 230 310 130 ...]
1:01:[100 0 0 0 0 0 260 0]

The first answer! 100K and 260K should fill the first disk! Notice
that flag 1 is set on the screen, which means that the goal of 360
was found within the desired accuracy of 0.  If flag 1 was clear, the
best answer would be displayed, but it wouldn't be within the limits.

Now do a [-] (the minus key) and [ENTER].  This will take out the
known solution from the list and leave you with the next argument
(properly DUPlicated). Fill 1 & 2 with the desired info and try
it again:

4:[0 230 310 130 170 50 0 190]
3:[0 230 310 130 170 50 0 190]
2:360=====>2:[0 230 310 130 17...]
1:01:[0 230 0 130 0 0 0 0]

The next answer is 230K and 130K.  After a [-], [ENTER], 360 and 0, 
you continue:

4:[0 0 310 0 170 50 0 190]
3:[0 0 310 0 170 50 0 190]
2:360=====>2:[0 0 310 0 170 50...]
1:01:[0 0 310 0 0 50 0 0]

The third disk shall hold the 310K file and the 50K file. [-], [ENTER],
360, 0 :

4:[0 0 0 0 170 0 0 190]
3:[0 0 0 0 170 0 0 190]
2:360=====>2:[0 0 0 0 170 0 0 ...]
1:01:[0 0 0 0 170 0 0 190]

The fourth and final disk should therefore hold 170K and 190K.

The possibilities are endless.As another example, say that
you needed to balance your checkbook.  Just type in a vector of all
the checks that you wrote, then set your final balance as the goal
(don't forget any little bank fees).  Follow the example below:

You've written checks for $32.56, $27.90, $130.21, and $46.35. 
Your total for checks written is $106.81. Solve:

3:[32.56 27.90 130.21 46.35]
2:106.81=====>
1:01:[32.56 27.9 0 46.35]

The check for 130.21 must still be uncashed.

As the last example, I will use the problem that inspired me to write
this program in the first place.  I want to tape some songs from CD 
to audio cassette.  The cassette is 45 minutes on each side, for a 
total of 90 minutes.  There should be a way to place the songs
selectively on each side so as to fit every song in its entirety.
This is more difficult than it sounds, if you don't have a computer, 
because there are generally 20+ songs to deal with.  Anyway, here
are the song lengths (in seconds):

[540 482 669 841 334 592 604 612 381 344]

The goal is 2700 seconds (45 minutes).The accuracy will be 0.

4:[540 482 669 841 ...]
3:[540 482 669 841 ...]
2:2700=====>2:[540 482 669 841 ...]
1:01:[540 0 0 841 334 ...]

The first side of the tape must contain the songs the are lengths
540, 841, 334, 604, and 381 for a total of exactly 2700 seconds.  A
simple [-] and you get what songs go on the second side (482, 669, 
592, 612, and 344).

(Note: I have tried this method, and it works amazingly well.)

That's it for the examples. Here are just a few random specs to help 
you on your way:

* This engine uses error checking, so don't worry about your arguments
causing a system crash.

* Flags 1, 2, & 3 are used by this program.  They are all cleared at
the beginning and modified during the course of the execution.Their
descriptions are as follows:

SetClear

Flag 1: The answer is withinThe answer is the closest to
the desired accuracy.the accuracy as possible, but
NOT within it.

Flag 2: The argument is a two-The program runs normally.
dimensional array. The
arguments are left alone.

Flag 3: (This flag is always clear at the end of the engine)

* If a complex vector is given as an argument, it will be converted
to the real part only.Example: [(2,3) (4,5) (6,4) (3,2)] would be
considered to be [2 4 6 3]. 

Note: The [-] still works with a complex and a real-only vector, as
shown:

2:[(2,3) (4,5) (6,4)] [-]
1:[0 4 0]=====>1:[(2,3) (0,5) (6,4)]

* Although GSE appears to be the only program in the GSE1 directory,
there are three "hidden" files called GSEV, GSER, and MMUL.  These
programs are subroutines of GSE, and should not be used alone, for
they can cause a Memory Clear if not used correctly.  GSE uses them
correctly, so they are hidden from the VAR menu.  But this means
that the GSE program cannot be removed from the GSE1 directory and
used alone.  If you wish to call GSE from your own software, you
may make the GSE1 directory into a library.  [See USRLIB on EduCALC
Goodies Disk #1.  -jkh-]

Now for the disclaimer:
This program contains undocumented features of the HP 48SX and
consequently I bear no responsibility for any damage it may cause.
(I haven't ever had a problem, though).

If you decide that this engine is worth while to use in your personal
or commercial applications, I would appreciate some compensation for
this.  This first version contains only the "bare-bones" of what I 
have envisioned this application to be.  Future releases shall include 
batch goal-seeking (having multiple goals with the same vector and
minimizing the overall difference), speed enhancements (in theory
there should be a big future for this one), and some bonus features.
Remember, the only way to be alerted to these changes is to register
for that privilege.  Send your donations to:

Jeff Duncombe
PO Box 20098
Fountain Valley, CA 92708

(Suggested amounts are $10 for personal use, commercial royalties may 
vary.  Contact me for details.)

Happy Goal Seeking!

ASC-encoded GSE1 directory; use ASC-> or ASC2BIN to convert.

%%HP: T(3)A(D)F(.);
"69A20FF7174000000040D4D455C440D9D20E16321C432D6E201087D6E201097E
1632D6E2010978B9C1EB3A19C2A2DBBF10A132D6E201096D6E201087D6E20109
66C7D1D6E201097D6E2010966C7D1EEDA1C4232D6E2010878B9C1900D1EF5329
3632B21307B000407435542540D9D20E16323CE229C2A2973C1AFE22D9D20940
405233094040523301C432D6E201096E163233032D6E201096530402E230EBBE
1D5032D9D20C1216D6E201096B68D12A6F088130B20402E230D6E2010969C2A2
ED4D1B20405031644230E9330C1216477F0F1AA188130350402E2303CE22EBBE
1AFE22D9D2088130940405031644230E9330B20402E230F30405031644230E93
3088130C12163CE22CFCE1AFE22D9D209C2A2D82C1B21305DF22B21305DF227A
726C12163CE22EBBE1AFE22D9D2088130D6E2010969C2A22A6F035040E933035
040E933035040E933084E204074355425B21305BF22D9D2091136B21305DF22E
F116D6E201096B68D1940405233032230477F0120402E230D6E2010964B2A2ED
4D1120405031644230E933045632D6E201096976320890244230B213049632EF
53244230B21305BF22D9D209404052330940405233085230B21305DF2293632B
21306B200407435546540D9D20E16325923088130AC7C1E90168B9C18B9C13CE
229C2A2D9AE1AFE22D9D20ED2A2D82C1B21305BF22D9D2032230881303CE224B
2A2EBBE1AFE22D9D2059230599A1CAF06599A13F2A2D82C1B21305DF2232230F
1AA1B21305DF2293632B2130FC00000339200000000000000000710003074355
430D9D20E1632D9D200FE81C2F46D9D20E16329C2A2EE2C1ED2A2EE2C13F2A2E
E2C184E2040743554653CE22ED2A2973C1AFE22D9D20EF116881308B9C188130
EB3A19C2A22A6F0AF016881304B2A28813094040E9330681D1AF0169C2A28813
094040E9330681D14CB26EBFB14CB2684E20407435542585230322306272684E
2040D4D455C43223044230B21305DF223CE223F2A21A4C1AFE22599A15DF2293
632B2130B213093632B21304887"

'GSE1' BYTES --> #7884h, 756.

And here it is uuencoded:

BEGIN--cut here--CUT HERE--
begin 600 gse1
M2%!(4#0X+466*O!_<00````$34U53`2=+>!A(\$TTN8"`7AM+A"0YV$C;2X0
MD(>;'+ZCD2PJO?L!&B-M+A"0UN8"`7AM+A"09GP=;2X0D-?F`@%IQM?AWAI,
M,M+F`@%XN,F1`!W^-9)C(RLQ<`L`!$=315($G2W@82/#+I(L*GG#H>\BG2V0
M!`0E,Y`$!"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