Coding Suggestions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Batin
Forum Newbie
Posts: 4
Joined: Tue Dec 30, 2008 5:14 pm

Coding Suggestions

Post 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
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Coding Suggestions

Post by jaoudestudios »

Is this your school home work?
Batin
Forum Newbie
Posts: 4
Joined: Tue Dec 30, 2008 5:14 pm

Re: Coding Suggestions

Post 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.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Coding Suggestions

Post 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
Batin
Forum Newbie
Posts: 4
Joined: Tue Dec 30, 2008 5:14 pm

Re: Coding Suggestions

Post 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 :)
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Coding Suggestions

Post 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! :)
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Coding Suggestions

Post 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.
Batin
Forum Newbie
Posts: 4
Joined: Tue Dec 30, 2008 5:14 pm

Re: Coding Suggestions

Post 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 :)
User avatar
volomike
Forum Regular
Posts: 633
Joined: Wed Jan 16, 2008 9:04 am
Location: Myrtle Beach, South Carolina, USA

Re: Coding Suggestions

Post 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.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Coding Suggestions

Post 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?
silenceghost
Forum Newbie
Posts: 22
Joined: Sun Oct 19, 2008 3:25 am

Re: Coding Suggestions

Post 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());
 
Post Reply