Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- ini_set(max_execution_time, 172800);
- include 'koneksi.php';
- function count_decimals($x){
- return strlen(substr(strrchr($x+"", "."), 1));
- }
- function random($min, $max){
- $decimals = max(count_decimals($min), count_decimals($max));
- $factor = pow(10, $decimals);
- return rand($min*$factor, $max*$factor) / $factor;
- }
- mysql_query("DELETE FROM bobot");
- $sql = mysql_query("SELECT occupancy,
- MAX(temperature) AS max_temperature,
- MAX(humidity) AS max_humidity,
- MAX(light) AS max_light,
- MAX(co2) AS max_co2,
- MAX(humidity_ratio) AS max_humidity_ratio,
- MIN(temperature) AS min_temperature,
- MIN(humidity) AS min_humidity,
- MIN(light) AS min_light,
- MIN(co2) AS min_co2,
- MIN(humidity_ratio) AS min_humidity_ratio
- FROM training where occupancy in (0,1) group by occupancy");
- $array = array();
- while($data = mysql_fetch_array($sql))
- {
- $array[$data['occupancy']] = array(
- 'max_temperature'=>$data['max_temperature'],
- 'max_humidity'=>$data['max_humidity'],
- 'max_light'=>$data['max_light'],
- 'max_co2'=>$data['max_co2'],
- 'max_humidity_ratio'=>$data['max_humidity_ratio'],
- 'min_temperature'=>$data['min_temperature'],
- 'min_humidity'=>$data['min_humidity'],
- 'min_light'=>$data['min_light'],
- 'min_co2'=>$data['min_co2'],
- 'min_humidity_ratio'=>$data['min_humidity_ratio']
- );
- }
- foreach($array as $key=>$value)
- {
- $data_rand[$key]['temperatur_rand'] = random($value['min_temperature'],$value['max_temperature']);
- $data_rand[$key]['humidity_rand'] = random($value['min_humidity'],$value['max_humidity']);
- $data_rand[$key]['light_rand'] = random($value['min_light'],$value['max_light']);
- $data_rand[$key]['co2_rand'] = random($value['min_co2'],$value['max_co2']);
- $data_rand[$key]['humidity_ratio_rand'] = random($value['min_humidity_ratio'],$value['max_humidity_ratio']);
- }
- $sql = mysql_query("SELECT count(*) as jml FROM training");
- $tmp = mysql_fetch_array($sql);
- $jum_sum = $tmp['jml'];
- $sql = mysql_query("SELECT * FROM training ORDER BY id asc limit 1");
- $tmp = mysql_fetch_array($sql);
- $id_training_awal = $tmp['id'];
- foreach($data_rand as $key=>$value)
- {
- mysql_query("INSERT INTO bobot VALUES ('',
- '$value[temperatur_rand]',
- '$value[humidity_rand]',
- '$value[light_rand]',
- '$value[co2_rand]',
- '$value[humidity_ratio_rand]',
- '$id_training_awal',
- '".($key==1 ? 1:2)."',
- '1')");
- }
- unset($array);
- unset($data_rand);
- $learning_rate = 0.5;
- for($iterasi = 1; $iterasi<=3;$iterasi++){
- if($iterasi==1){
- $learning_rate = 0.5;
- } else {
- $learning_rate = $learning_rate * 0.6;
- }
- $sql_vektor_1 = mysql_query("SELECT * FROM training where status='1' ORDER BY id ASC limit 1") or die(mysql_error());
- $array_vektor_1 = mysql_fetch_array($sql_vektor_1);
- $insert_vektor = mysql_query("INSERT INTO training VALUES ('',
- '',
- '$array_vektor1[temperature]',
- '$array_vektor1[humidity]',
- '$array_vektor1[light]',
- '$array_vektor1[co2]',
- '$array_vektor1[humidity_ratio]',
- '$array_vektor1[occupancy]',
- '1',
- '$iterasi')")or die(mysql_error());
- $sub_sql = "select distinct temperature,
- humidity,
- light,
- co2,
- humidity_ratio
- from training
- where status='1' and (
- temperature<>'$array_vektor1[temperature]' or
- humidity<>'$array_vektor1[humidity]' or
- light<>'$array_vektor1[light]' or
- co2<>'$array_vektor1[co2]' or
- humidity_ratio<>'$array_vektor1[humidity_ratio]')";
- $insert_vektor = mysql_query("INSERT INTO training (select '',
- '',
- temperature,
- humidity,
- light,
- co2,
- humidity_ratio,
- occupancy,
- 0,
- $iterasi
- from ($sub_sql) a )")or die(mysql_error());
- $sql_vektor_12 = mysql_query("SELECT * FROM training ORDER BY id DESC") or die(mysql_error());
- $array_vektor_12 = mysql_fetch_array($sql_vektor_12);
- for($i=1;$i<=$jum_sum;$i++){
- $sql_bobot = mysql_query("(SELECT * FROM bobot ORDER BY id DESC, id_bobot ASC limit 1) union all
- (SELECT * FROM bobot ORDER BY id DESC, id_bobot DESC limit 1)") or die(mysql_error());
- $array_bobot1 = mysql_fetch_array($sql_bobot);
- $bobot_vektor11 = $array_bobot1['bobot_vektor1'];
- $bobot_vektor12 = $array_bobot1['bobot_vektor2'];
- $bobot_vektor13 = $array_bobot1['bobot_vektor3'];
- $bobot_vektor14 = $array_bobot1['bobot_vektor4'];
- $bobot_vektor15 = $array_bobot1['bobot_vektor5'];
- $array_bobot2 = mysql_fetch_array($sql_bobot);
- $bobot_vektor21 = $array_bobot2['bobot_vektor1'];
- $bobot_vektor22 = $array_bobot2['bobot_vektor2'];
- $bobot_vektor23 = $array_bobot2['bobot_vektor3'];
- $bobot_vektor24 = $array_bobot2['bobot_vektor4'];
- $bobot_vektor25 = $array_bobot2['bobot_vektor5'];
- echo $bobot_vektor13 . "<br>";
- $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());
- while($array_vektor = mysql_fetch_array($sql_vektor)){
- $id_vektor = $array_vektor['id'];
- $d1 =pow($bobot_vektor11-$array_vektor['temperature'],2)+
- pow($bobot_vektor12-$array_vektor['humidity'],2)+
- pow($bobot_vektor13-$array_vektor['light'],2)+
- pow($bobot_vektor14-$array_vektor['co2'],2)+
- pow($bobot_vektor15-$array_vektor['humidity_ratio'],2);
- $d2 =pow($bobot_vektor21-$array_vektor['temperature'],2)+
- pow($bobot_vektor22-$array_vektor['humidity'],2)+
- pow($bobot_vektor23-$array_vektor['light'],2)+
- pow($bobot_vektor24-$array_vektor['co2'],2)+
- pow($bobot_vektor25-$array_vektor['humidity_ratio'],2);
- $id_vektor_plus = $id_vektor + 1;
- if($d1 < $d2){
- $sql_cek1 = mysql_query("SELECT * FROM bobot WHERE id='$id_vektor_plus' and iterasi='$iterasi'")or die(mysql_error());
- if(mysql_num_rows($sql_cek1)){
- echo '';
- } else{
- $w11 = $bobot_vektor11 + $learning_rate * ($array_vektor['temperature'] - $bobot_vektor11);
- $w12 = $bobot_vektor12 + $learning_rate * ($array_vektor['humidity'] - $bobot_vektor12);
- $w13 = $bobot_vektor13 + $learning_rate * ($array_vektor['light'] - $bobot_vektor13);
- $w14 = $bobot_vektor14 + $learning_rate * ($array_vektor['co2'] - $bobot_vektor14);
- $w15 = $bobot_vektor15 + $learning_rate * ($array_vektor['humidity_ratio'] - $bobot_vektor15);
- // echo $array_vektor['id'] . "=" . $bobot_vektor13 . "+" . $learning_rate . "*" . $array_vektor['light'] . "-" . $bobot_vektor13 . "=" . $w13 . "<br>";
- $w21 = $bobot_vektor21;
- $w22 = $bobot_vektor22;
- $w23 = $bobot_vektor23;
- $w24 = $bobot_vektor24;
- $w25 = $bobot_vektor25;
- $sql_cek_bobot1 = mysql_query("SELECT * FROM bobot WHERE id='$id_vektor_plus' and kategori=1")or die(mysql_error());
- if(mysql_num_rows($sql_cek_bobot1)){
- } else {
- $update_bobot1= mysql_query("INSERT INTO bobot VALUES ('','$w11','$w12','$w13','$w14','$w15', '$id_vektor_plus',1,'$iterasi')")or die(mysql_error());
- }
- $sql_cek_bobot2 = mysql_query("SELECT * FROM bobot WHERE id='$id_vektor_plus' and kategori=2")or die(mysql_error());
- if(mysql_num_rows($sql_cek_bobot2)){
- } else {
- $update_bobot2= mysql_query("INSERT INTO bobot VALUES ('','$w21','$w22','$w23','$w24','$w25','$id_vektor_plus',2,'$iterasi')")or die(mysql_error());
- }
- }
- $update_vektor = mysql_query("UPDATE training SET status='1' WHERE id = '$id_vektor_plus'") or die(mysql_error());
- } else if ($d1 > $d2){
- $sql_cek1 = mysql_query("SELECT * FROM bobot WHERE id = '$id_vektor_plus'");
- if(mysql_num_rows($sql_cek1)){
- echo '';
- } else{
- $w21 = $bobot_vektor21 + $learning_rate * ($array_vektor['temperature'] - $bobot_vektor21);
- $w22 = $bobot_vektor22 + $learning_rate * ($array_vektor['humidity'] - $bobot_vektor22);
- $w23 = $bobot_vektor23 + $learning_rate * ($array_vektor['light'] - $bobot_vektor23);
- $w24 = $bobot_vektor24 + $learning_rate * ($array_vektor['co2'] - $bobot_vektor24);
- $w25 = $bobot_vektor25 + $learning_rate * ($array_vektor['humidity_ratio'] - $bobot_vektor25);
- //echo $bobot_vektor11 . "+" . $learning_rate . "*" . $array_vektor['item'] . "-" . $bobot_vektor11 . "<br>";
- $w11 = $bobot_vektor11;
- $w12 = $bobot_vektor11;
- $w13 = $bobot_vektor13;
- $w14 = $bobot_vektor14;
- $w15 = $bobot_vektor15;
- $update_bobot1= mysql_query("INSERT INTO bobot VALUES ('','$w11','$w12','$w13','$w14', '$w15', '$id_vektor_plus',1,'$iterasi')")or die(mysql_error());
- $update_bobot2= mysql_query("INSERT INTO bobot VALUES ('','$w21','$w22','$w23','$w24','$w25','$id_vektor_plus',2,'$iterasi')")or die(mysql_error());
- }
- $update_vektor = mysql_query("UPDATE training SET status='1' WHERE id = '$id_vektor_plus'") or die(mysql_error());
- }
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement