Step3: 动态的道路损耗度模拟

路网权值

接下来我们来为道路族添加损耗度属性,来模拟道路在使用中的磨损情况。在此之前我们先了解一下道路权值,在未设定道路权值之前,代理人会寻找最短路径从A点出发前往B点,过程中的道路对其路径的选择并无影响,而在设定道路权值之后,代理人会优先选择权值较高的道路。 简单来说,道路权值越高,会有越多人选择经过这条路。

现在我们为道路添加几个属性:

  • 损耗度(destruction_coeff):我们设定损耗度最小为1,最大为2,初始值为1-2之间的随机值。

  • 道路颜色(color):取决于道路损耗度,损耗度越高,颜色越红,,损耗度越低,颜色越绿。

species road  {
    //添加属性损耗度,初始值为1-2之间的随机值,最大值为2
    float destruction_coeff <- rnd(1.0,2.0) max: 2.0;
    //设定一个中间变量colorValue,destruction_coeff越大,colorValue越接近255
    int colorValue <- int(255*(destruction_coeff - 1)) update: int(255*(destruction_coeff - 1));
    //设定道路颜色,colorValue越大,颜色越接近红色,反之,越接近绿色
    rgb color <- rgb(min([255, colorValue]),max ([0, 255 - colorValue]),0)  update: rgb(min([255, colorValue]),max ([0, 255 - colorValue]),0) ;
    ...
}

然后在全局定义中为路网添加权值:

  • 道路权值(weight):在道路系统中,道路损耗度越大,说明越多人经过了这条道路,我们将道路权值设定为 “道路长度x损耗度”。

    init {
        ...
        create road from: shape_file_roads ;
        //创建一个字典,字典内为每条道路::对应道路的损耗度x长度
        map<road,float> weights_map <- road as_map (each:: (each.destruction_coeff * each.shape.perimeter));
        //通过创建的字典为路网权重赋值
        the_graph <- as_edge_graph(road) with_weights weights_map;
        ...
    }

路网权值的动态更新

在初始化路网权值之后,我们希望在模拟过程中,路网权值也能得到动态更新,即实现“走的人越多——路网权值越高——越多人选择走这条路”这样一个动态过程。

首先,我们定义当一个人走过一条路时,这条路的道路磨损度增加0.02,这个变量用destroy表示。

接下在人群族的移动行为中,为其每次移动经过的道路更新磨损度。

TIPS:这里更新道路磨损度是使用的destruction_coeff + (destroy * dist / shape.perimeter),而不是直接使用destruction_coeff + destroy * dist,这是因为GAMA是根据设定的step更新的,因此一步更新的路径长度dist=step x speed,并不会等于道路长度(shape.perimeter),所以这里取路径长度(dist)和道路长度(shape.perimeter)的比值乘以每次损耗量作为一步之内更新的道路损耗度。这样当路径长度之和等于道路长度时,即代理人走过这条道路之后,道路损耗量更新量为destroy

实现道路损耗度的动态更新之后,我们再实现路网权值的动态更新。

本节完整代码如下:

Last updated

Was this helpful?