Skip to content

A solution that sporadically works up to #4

Paul Houle edited this page Jan 23, 2015 · 3 revisions

I got this far just messing around. I write Java everday but Javascript only occasionally

{
    init: function(elevators, floors) {
        var cnt=floors.length
        
		for(var i=0;i<cnt;i++) {
           floors[i].on("up_button_pressed", function() {
                elevators[0].goToFloor(this.floorNum())
           })
           
           floors[i].on("down_button_pressed", function() {
                elevators[1].goToFloor(this.floorNum())
           })
		}
        
        for(var i=0;elevators.length;i++) {
            elevators[i].on("floor_button_pressed", function(floorNum) {
                this.goToFloor(floorNum)
            })
        }
        
        elevators[0].on("idle", function() { this.goToFloor(0)})
        elevators[0].on("idle", function() { this.goToFloor(7)});
    },
    update: function(dt, elevators, floors) {
        // We normally don't need to do anything here
    },
    flip: function() {
        return Math.floor(2.0*Math.random());  
    }
}

I am still getting my head around the API and how best to use it. I am also trying to avoid creating data structures to put off the question of having any real architecture, which involves having a strategy for managing asynchronous communications.

One elevator is allocated to carry people up and another to carry down. This may be right if 50% of traffic is up and 50% is down but I don't know that right now. Certainly this strategy will break down on #5 when you have more elevators.

My guess is that if I had the right tools to run simulations, this could be tuned up to remove a little fat and then succeed consistently, but I think that's a dead end, and a next generation solution may try suboptimization of (i) choosing which elevator to load people on and (ii) how to get people out of the elevators.

I think ANY strategy is going to need parametric tuning to put up the best score. Some call this "machine learning"; the most fruitful strategy, if complex, could be something like the methods used for computer chess because the search space is not very big.

Clone this wiki locally