Lab6

请仔细对照实验手册,针对三个问题中的每一项任务,在下面各节中记录你的实验过程、阐述你的设计思路和问题求解思路,可辅之以示意图或关键源代码加以说明(但千万不要把你的源代码全部粘贴过来!)。
3.1 ADT设计方案
设计了哪些ADT、各自的作用、属性、方法;
给出每个ADT的specification;
(可选)以类图形式给出多个类之间的关系。

  1. Monkey 猴子
    /**

    • Representation invariant:
    • name = Monkey + time created + number
    • direction = L->R or R->L
    • 1 =< speed <= MaximumVelocity
    • the type of speed is integer
    • Abstract Function:
    • represent the Monkey to climb over the river
    • Representation exposure:
    • the position parameter is private and can only be accessed by modifier
    • all other parameters are final private immutable variants
    • client only have the access the read them rather than modify them
    • Thread safety argument:
    • the position parameter will only be used be one thread and invisible to other threads
    • all other parameters in the Monkey are immutable which can be thread safe
      */
  2. MonkeyRun 猴子线程

  3. MonkeyGenerator 猴子生成器

  4. Ladder 梯子
    /**

    • Representation invariant:
    • list is the steps of the ladder
    • direction = 0, 1, 2 indicating not chosen, L->R and R->L
    • number is the identity of the ladder
    • Abstract Function:
    • represent the ladder to be climbed by monkeys
    • Representation exposure:
    • the list is private and only be accessed by method, it will not be modified directly
    • the direction and the number are both private integer which has no worry about representation exposure
    • Thread safety argument:
    • it is not thread safe with the contribution of the ladder class only
    • all method needs to be synchronized in the MonkeyRun class
      */
  5. Strategy 策略模式

  6. Logger log

  7. Fairness 公平性计算

  8. Initialization 初始化

  9. Mainprocess 主进程
    3.2 Monkey线程的run()的执行流程图
    这里无需考虑具体采用的梯子选择策略。

  10. 基本思路:
    while( true ){
    if (有梯子可以爬) {
    爬梯子;
    sleep(1000);
    break;
    }
    else{
    sleep(1000);
    }
    }
    while(true){
    If (没到对岸) {
    往前动一次;
    }
    If ( 到对岸了) {
    break;
    }
    sleep(1000);
    }
    3.3 至少两种“梯子选择”策略的设计与实现方案
    3.3.1 策略1
    优先寻找空梯子,没有的话在同方向梯子里随机选一个。先搜索空梯子,再进行顺序搜索。
    3.3.2 策略2
    优先选择空梯子,没有的话则选择前进速度最快的。具体方法是先搜寻空梯子,再查找速度最快的。
    3.3.3 策略3(可选)
    优先寻找空梯子,没有的话就停止,等到有空梯子为止。具体方法是搜索空梯子,没有就等待直到有位置。
    3.4 “猴子生成器”MonkeyGenerator
    用全局变量记录生成的猴子数量,用静态方法产生和返回新的猴子,猴子的属性random产生。
    3.5 如何确保threadsafe?
    每一个线程读取梯子状态,选择梯子,在梯子上向前移动的时候都需要锁住ladder。
    3.6 系统吞吐率和公平性的度量方案
    用Fairness类来计算公平性,而吞吐率直接用总猴子数除以总时间来表示。
    3.7 输出方案设计
    Logger记录每一步操作:包括参数设置,策略选择,猴子产生,猴子移动,猴子到达等。
    3.8 猴子过河模拟器v1
    3.8.1 参数如何初始化
    用Initialization类初始化读入的参数,并调用所有需要初始化的类提供的方法。
    3.8.2 使用Strategy模式为每只猴子选择决策策略
    在Strategy中加入随机函数设置相应策略即可。
    3.9 猴子过河模拟器v2
    在不同参数设置和不同“梯子选择”模式下的“吞吐率”和“公平性”实验结果及其对比分析。
    3.9.1 对比分析:固定其他参数,选择不同的决策策略
    固定其他参数n = 3, h = 20, t = 3, N = 50, k = 3, MV = 5
    策略1:
    The throughput rate is 1.8518518518518519
    The Fairness is -0.09306122448979592
    策略2:
    The throughput rate is 1.6666666666666667
    The Fairness is 0.08163265306122448
    策略3:
    The throughput rate is 0.37037037037037035
    The Fairness is 0.09551020408163265
    3.9.2 对比分析:变化某个参数,固定其他参数
    固定其他参数h = 20, t = 3, N = 50, k = 3, MV = 5,变化n = 3,4,5
    策略1 :
    n = 3 :
    The throughput rate is 1.8518518518518519
    The Fairness is -0.09306122448979592
    n = 4 :
    The throughput rate is 2.0833333333333335
    The Fairness is -0.08244897959183674
    n = 5 :
    The throughput rate is 1.4285714285714286
    The Fairness is 0.15428571428571428
    策略2 :
    n = 3 :
    The throughput rate is 1.6666666666666667
    The Fairness is 0.08163265306122448
    n = 4 :
    The throughput rate is 1.4705882352941178
    The Fairness is 0.15836734693877552
    n = 5 :
    The throughput rate is 1.4285714285714286
    The Fairness is 0.12081632653061225
    策略3 :
    n = 3 :
    The throughput rate is 0.37037037037037035
    The Fairness is 0.09551020408163265
    n = 4 :
    The throughput rate is 0.49019607843137253
    The Fairness is 0.22857142857142856
    n = 5 :
    The throughput rate is 0.6756756756756757
    The Fairness is 0.24897959183673468
    3.9.3 分析:吞吐率是否与各参数/决策策略有相关性?
    吞吐率与决策方式有相关性,但是与n的相关性不是正相关。
    3.9.4 压力测试结果与分析
    n = 5, h = 20, t = 1, N = 100, k = 3, MV = 5
    The throughput rate is 1.694915254237288
    The Fairness is 0.43555555555555553
    少梯子,多猴子的情况运行较为合理。
    3.10 猴子过河模拟器v3
    针对教师提供的三个文本文件,分别进行多次模拟,记录模拟结果。
    使用Density Strategy:
    吞吐率 公平性
    Competiton_1.txt
    第1次模拟 2.02 0.45
    第2次模拟 2.03 0.49
    第3次模拟 1.98 0.36
    第4次模拟 2.01 0.45
    第5次模拟 1.96 0.41
    第6次模拟 2.00 0.48
    第7次模拟 1.98 0.40
    第8次模拟 2.00 0.47
    第9次模拟 2.02 0.47
    第10次模拟 2.03 0.41
    平均值 2.00 0.45
    Competiton_2.txt
    第1次模拟 3.95 0.45
    第2次模拟 3.80 0.37
    第3次模拟 3.95 0.41
    第4次模拟 3.93 2.44
    第5次模拟 4.11 0.45
    第6次模拟 3.85 0.47
    第7次模拟 3.92 0.45
    第8次模拟 3.80 0.39
    第9次模拟 4.19 0.45
    第10次模拟 3.90 0.45
    平均值 3.95 0.40
    Competiton_3.txt
    第1次模拟 1.00 0.45
    第2次模拟 1.01 0.49
    第3次模拟 0.98 0.40
    第4次模拟 1.01 0.51
    第5次模拟 0.98 0.36
    第6次模拟 0.99 0.39
    第7次模拟 1.01 0.52
    第8次模拟 1.03 0.52
    第9次模拟 1.01 0.51
    第10次模拟 1.03 0.40
    平均值 1.01 0.47

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值