iT邦幫忙

2017 iT 邦幫忙鐵人賽
DAY 35
0
自我挑戰組

從科展學寫程式系列 第 31

31 柏拉圖問題 競爭模型對拿取總價值的影響 I(我又回來了~~

  • 分享至 

  • xImage
  •  

雖然晚了許多,不過剛剛寫的程式就是要來現一下啊~

剛考完試的我就直接趕回戰場繼續寫啊~(雖然不知道我會不會又三分鐘熱度)

下面是剛出爐的「競爭」模型,不過這是「朋友」的概念,他會在街道的盡頭走回來,他會告訴你他那一邊的所有狀況(其實就是分數),並且用手機直撥給你。在第21篇的第二點有說明過,只不過這次的對手不會拿取任何一朵花,看你們兩個人一起合作可不可以拿到最大值的花~~
程式在下面:

<?php

if (isset($argv))
    for ($i=1;$i<count($argv);$i++)
    {
        $it = explode("=",$argv[$i]);
        $_GET[$it[0]] = $it[1];
    }

$n="\n";
$brn="<br />\n";

date_default_timezone_set("Asia/Taipei");

//設定變數

//mode=0 無變數
//mode=1 凋謝速率
//mode=2 員工貢獻度
//mode=3 競爭
//mode=4 參考 v(朋友)

if(isset($_GET['debug'])) $debug=$_GET['debug'];
else $debug=1;//顯示步驟

if(isset($_GET['from'])) $from=$_GET['from'];
else $from=0;

if(isset($_GET['to'])) $to=$_GET['to'];
else $to=100;

if(isset($_GET['choice_time'])) $choice_time=$_GET['choice_time'];
else $choice_time=1;

if(isset($_GET['limit'])) $limit=$_GET['limit'];
else $limit=100;

if(isset($_GET['gap'])) $gap=$_GET['gap'];
else $gap=0.1;

if(isset($_GET['loop_num'])) $loop_num=$_GET['loop_num'];
else $loop_num=50;

printf("date,percent,=>,best,fail,fine $n");

 for($k=$from;$k<=$to;$k=$k+$gap){

 $percent=$k;

 $best=0;
 $fail=0;
 $fine=0;

 if($debug>1){
  echo "From: ", $from, $n;
  echo "To: ", $to, $n;
  echo "Limit: ", $limit, $n;
  echo "Percent: ", $percent, $n;
  echo "Border: ", $limit*$percent/100, $n;
 } 
  
 for($j=0;$j<$loop_num;$j++){
  
  $max_num=0;
  $max_percent=$limit*$percent/100;
  $percent_num=0;
  $friend_percent_num=0;
  $choice=0;
  $time=$choice_time;
  $thisnum=range(1,$limit,1);//(小,大,差)
  shuffle($thisnum);//洗牌
  
   for($i=0;$i<$limit;$i++){
    //$num=rand($from, $to);
    
    $num=$thisnum[$i];
    $friend_num=$thisnum[$limit-$i-1];
    if($max_num<=$num) $max_num=$num;
    if($max_num<=$friend_num)$max_num=$friend_num;
    
    if($i<$max_percent) {
     if($percent_num<$num) $percent_num=$num;
     //echo "P:", $percent_num, $n;
    }
    if($friend_percent_num<$friend_num) $friend_percent_num=$friend_num;
    
    if($debug>0) echo $i+1, " ", $num, " ", $friend_num, " ", $percent_num, " ", $friend_percent_num, $n;
    if($time>0 and $num>$percent_num and $num>=$friend_percent_num) {
    $choice=$num;
    $time=$time-1;
    }
   }
  
  if($debug>0){
   echo "Choice: ", $choice, $n;
   echo "Max: ", $max_num, $n;
  }
  
  if($max_num==$choice) $best++;
  if($choice==0) $fail++;
  if($max_num>$choice and $choice>0) $fine++;
  
 }  

 $thedate=date("m-d H:i");
 printf("%s,%.1f => %.7f %.7f %.7f (%.0f/%.0f)\n", $thedate, $percent, $best/$loop_num, $fail/$loop_num, $fine/$loop_num, log($loop_num, 10), log($limit, 10));

} 

?>

之後大概跑了一下,數據長下面的樣子:

date,percent,=>,best,fail,fine 
01-18 23:16,0.0 => 0.1522000 0.0000000 0.8478000 (4/2)
01-18 23:16,1.0 => 0.2913000 0.0089000 0.6998000 (4/2)
01-18 23:16,2.0 => 0.3605000 0.0197000 0.6198000 (4/2)
01-18 23:16,3.0 => 0.4239000 0.0285000 0.5476000 (4/2)
01-18 23:16,4.0 => 0.4663000 0.0402000 0.4935000 (4/2)
01-18 23:16,5.0 => 0.4903000 0.0507000 0.4590000 (4/2)
01-18 23:16,6.0 => 0.5175000 0.0598000 0.4227000 (4/2)
01-18 23:16,7.0 => 0.5476000 0.0683000 0.3841000 (4/2)
01-18 23:16,8.0 => 0.5737000 0.0811000 0.3452000 (4/2)
01-18 23:16,9.0 => 0.5850000 0.0904000 0.3246000 (4/2)
01-18 23:16,10.0 => 0.6037000 0.1026000 0.2937000 (4/2)
01-18 23:16,11.0 => 0.6156000 0.1075000 0.2769000 (4/2)
01-18 23:16,12.0 => 0.6246000 0.1210000 0.2544000 (4/2)
01-18 23:16,13.0 => 0.6329000 0.1337000 0.2334000 (4/2)
01-18 23:16,14.0 => 0.6514000 0.1371000 0.2115000 (4/2)
01-18 23:16,15.0 => 0.6566000 0.1502000 0.1932000 (4/2)
01-18 23:16,16.0 => 0.6580000 0.1602000 0.1818000 (4/2)
01-18 23:16,17.0 => 0.6574000 0.1659000 0.1767000 (4/2)
01-18 23:16,18.0 => 0.6597000 0.1828000 0.1575000 (4/2)
01-18 23:16,19.0 => 0.6792000 0.1831000 0.1377000 (4/2)
01-18 23:16,20.0 => 0.6656000 0.2082000 0.1262000 (4/2)
01-18 23:16,21.0 => 0.6664000 0.2110000 0.1226000 (4/2)
01-18 23:16,22.0 => 0.6620000 0.2283000 0.1097000 (4/2)
01-18 23:16,23.0 => 0.6737000 0.2285000 0.0978000 (4/2)
01-18 23:16,24.0 => 0.6689000 0.2407000 0.0904000 (4/2)
01-18 23:16,25.0 => 0.6648000 0.2555000 0.0797000 (4/2)
01-18 23:16,26.0 => 0.6602000 0.2635000 0.0763000 (4/2)
01-18 23:16,27.0 => 0.6616000 0.2715000 0.0669000 (4/2)
01-18 23:16,28.0 => 0.6625000 0.2793000 0.0582000 (4/2)
01-18 23:16,29.0 => 0.6529000 0.2900000 0.0571000 (4/2)
...

發現總共有100家的時候,從第23家或第24家後再選擇拿取可以拿到最大值的機率會到67%左右~大發現~~

我瘋了。

不過值得一提的是,我這次寫程式的速度終於有變快一些了,修bug的時間也變短了。只不過還是會常常忘記";"這個東西...


上一篇
30 柏拉圖最後的抉擇 The End
下一篇
32 柏拉圖問題 競爭模型對拿取總價值的影響 II
系列文
從科展學寫程式43
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言