Advertisement
cecepsuwanda

iterasi.php

Jun 11th, 2018
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.15 KB | None | 0 0
  1. <?php
  2. ini_set(max_execution_time, 172800);
  3. include 'koneksi.php';
  4.  
  5. function count_decimals($x){
  6.     return  strlen(substr(strrchr($x+"", "."), 1));
  7. }
  8.  
  9. function random($min, $max){
  10.     $decimals = max(count_decimals($min), count_decimals($max));
  11.     $factor = pow(10, $decimals);
  12.     return rand($min*$factor, $max*$factor) / $factor;
  13. }
  14.  
  15.  
  16. mysql_query("DELETE FROM bobot");
  17.  
  18. $sql = mysql_query("SELECT occupancy,
  19.                           MAX(temperature) AS max_temperature,
  20.                           MAX(humidity) AS max_humidity,
  21.                           MAX(light) AS max_light,
  22.                           MAX(co2) AS max_co2,
  23.                           MAX(humidity_ratio) AS max_humidity_ratio,
  24.                            MIN(temperature) AS min_temperature,
  25.                           MIN(humidity) AS min_humidity,
  26.                           MIN(light) AS min_light,
  27.                           MIN(co2) AS min_co2,
  28.                           MIN(humidity_ratio) AS min_humidity_ratio
  29.                    FROM training where occupancy in (0,1) group by occupancy");
  30.  
  31.  $array = array();  
  32.  while($data = mysql_fetch_array($sql))
  33.  {
  34.     $array[$data['occupancy']] = array(
  35.                                         'max_temperature'=>$data['max_temperature'],
  36.                                         'max_humidity'=>$data['max_humidity'],
  37.                                         'max_light'=>$data['max_light'],
  38.                                         'max_co2'=>$data['max_co2'],
  39.                                         'max_humidity_ratio'=>$data['max_humidity_ratio'],
  40.                                         'min_temperature'=>$data['min_temperature'],
  41.                                         'min_humidity'=>$data['min_humidity'],
  42.                                         'min_light'=>$data['min_light'],
  43.                                         'min_co2'=>$data['min_co2'],
  44.                                         'min_humidity_ratio'=>$data['min_humidity_ratio']                                    
  45.                                        );
  46.  }
  47.  
  48. foreach($array as $key=>$value)
  49. {
  50.   $data_rand[$key]['temperatur_rand'] = random($value['min_temperature'],$value['max_temperature']);
  51.   $data_rand[$key]['humidity_rand'] = random($value['min_humidity'],$value['max_humidity']);
  52.   $data_rand[$key]['light_rand'] = random($value['min_light'],$value['max_light']);
  53.   $data_rand[$key]['co2_rand'] = random($value['min_co2'],$value['max_co2']);
  54.   $data_rand[$key]['humidity_ratio_rand'] = random($value['min_humidity_ratio'],$value['max_humidity_ratio']);
  55. }
  56.  
  57. $sql = mysql_query("SELECT count(*) as jml FROM training");
  58. $tmp = mysql_fetch_array($sql);
  59. $jum_sum = $tmp['jml'];
  60.  
  61. $sql = mysql_query("SELECT * FROM training ORDER BY id asc limit 1");
  62. $tmp  = mysql_fetch_array($sql);
  63. $id_training_awal = $tmp['id'];
  64.  
  65. foreach($data_rand as $key=>$value)
  66. {
  67.   mysql_query("INSERT INTO bobot VALUES ('',
  68.                                         '$value[temperatur_rand]',
  69.                                          '$value[humidity_rand]',
  70.                                          '$value[light_rand]',
  71.                                          '$value[co2_rand]',
  72.                                          '$value[humidity_ratio_rand]',
  73.                                          '$id_training_awal',
  74.                                          '".($key==1 ? 1:2)."',
  75.                                          '1')");
  76. }
  77.  
  78. unset($array);
  79. unset($data_rand);
  80.  
  81.  
  82. $learning_rate = 0.5;
  83.  
  84. for($iterasi = 1; $iterasi<=3;$iterasi++){
  85.  
  86.  
  87.     if($iterasi==1){
  88.  
  89.         $learning_rate = 0.5;
  90.        
  91.     } else {
  92.        
  93.         $learning_rate = $learning_rate * 0.6;
  94.     }
  95.  
  96.     $sql_vektor_1 = mysql_query("SELECT * FROM training where status='1' ORDER BY id ASC limit 1") or die(mysql_error());
  97.     $array_vektor_1 = mysql_fetch_array($sql_vektor_1);
  98.    
  99.     $insert_vektor = mysql_query("INSERT INTO training VALUES ('',
  100.                                                                '',
  101.                                                                '$array_vektor1[temperature]',
  102.                                                                '$array_vektor1[humidity]',
  103.                                                                '$array_vektor1[light]',
  104.                                                                '$array_vektor1[co2]',
  105.                                                                '$array_vektor1[humidity_ratio]',
  106.                                                                '$array_vektor1[occupancy]',
  107.                                                                '1',
  108.                                                                '$iterasi')")or die(mysql_error());
  109.    
  110.     $sub_sql = "select distinct temperature,
  111.                      humidity,
  112.                      light,
  113.                      co2,
  114.                      humidity_ratio
  115.                 from training
  116.                 where status='1' and (
  117.                       temperature<>'$array_vektor1[temperature]' or
  118.                       humidity<>'$array_vektor1[humidity]' or
  119.                       light<>'$array_vektor1[light]' or
  120.                       co2<>'$array_vektor1[co2]' or
  121.                       humidity_ratio<>'$array_vektor1[humidity_ratio]')";
  122.    
  123.     $insert_vektor = mysql_query("INSERT INTO training (select '',
  124.                                                                '',
  125.                                                                temperature,
  126.                                                                humidity,
  127.                                                                light,
  128.                                                                co2,
  129.                                                                humidity_ratio,
  130.                                                                occupancy,
  131.                                                                0,
  132.                                                                $iterasi
  133.                                                         from ($sub_sql) a )")or die(mysql_error());              
  134.    
  135.    
  136.     $sql_vektor_12 = mysql_query("SELECT * FROM training ORDER BY id DESC") or die(mysql_error());
  137.     $array_vektor_12 = mysql_fetch_array($sql_vektor_12);
  138.  
  139.     for($i=1;$i<=$jum_sum;$i++){
  140.         $sql_bobot = mysql_query("(SELECT * FROM bobot  ORDER BY id DESC, id_bobot ASC limit 1) union all
  141.                                    (SELECT * FROM bobot  ORDER BY id DESC, id_bobot DESC limit 1)") or die(mysql_error());
  142.        
  143.         $array_bobot1 = mysql_fetch_array($sql_bobot);
  144.         $bobot_vektor11 = $array_bobot1['bobot_vektor1'];
  145.         $bobot_vektor12 = $array_bobot1['bobot_vektor2'];
  146.         $bobot_vektor13 = $array_bobot1['bobot_vektor3'];
  147.         $bobot_vektor14 = $array_bobot1['bobot_vektor4'];
  148.         $bobot_vektor15 = $array_bobot1['bobot_vektor5'];
  149.        
  150.         $array_bobot2 = mysql_fetch_array($sql_bobot);
  151.         $bobot_vektor21 = $array_bobot2['bobot_vektor1'];
  152.         $bobot_vektor22 = $array_bobot2['bobot_vektor2'];
  153.         $bobot_vektor23 = $array_bobot2['bobot_vektor3'];
  154.         $bobot_vektor24 = $array_bobot2['bobot_vektor4'];
  155.         $bobot_vektor25 = $array_bobot2['bobot_vektor5'];
  156.         echo $bobot_vektor13 . "<br>";
  157.  
  158.         $sql_vektor = mysql_query("SELECT * FROM training where status='1' and (id+1) not in (select id from bobot) ORDER BY id DESC") or die(mysql_error());
  159.         while($array_vektor = mysql_fetch_array($sql_vektor)){
  160.             $id_vektor = $array_vektor['id'];
  161.            
  162.             $d1 =pow($bobot_vektor11-$array_vektor['temperature'],2)+
  163.                  pow($bobot_vektor12-$array_vektor['humidity'],2)+
  164.                  pow($bobot_vektor13-$array_vektor['light'],2)+
  165.                  pow($bobot_vektor14-$array_vektor['co2'],2)+
  166.                  pow($bobot_vektor15-$array_vektor['humidity_ratio'],2);
  167.  
  168.             $d2 =pow($bobot_vektor21-$array_vektor['temperature'],2)+
  169.                  pow($bobot_vektor22-$array_vektor['humidity'],2)+
  170.                  pow($bobot_vektor23-$array_vektor['light'],2)+
  171.                  pow($bobot_vektor24-$array_vektor['co2'],2)+
  172.                  pow($bobot_vektor25-$array_vektor['humidity_ratio'],2);           
  173.             $id_vektor_plus = $id_vektor + 1;
  174.            
  175.             if($d1 < $d2){         
  176.                 $sql_cek1 = mysql_query("SELECT * FROM bobot WHERE id='$id_vektor_plus' and iterasi='$iterasi'")or die(mysql_error());
  177.                 if(mysql_num_rows($sql_cek1)){
  178.                     echo '';
  179.                 }  else{
  180.                    
  181.                     $w11 = $bobot_vektor11 + $learning_rate * ($array_vektor['temperature'] - $bobot_vektor11);
  182.                     $w12 = $bobot_vektor12 + $learning_rate * ($array_vektor['humidity'] - $bobot_vektor12);
  183.                     $w13 = $bobot_vektor13 + $learning_rate * ($array_vektor['light'] - $bobot_vektor13);
  184.                     $w14 = $bobot_vektor14 + $learning_rate * ($array_vektor['co2'] - $bobot_vektor14);
  185.                     $w15 = $bobot_vektor15 + $learning_rate * ($array_vektor['humidity_ratio'] - $bobot_vektor15);
  186.                
  187.                     //  echo $array_vektor['id'] . "=" . $bobot_vektor13 . "+" . $learning_rate . "*" . $array_vektor['light']  . "-" . $bobot_vektor13 . "=" . $w13 . "<br>";
  188.                
  189.                     $w21 = $bobot_vektor21;
  190.                     $w22 = $bobot_vektor22;
  191.                     $w23 = $bobot_vektor23;
  192.                     $w24 = $bobot_vektor24;
  193.                     $w25 = $bobot_vektor25;
  194.                    
  195.                     $sql_cek_bobot1 = mysql_query("SELECT * FROM bobot WHERE id='$id_vektor_plus' and kategori=1")or die(mysql_error());
  196.                     if(mysql_num_rows($sql_cek_bobot1)){
  197.                        
  198.                     } else {
  199.                         $update_bobot1= mysql_query("INSERT INTO bobot VALUES ('','$w11','$w12','$w13','$w14','$w15', '$id_vektor_plus',1,'$iterasi')")or die(mysql_error());
  200.                        
  201.                     }
  202.                    
  203.                     $sql_cek_bobot2 = mysql_query("SELECT * FROM bobot WHERE id='$id_vektor_plus' and kategori=2")or die(mysql_error());
  204.                     if(mysql_num_rows($sql_cek_bobot2)){
  205.                        
  206.                     } else {
  207.                         $update_bobot2= mysql_query("INSERT INTO bobot VALUES ('','$w21','$w22','$w23','$w24','$w25','$id_vektor_plus',2,'$iterasi')")or die(mysql_error());
  208.                        
  209.                     }
  210.                    
  211.                 }
  212.                 $update_vektor = mysql_query("UPDATE training SET status='1' WHERE id = '$id_vektor_plus'") or die(mysql_error());
  213.                
  214.             } else if ($d1 > $d2){
  215.                
  216.                 $sql_cek1 = mysql_query("SELECT * FROM bobot WHERE id = '$id_vektor_plus'");
  217.                 if(mysql_num_rows($sql_cek1)){
  218.                     echo '';
  219.                 }  else{
  220.                    
  221.                    $w21 = $bobot_vektor21 + $learning_rate * ($array_vektor['temperature'] - $bobot_vektor21);
  222.                    $w22 = $bobot_vektor22 + $learning_rate * ($array_vektor['humidity'] - $bobot_vektor22);
  223.                    $w23 = $bobot_vektor23 + $learning_rate * ($array_vektor['light'] - $bobot_vektor23);
  224.                    $w24 = $bobot_vektor24 + $learning_rate * ($array_vektor['co2'] - $bobot_vektor24);
  225.                    $w25 = $bobot_vektor25 + $learning_rate * ($array_vektor['humidity_ratio'] - $bobot_vektor25);
  226.                
  227.                    //echo $bobot_vektor11 . "+" . $learning_rate . "*" . $array_vektor['item']  . "-" . $bobot_vektor11 . "<br>";
  228.                
  229.                    $w11 = $bobot_vektor11;
  230.                    $w12 = $bobot_vektor11;
  231.                    $w13 = $bobot_vektor13;
  232.                    $w14 = $bobot_vektor14;
  233.                    $w15 = $bobot_vektor15;
  234.                    
  235.                     $update_bobot1= mysql_query("INSERT INTO bobot VALUES ('','$w11','$w12','$w13','$w14', '$w15', '$id_vektor_plus',1,'$iterasi')")or die(mysql_error());
  236.                    
  237.                     $update_bobot2= mysql_query("INSERT INTO bobot VALUES ('','$w21','$w22','$w23','$w24','$w25','$id_vektor_plus',2,'$iterasi')")or die(mysql_error());
  238.                 }
  239.                 $update_vektor = mysql_query("UPDATE training SET status='1' WHERE id = '$id_vektor_plus'") or die(mysql_error());
  240.                
  241.                
  242.             }
  243.         }
  244.  
  245.     }
  246.  
  247. }
  248. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement