I pushed forward and implemented a solution using Alex's amb rosettacode
solution. It's probably not optimal but it runs really quick 0.08 seconds
for 100 acres. It grows to 5 seconds for 1000 acres.

Please forgive any style issues

"Example: A farmer has 100 acres on which to plan oats or corn. Each acre
of oats requires $18 capital and 2 hours of labor. Each acre of corn
requires $36 capital and 6 hours of labor. Labor costs are $8 per hour. The
farmer has $2100 available for capital and $2400 available for labor. If
the revenue is $55 from each acre of oats and $125 from each acre of corn,
what planting combination will produce the greatest total profit? What is
the maximun profit?"

(be amb (@E @Lst)
   (lst @E @Lst) )

(setq Oats (new NIL '+Choice 'capital 18 'labor 2 'revenue 55))
(setq Corn (new NIL '+Choice 'capital 36 'labor 6 'revenue 125))
(setq LaborCost 8)
(setq CapitalAvailable 2100)
(setq LaborAvailable 2400)

(de laborCost (OatAcre CornAcre)
    (+
        (* LaborCost (* OatAcre (get Oats 'labor)))
        (* LaborCost (* CornAcre (get Corn 'labor))) ) )

(de capitalCost (OatAcre CornAcre)
    (+
        (* OatAcre (get Oats 'capital))
        (* CornAcre (get Corn 'capital))) )

(de totalRevenue (OatAcre CornAcre)
    (+
        (* OatAcre (get Oats 'revenue))
        (* CornAcre (get Corn 'revenue))) )

(be isValid (@OatsAcre @CornAcre)
    (@Labor laborCost (-> @OatsAcre) (-> @CornAcre))
    (@Capital capitalCost (-> @OatsAcre) (-> @CornAcre))
    (@Revenue totalRevenue (-> @OatsAcre) (-> @CornAcre))

    (@Valid and
        (<= (-> @Capital) CapitalAvailable)
        (<= (-> @Labor) LaborAvailable) )
    (@ push 'Solutions (list (-> @Revenue) (-> @OatsAcre) (-> @CornAcre)))
    (or
      ((equal @Valid T))
      ((amb @ NIL)) ) )

(be problem ((@Choice1 @Choice2))
    (@Acres range 1 100)
    (amb @Choice1 @Acres)
    (amb @Choice2 @Acres)
    (isValid @Choice1 @Choice2) )

(off Solutions)
(bench (solve '((problem @X))) T)
(printsp "Maximal solution: " (maxi val Solutions)

: .083 sec
: Maximal solution: (6890 48 34)

Which is really close to the real number solution given in the problem

"The values in B5 and B6 should have become 50 and 33.3, and that the
maximum profit is $6916.6 and your spreadsheet will look like the
following. Thus x=50 and y=33.3 are the number of acres that the farmer
should devote to oats and corn, respectively. "

Fun!


On Thu, Jul 26, 2012 at 12:39 PM, Alexander Burger <a...@software-lab.de>wrote:

> Hi Joe,
>
> > Thank you. My researched suggested that it was possible with other prolog
> > implementations so I wasn't sure if I was missing something simple in
> pilog.
>
> Should be no problem to translate it, if you have a solution in Prolog.
> But, as we said, it is probably not really useful.
>
>
> > I also wasn't sure if with a simple problem space I could combine
> something
> > like permute with a known range of possibilities.
>
> Yeah, this might work in this special case. But for general linear
> programming problems, other algorithms are much better.
>
> Cheers,
> - Alex
> --
> UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe
>

Reply via email to