Page 1 of 1

Coding Suggestions

Posted: Tue Dec 30, 2008 5:21 pm
by Batin
Hi all,

Here's the scenaro. Let's say a train track company wants to work out how many pieces of track they'll need to use for a certain length of planned track.

Track pieces come in different sizes and the track company needs a way of finding out the number of track pieces needed to cover the length planned as well as at the same time being economic (so it needs to select the different sizes of tracks that make up the whole length with minimum wastage)

Available Track Lengths are
2m
1m
0.5m
0.25m

So if the planned total length was 10m, they'd use 5 x 2m lengths. If it was 9m, then 4 x 2m and 1 x 1m. And so on

How could you automate this?

Cheers,

Pete

Re: Coding Suggestions

Posted: Tue Dec 30, 2008 5:28 pm
by jaoudestudios
Is this your school home work?

Re: Coding Suggestions

Posted: Tue Dec 30, 2008 5:35 pm
by Batin
lol no. I'm 27 and until now thought I was pretty good at PHP (I'm a self employed PHP Developer).

They're actually a curtain track manufacturer for the theatre industry wanting an online quote tool.

Thought about taking the planned length and dividing it between the larget track piece first and then working my way down with the remainder.

But it's messy, must be a cleaner approach to it.

Re: Coding Suggestions

Posted: Tue Dec 30, 2008 5:59 pm
by califdon
Yeh, I was wondering about railroad tracks that short! :lol:

What you described is known as "linear programming" in mathematics (the 'programming' does not refer to computer programming, by the way). I haven't done any of it since I was in college (long before you were born!), but there's a good general explanation in Wikipedia http://en.wikipedia.org/wiki/Linear_programming and I'm sure if you search Google for some of those terms, you'll find something that relates to what you need. For example, here's one I found: http://webscripts.softpedia.com/script/ ... 34607.html

Re: Coding Suggestions

Posted: Tue Dec 30, 2008 6:08 pm
by Batin
califdon wrote:but there's a good general explanation in Wikipedia http://en.wikipedia.org/wiki/Linear_programming
Woah, to me that's some serious math! Will look into it more though, term I'm not familiar with and will bug me until I can understand it lol. Thanks for the tip :)

Re: Coding Suggestions

Posted: Tue Dec 30, 2008 7:12 pm
by califdon
Batin wrote:Woah, to me that's some serious math! Will look into it more though, term I'm not familiar with and will bug me until I can understand it lol. Thanks for the tip :)
Oh it is serious math, indeed. I never did too well at it, as I recall. But if you're going to solve that kind of problem, it's what you have to use. Good luck! :)

Re: Coding Suggestions

Posted: Tue Dec 30, 2008 7:16 pm
by Mark Baker
Does this problem include curved sections of track? In which case you may need shorter straight lengths on the approach to each curve as well.

Re: Coding Suggestions

Posted: Tue Dec 30, 2008 7:34 pm
by Batin
Mark Baker wrote:Does this problem include curved sections of track? In which case you may need shorter straight lengths on the approach to each curve as well.
Very good question, they can indeed have curved components but this hasn't been mentioned in spec so I'm going to say ignore it :)

Re: Coding Suggestions

Posted: Wed Dec 31, 2008 1:07 am
by volomike
jaoudestudios wrote:Is this your school home work?
hahaha. I was thinking the same thing. A few posts on this site, in fact, have started to look like school homework. It's hard to discern.

Re: Coding Suggestions

Posted: Wed Dec 31, 2008 1:34 am
by onion2k
Batin wrote:Thought about taking the planned length and dividing it between the larget track piece first and then working my way down with the remainder.

But it's messy, must be a cleaner approach to it.
That sounds like a pretty sensible approach to me. Why is it messy?

Re: Coding Suggestions

Posted: Wed Dec 31, 2008 11:25 pm
by silenceghost
Hey hope this code will automate your problem just specify the higher track in array_track descending order you will get an array i hope this is a quick and dirty code hope you can optimize it is not valid economical optimization for all track
if you have 25 available track it will return
Array ( [0] => 7 [1] => 7 [2] => 7 [3] => 0.5 [4] => 0.5 [5] => 0.5 [6] => 0.5 [7] => 0.5 [8] => 0.5 [9] => 0.5 [10] => 0.5 )

actuall optimization i think it would be
[0]=>7 [1] => 6 [2] =>6 [3] =>6
which i think it is very difficult to write the programe

Code: Select all

 
function getTrackArray(){
     $total_available=15;
     $array_track=array(7,6,0.5,0.25);
    $array_string=array();
  while(true){
 
    foreach($array_track as $value){
        if($value<=$total_available){
            $array_string[]=$value;
            $total_available=$total_available-$value;
            break;
        }
    }
    if($total_available<=0)
        break;
  }
    return $array_string;
}
print_r(getTrackArray());