Step5: 加入道路自动修复机制(Optional)

现在我们来为仿真模型加入道路修复机制:

  • 在全局定义中定义一个修复间隔时间,设定其值为2,即每隔两小时进行一次修复。

  • 当达到修复时间是,修复路网中磨损度最高的道路,将其磨损度设为1。

global {
    ...
    //设定修复间隔时间
    int repair_time <- 2 ;
    //创建道路修复函数,每2小时运行一次
    reflex repair_road when: every(repair_time #hour) {
    //取得损耗度最大的道路
    road the_road_to_repair <- road with_max_of (each.destruction_coeff) ;
    //将此道路磨损度设为1
    ask the_road_to_repair {
        destruction_coeff <- 1.0 ;
    }
    }
}

container with_max_of expression: 返回container中拥有最大expression值的元素,注意与max_of的区别。

最后在实验设置中,将修复间隔时间设定为可调整参数。

parameter "Number of steps between two road repairs" var: repair_time category: "Road" ;

本节完整代码如下:

Last updated

Was this helpful?