Re: pilog solver

2012-07-29 Thread Alexander Burger
Hi Joe,

thanks for sharing this!

> Please forgive any style issues

The style looks perfect! :)


A minor issue might be the '+Choice' argument in

> (setq Oats (new NIL '+Choice 'capital 18 'labor 2 'revenue 55))
> (setq Corn (new NIL '+Choice 'capital 36 'labor 6 'revenue 125))

It looks like a class (due to the '+' convention), but if Oats and Corn
are supposed to be objects, then the class(es) must be a list. So I
would suggest

   (setq Oats (new '(+Choice) 'capital 18 'labor 2 'revenue 55))
   (setq Corn (new '(+Choice) 'capital 36 'labor 6 'revenue 125))

This is ok even if no class '+Choice' is defined, as the code doesn't
use any OO-message-passing (yet).

Cheers,
- Alex
-- 
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe


Re: pilog solver

2012-07-28 Thread Joe Bogner
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 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
>


Re: pilog solver

2012-07-26 Thread Alexander Burger
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


Re: pilog solver

2012-07-26 Thread Joe Bogner
My interest also piqued when I saw the amb example in rosettacode. It seems
like that could be combined with something to backtrack if a permutation
goes out of bounds.

http://www.randomhacks.net/articles/2005/10/11/amb-operator

On Thu, Jul 26, 2012 at 12:21 PM, Joe Bogner  wrote:

> 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.
>
> I also wasn't sure if with a simple problem space I could combine
> something like permute with a known range of possibilities. In the example
> below, I could generate a list of the whole numbers from 1-100 for the
> acreage and then have some helper functions to test for ranges. I wouldn't
> need to solve for a real number.
>
> It sounds like there are better ways to do it. I was hoping to come up
> with something as nicely declarative/expressive as pilog that didn't have a
> bunch of conditionals. I could probably use some dsl though. I have no
> experience with pilog and was interested in an application of it.
>
> Thanks again
>
>
>
>
> On Thu, Jul 26, 2012 at 11:31 AM, Alexander Burger 
> wrote:
>
>> On Thu, Jul 26, 2012 at 04:02:07PM +0200, Henrik Sarvell wrote:
>> > I can't say for sure if prolog is a good fit or not. The problems seems
>> a
>> > little bit too arithmetic maybe but do not trust my word on it.
>>
>> I also don't think that Prolog or Pilog are well suited for that.
>>
>> Linear programming is an optimization technique, often employing the
>> simplex algorithm where you solve a number of linear equations by
>> "pivot"ing their terms. I think this can be easier solved in plain Lisp
>> than in Pilog.
>>
>> Cheers,
>> - Alex
>> --
>> UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe
>>
>
>


Re: pilog solver

2012-07-26 Thread Joe Bogner
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.

I also wasn't sure if with a simple problem space I could combine something
like permute with a known range of possibilities. In the example below, I
could generate a list of the whole numbers from 1-100 for the acreage and
then have some helper functions to test for ranges. I wouldn't need to
solve for a real number.

It sounds like there are better ways to do it. I was hoping to come up with
something as nicely declarative/expressive as pilog that didn't have a
bunch of conditionals. I could probably use some dsl though. I have no
experience with pilog and was interested in an application of it.

Thanks again




On Thu, Jul 26, 2012 at 11:31 AM, Alexander Burger wrote:

> On Thu, Jul 26, 2012 at 04:02:07PM +0200, Henrik Sarvell wrote:
> > I can't say for sure if prolog is a good fit or not. The problems seems a
> > little bit too arithmetic maybe but do not trust my word on it.
>
> I also don't think that Prolog or Pilog are well suited for that.
>
> Linear programming is an optimization technique, often employing the
> simplex algorithm where you solve a number of linear equations by
> "pivot"ing their terms. I think this can be easier solved in plain Lisp
> than in Pilog.
>
> Cheers,
> - Alex
> --
> UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe
>


Re: pilog solver

2012-07-26 Thread Yiorgos Adamopoulos
On Thu, Jul 26, 2012 at 4:11 PM, Joe Bogner  wrote:
> I'd like to explore the use of pilog for solving linear programming
> problems. I'm not exactly sure where to start because I'm just starting to
> learn prolog.

Maybe http://www.eclipseclp.org/
-- 
http://gr.linkedin.com/in/yiorgos
-- 
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe


Re: pilog solver

2012-07-26 Thread Alexander Burger
On Thu, Jul 26, 2012 at 04:02:07PM +0200, Henrik Sarvell wrote:
> I can't say for sure if prolog is a good fit or not. The problems seems a
> little bit too arithmetic maybe but do not trust my word on it.

I also don't think that Prolog or Pilog are well suited for that.

Linear programming is an optimization technique, often employing the
simplex algorithm where you solve a number of linear equations by
"pivot"ing their terms. I think this can be easier solved in plain Lisp
than in Pilog.

Cheers,
- Alex
-- 
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe


Re: pilog solver

2012-07-26 Thread Henrik Sarvell
I can't say for sure if prolog is a good fit or not. The problems seems a
little bit too arithmetic maybe but do not trust my word on it.

Otherwise it seems like a typical curve examination problem to find all the
maxima, the maxima with the highest profit is the solution.



On Thu, Jul 26, 2012 at 3:11 PM, Joe Bogner  wrote:

> Hi,
>
> I'd like to explore the use of pilog for solving linear programming
> problems. I'm not exactly sure where to start because I'm just starting to
> learn prolog.
>
> Is it the right tool to solving a problem like this?
>
> "*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?"
>
>  http://galileo.stmarys-ca.edu/jsauerbe/m3f09/solver.html
>
>
> Any examples or guidance on where to look would be very helpful.
>
> Thank you
> Joe
>
>