• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Algorithm Help

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Righty-ho chaps, here's the situation:

I'm creating a program that simulates traffic flow. I'm currently implementing speed limit zones. A "Limit" object (below) contains the value for the speed and a value for the distance along the simulation at which this limit comes into effect (so a Limit with speed = 50 and pos = 500 would be a 50m/s limit starting at the 500m point in the simulation).

A Vehicle object contains, amongst other things, a value "X", representing the distance along the simulation. I intend to use Vehicle.X to check the limit at which the vehicle should be travelling.

The class LimitSet extends Vector as a collection of Limit objects. My problem lies within the method checkLimit. This method accepts a Vehicle object and uses its "X" value to determine what limit should be applied. It does this by running through all the Limit objects currently stored (in ascending order of "pos", with LimitSet[0].pos = 0) and returning the speed value of the approriate Limit object.

It first checks to see if there is more than one Limit object in the collection - if not then there is no need to perform any calculations and it simply returns the single limit. If there are multiple Limits to check then it loops through until the right one if found.

I'm having problems, however, getting the algorithm to work in a simulation using more than one vehicle and more than one limit. Single vehicles can follow and limit pattern, and convoys can follow one limit. Two or more cars going through different speed zones result in ArrayIndexOutOfBounds errors, wrong limits being obeyed and more, depending on what I change to try and fix it.

I was hoping someone could cast a more experienced eye over the algorithm and see if they can find a working, generic formula that gives the right value for any situation.

Cheers.





 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't trace through all the code, but I'll tell you right off the bat that extending Vector with type-specific methods is a really bad design, for one important reason: code can call the not-overridden non type-specific methods and generally much things up. In fact, that's what I think is probably happening here: compare the one and two-argument forms of addLimit(), and note how one neglects to set the index of the limit object. This will give rise to multiple Limit objects all with a zero index, presumably a bad thing. Frankly, it's also a bad design to have the Limit object hold the index, if you're trying to keep that index synced with the index in the Vector; as you can see here, that's a hard thing to do, especially as the Vector changes.

Thirdly, never use Vector in new code. ArrayList is intended as its replacement.

Now, for the fix: instead of extending ArrayList, have a member variable of type ArrayList, and use it internally. Instead of having Limit hold an index, just use the index you find it at. Get these things straightened out, and your redesigned program may well work.
 
John Brookes
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aah thanks, I've got you. When I added the index to the Limit object itself I really didn't like the idea, but it was a bodge job I was hoping would get around certain issues - the code isn't all mine, as I'm just modifying an existing program as part of a Uni project. I'll stick those ArrayLists in and try it again.

No doubt I'll be back later

Thanks again.
 
John Brookes
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right, here's the new code for LimitSet:




I added the ArrayList and changed all instances of Limit.index to use the index from the ArrayList itself. There were some problems using JComboBox.setSelectedItem and such and I needed a way to get an index using just a Limit item, hence the "LimitSet.getIndex(Limit in)" method. I'm not too keen on this, however, because I'm not sure how robust the whole "in.equals(limitset.get(i))" statement is.


Either way, the algorithm still doesn't work. I think the problem is down to what values I use and in which combination for calculating the loop controls, and what value to return based on "count".

Basically, the algorithm goes through each Limit in the set and checks if the speed zone's starting distance is less than (or equal to) the car's current displacement, X. If it is, then the vehicle has passed through this "barrier" meaning it could still be in the zone or it could have gone through into the next one. In this case it goes on to check the next zone (count++, loop through) and checks again. When the expression cannot be satisfied it means that the program has found the first zone which the vehicle CANNOT HAVE ENTERED. By definition, this means the vehicle is in the preceding zone. The loop is marked as finished (finished = true) and the program drops out of it, returning the limit at "count-1" (i.e. the preceding zone).

In it's current state, the program ignores the final zone in the ArrayList, but does continue to function without system error. I can't for the life of me figure out what's wrong with it!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic