diff --git a/python/agent_types.py b/python/agent_types.py index 4aafe7a..813a55f 100644 --- a/python/agent_types.py +++ b/python/agent_types.py @@ -5,7 +5,7 @@ import config as defaults # changeFasterLane = changeFasterLaneBuilder() -changeFasterLane = changeFasterLaneBuilder(likelihood_mult=0.5, speedThreshold=2) +changeFasterLane = changeFasterLaneBuilder(likelihood_mult=0.5, speedThreshold=2, bias=1) basicHumanParams = { "name" : "human", "shape" : "passenger", @@ -20,6 +20,7 @@ "speedDev" : 0.5, "sigma" : 0.75, "tau" : 3, # http://www.croberts.com/respon.htm + # "carFollowModel" : "Wiedemann", # "PWagner2009", # "laneChangeModel": 'LC2013', } diff --git a/python/carfns.py b/python/carfns.py index 95318da..372673d 100644 --- a/python/carfns.py +++ b/python/carfns.py @@ -12,7 +12,7 @@ def randomChangeLaneFn((idx, car), sim, step): def changeFasterLaneBuilder(speedThreshold = 5, likelihood_mult = 0.5, dxBack = 0, dxForward = 60, - gapBack = 10, gapForward = 5): + gapBack = 10, gapForward = 5, bias=0): """ Intelligent lane changer :param speedThreshold: minimum speed increase required @@ -21,6 +21,7 @@ def changeFasterLaneBuilder(speedThreshold = 5, likelihood_mult = 0.5, :param dxForward: Farthest distance forward car can see :param gapBack: Minimum required clearance behind car :param gapForward: Minimum required clearance in front car + :param bias: additive speed bias term (m/s) :return: carFn to input to a carParams """ def carFn((idx, car), sim, step): @@ -32,7 +33,7 @@ def carFn((idx, car), sim, step): continue cars = sim.getCars(idx, dxBack=dxBack, dxForward=dxForward, lane=lane) if len(cars) > 0: - v[lane] = mean([c["v"] for c in cars]) + v[lane] = mean([c["v"] for c in cars]) + bias else: v[lane] = traci.vehicle.getMaxSpeed(car["id"]) maxv = max(v) diff --git a/python/icra.py b/python/icra.py index ed2e8fc..cf91543 100644 --- a/python/icra.py +++ b/python/icra.py @@ -1,4 +1,5 @@ import random +import copy import config as defaults from loopsim import LoopSim @@ -10,27 +11,41 @@ if __name__ == "__main__": - vTypeParams = humanParams - vTypeParams["laneSpread"] = 0 - vTypeParams["tau"] = 0.5 - vTypeParams["speedDev"] = 0.1 - vTypeParams["speedFactor"] = 1 + globalLaneSpread = 0 + + hParams = humanParams + hParams["laneSpread"] = globalLaneSpread + hParams["tau"] = 2 + hParams["speedDev"] = 0.2 + hParams["speedFactor"] = 1.1 + hParams["minGap"] = 1 + + hParams1 = copy.copy(humanParams) + hParams1["name"] = "human1" + hParams1["laneSpread"] = 1 + hParams1["tau"] = 2 + hParams1["speedDev"] = 0.4 + hParams1["speedFactor"] = 1.2 + hParams1["count"] = 0 mParams = midpointParams - mParams["laneSpread"] = 0 + mParams["laneSpread"] = globalLaneSpread + # mParams["tau"] = 0.1 gParams = fillGapMidpointParams - gParams["laneSpread"] = 0 + gParams["laneSpread"] = globalLaneSpread + # gParams["tau"] = 0.1 aParams = ACCParams - aParams["laneSpread"] = 0 + aParams["laneSpread"] = globalLaneSpread + # aParams["tau"] = 0.1 ############### # Option ranges ############### - numLanes = 2 - totVehicles = 22 + numLanes = 1 + totVehicles = 11 ''' sigmaValues = [0.5, 0.9] @@ -39,12 +54,14 @@ robotTypes = (mParams, gParams, aParams) ''' - sigmaValues = [0.9] + sigmaValues = [0.5] numRobotsValues = [0, 1] numRuns = 1 robotTypes = [gParams] - fps = 24 + # WARNING SUMO can't really handle more than 2 fps, probably due to limitations of underlying car following models + # TODO(cathywu) develop our own high resolution car following models? + fps = 2 simStepLength = 1./fps simTime = 100 simSteps = int(simTime/simStepLength) @@ -53,9 +70,9 @@ for sigma in sigmaValues: for numRobots in numRobotsValues: for rParams in robotTypes: - vTypeParams["sigma"] = sigma - vTypeParams["count"] = (totVehicles-numRobots) - rParams["count"] = numRobots + hParams["sigma"] = sigma + hParams["count"] = (totVehicles-numRobots) + rParams["count"] = numRobots if defaults.RANDOM_SEED: print ">>> Setting random seed to ", defaults.RANDOM_SEED @@ -63,7 +80,7 @@ for run in range(numRuns): opts = { - "paramsList" : [vTypeParams, rParams], + "paramsList" : [hParams, hParams1, rParams], "simSteps" : simSteps, "tag" : "Sugiyama-sigma=%03f-run=%d" % (sigma, run), } @@ -72,8 +89,9 @@ print "sigma =", sigma, "numRobots = ", numRobots, print "robot type =", rParams["name"], "run = ", run print "***" - speedRange = (0,30) - sim.simulate(opts, sumo='sumo-gui', speedRange=speedRange, sublane=False) + speedRange = (0, 30) + sim.simulate(opts, sumo='sumo', speedRange=speedRange, sublane=False) + # sim.simulate(opts, sumo='sumo-gui', speedRange=speedRange, sublane=False) sim.plot(show=False, save=True, speedRange=speedRange).close("all") if numRobots == 0: break diff --git a/python/loopsim.py b/python/loopsim.py index 9d48d82..c7eaf31 100644 --- a/python/loopsim.py +++ b/python/loopsim.py @@ -129,24 +129,44 @@ def _addCars(self, paramsList): cars = {} self.numCars = 0 + # Tabulate total number of cars per lane + num_cars_per_lane = [0] * self.numLanes + # Create car list for param in paramsList: self.numCars += param["count"] for i in range(param["count"]): vtype = param["name"] laneSpread = param.get("laneSpread", True) + if laneSpread is not True: + num_cars_per_lane[laneSpread] += 1 carname = "%s-%03d" % (vtype, i) cars[carname] = (vtype, laneSpread) - lane = 0 + lane = -1 carsitems = cars.items() + # Add all cars to simulation ... - random.shuffle(carsitems) # randomly - for i, (carname, (vtype, laneSpread)) in enumerate(carsitems): - x = self.length * i / self.numCars - self._createCar(carname, x, vtype, - lane if laneSpread is True else laneSpread) - lane = (lane + 1) % self.numLanes + random.shuffle(carsitems) # randomly + + # Counters of cars added so far (per lane) + cars_added_per_lane = [0] * self.numLanes + # For lanespead = True, set the default cars per lane + default_cars_per_lane = np.ceil(float(self.numCars) / self.numLanes) + + for carname, (vtype, laneSpread) in carsitems: + if laneSpread is True: + # If lanespread = True, then uniformly set the car positions + lane = (lane + 1) % self.numLanes + lane_total = default_cars_per_lane + else: + # Otherwise, space the cars out evenly per lane + lane = laneSpread + lane_total = num_cars_per_lane[laneSpread] + + x = self.length * cars_added_per_lane[lane] / lane_total + cars_added_per_lane[lane] += 1 + self._createCar(carname, x, vtype, lane) self.carNames = cars.keys() diff --git a/python/sugiyama_jam.py b/python/sugiyama_jam.py index 275b85d..f58bf99 100644 --- a/python/sugiyama_jam.py +++ b/python/sugiyama_jam.py @@ -12,8 +12,11 @@ if __name__ == "__main__": numLanes = 1 + simStepLength = 0.5 + simTime = 500 + simSteps = int(simTime/simStepLength) - sim = LoopSim("loopsim", length=230, numLanes=numLanes, simStepLength=0.5) + sim = LoopSim("loopsim", length=230, numLanes=numLanes, simStepLength=simStepLength) vTypeParams = humanParams vTypeParams["laneSpread"] = True @@ -29,7 +32,7 @@ opts = { "paramsList" : [vTypeParams], - "simSteps" : 500, + "simSteps" : simSteps, "tag" : "Sugiyama", }