implementasi Algoritma find-s

Algoritma find-s merupakan algoritma pertama yang saya pelajari. algoritma ini cukup mudah tapi membutuhkan komputasi yang banyak.
berikut adalah sources code algoritma find s

Table SQL

-- phpMyAdmin SQL Dump
-- version 4.5.1
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: 19 Apr 2016 pada 13.19
-- Versi Server: 10.1.10-MariaDB
-- PHP Version: 5.6.19

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `data`
--

-- --------------------------------------------------------

--
-- Struktur dari tabel `data_training`
--

CREATE TABLE `data_training` (
  `id_data` int(11) NOT NULL,
  `Sky` text NOT NULL,
  `AirTemp` text NOT NULL,
  `Humidity` text NOT NULL,
  `Wind` text NOT NULL,
  `Water` text NOT NULL,
  `Forecast` text NOT NULL,
  `EnjoySport` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data untuk tabel `data_training`
--

INSERT INTO `data_training` (`id_data`, `Sky`, `AirTemp`, `Humidity`, `Wind`, `Water`, `Forecast`, `EnjoySport`) VALUES
(1, 'sunny', 'warm', 'normal', 'strong', 'warm', 'same', 'yes'),
(2, 'sunny', 'warm', 'high', 'strong', 'warm', 'same', 'yes'),
(3, 'rainy', 'cold', 'high', 'strong', 'warm', 'change', 'no'),
(4, 'sunny', 'warm', 'high', 'strong', 'cool', 'change', 'yes');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `data_training`
--
ALTER TABLE `data_training`
  ADD PRIMARY KEY (`id_data`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `data_training`
--
ALTER TABLE `data_training`
  MODIFY `id_data` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;


Controller
find-sController.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Find_s extends CI_Controller {

/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -  
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in 
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* @see http://codeigniter.com/user_guide/general/urls.html
*/
public function index()
{
$data=$this->dmodel->getDataLearning()->result();
$j=0;
foreach($data as $row){
if($row->EnjoySport=='yes'){
$datalearning[$j]['Sky']=$row->Sky;
$datalearning[$j]['AirTemp']=$row->AirTemp;
$datalearning[$j]['Humidity']=$row->Humidity;
$datalearning[$j]['Wind']=$row->Wind;
$datalearning[$j]['Water']=$row->Water;
$datalearning[$j]['Forecast']=$row->Forecast;
$j=$j+1;
}
}
for($k=0;$k<$j;){
$datatemp[$k]['Sky'] = $datalearning[$k]['Sky'];
$datatemp[$k]['AirTemp']=$datalearning[$k]['AirTemp'];
$datatemp[$k]['Humidity']=$datalearning[$k]['Humidity'];
$datatemp[$k]['Wind']=$datalearning[$k]['Wind'];
$datatemp[$k]['Water']=$datalearning[$k]['Water'];
$datatemp[$k]['Forecast']=$datalearning[$k]['Forecast'];
$k=$k+1;
}
$k=0;
$a=0;
$b=0;
$c=0;
$d=0;
$e=0;
$f=0;
for($l=1;$l<$j;){
if(($datalearning[$l]['Sky'] == $datatemp[$k]['Sky'])and ($a==0)){
$hipotesa[$k]['Sky']=$datalearning[$l]['Sky'];
}
else{
$hipotesa[$k]['Sky']='*';
$a=1;
}
if(($datalearning[$l]['AirTemp'] == $datatemp[$k]['AirTemp'])and ($b==0)){
$hipotesa[$k]['AirTemp']=$datalearning[$l]['AirTemp'];
}
else{
$hipotesa[$k]['AirTemp']='*';
$b=1;
}
if(($datalearning[$l]['Humidity'] ==$datatemp[$k]['Humidity'])and ($c==0)){
$hipotesa[$k]['Humidity']=$datalearning[$l]['Humidity'];
}
else{
$hipotesa[$k]['Humidity']='*';
$c=1;
}
if(($datalearning[$l]['Wind'] ==$datatemp[$k]['Wind'])and ($d==0)){
$hipotesa[$k]['Wind']=$datalearning[$l]['Wind'];
}
else{
$hipotesa[$k]['Wind']='*';
$d=1;
}
if(($datalearning[$l]['Water'] ==$datatemp[$k]['Water'])and ($e==0)){
$hipotesa[$k]['Water']=$datalearning[$l]['Water'];
}
else{
$hipotesa[$k]['Water']='*';
$e=1;
}
if(($datalearning[$l]['Forecast'] ==$datatemp[$k]['Forecast'])and ($f==0)){
$hipotesa[$k]['Forecast']=$datalearning[$l]['Forecast'];
}
else{
$hipotesa[$k]['Forecast']='*';
$f=1;
}
$l=$l+1;
$k=$k+1;
}
for($k=0;$k<$j-1;){
$datalearn=array(
'Sky'=>$hipotesa[$k]['Sky'],
'AirTemp'=>$hipotesa[$k]['AirTemp'],
'Humidity'=>$hipotesa[$k]['Humidity'],
'Wind'=>$hipotesa[$k]['Wind'],
'Water'=>$hipotesa[$k]['Water'],
'Forecast'=>$hipotesa[$k]['Forecast']
);
$k=$k+1;
}
$this->load->view('header');
$this->load->view('find_s/learning_find_s',$datalearn);
$this->load->view('footer');
}
function uji(){
$this->load->view('header');
$this->load->view('find_s/ujicoba');
$this->load->view('footer');
}
function hasil(){
$data=$this->dmodel->getDataLearning()->result();
$j=0;
foreach($data as $row){
if($row->EnjoySport=='yes'){
$datalearning[$j]['Sky']=$row->Sky;
$datalearning[$j]['AirTemp']=$row->AirTemp;
$datalearning[$j]['Humidity']=$row->Humidity;
$datalearning[$j]['Wind']=$row->Wind;
$datalearning[$j]['Water']=$row->Water;
$datalearning[$j]['Forecast']=$row->Forecast;
$j=$j+1;
}
}
for($k=0;$k<$j;){
$datatemp[$k]['Sky'] = $datalearning[$k]['Sky'];
$datatemp[$k]['AirTemp']=$datalearning[$k]['AirTemp'];
$datatemp[$k]['Humidity']=$datalearning[$k]['Humidity'];
$datatemp[$k]['Wind']=$datalearning[$k]['Wind'];
$datatemp[$k]['Water']=$datalearning[$k]['Water'];
$datatemp[$k]['Forecast']=$datalearning[$k]['Forecast'];
$k=$k+1;
}
$k=0;
$a=0;
$b=0;
$c=0;
$d=0;
$e=0;
$f=0;
for($l=1;$l<$j;){
if(($datalearning[$l]['Sky'] == $datatemp[$k]['Sky'])and ($a==0)){
$hipotesa[$k]['Sky']=$datalearning[$l]['Sky'];
}
else{
$hipotesa[$k]['Sky']='*';
$a=1;
}
if(($datalearning[$l]['AirTemp'] == $datatemp[$k]['AirTemp'])and ($b==0)){
$hipotesa[$k]['AirTemp']=$datalearning[$l]['AirTemp'];
}
else{
$hipotesa[$k]['AirTemp']='*';
$b=1;
}
if(($datalearning[$l]['Humidity'] ==$datatemp[$k]['Humidity'])and ($c==0)){
$hipotesa[$k]['Humidity']=$datalearning[$l]['Humidity'];
}
else{
$hipotesa[$k]['Humidity']='*';
$c=1;
}
if(($datalearning[$l]['Wind'] ==$datatemp[$k]['Wind'])and ($d==0)){
$hipotesa[$k]['Wind']=$datalearning[$l]['Wind'];
}
else{
$hipotesa[$k]['Wind']='*';
$d=1;
}
if(($datalearning[$l]['Water'] ==$datatemp[$k]['Water'])and ($e==0)){
$hipotesa[$k]['Water']=$datalearning[$l]['Water'];
}
else{
$hipotesa[$k]['Water']='*';
$e=1;
}
if(($datalearning[$l]['Forecast'] ==$datatemp[$k]['Forecast'])and ($f==0)){
$hipotesa[$k]['Forecast']=$datalearning[$l]['Forecast'];
}
else{
$hipotesa[$k]['Forecast']='*';
$f=1;
}
$l=$l+1;
$k=$k+1;
}
for($k=0;$k<$j-1;){
$datalearn=array(
'Sky'=>$hipotesa[$k]['Sky'],
'AirTemp'=>$hipotesa[$k]['AirTemp'],
'Humidity'=>$hipotesa[$k]['Humidity'],
'Wind'=>$hipotesa[$k]['Wind'],
'Water'=>$hipotesa[$k]['Water'],
'Forecast'=>$hipotesa[$k]['Forecast']
);
$k=$k+1;
}
$ac=0;
if($datalearn['Sky']!='*'){
$ac=$ac+1;
}
if($datalearn['AirTemp']!='*'){
$ac=$ac+1;
}
if($datalearn['Humidity']!='*'){
$ac=$ac+1;
}
if($datalearn['Wind']!='*'){
$ac=$ac+1;
}
if($datalearn['Water']!='*'){
$ac=$ac+1;
}
if($datalearn['Forecast']!='*'){
$ac=$ac+1;
}
$count=0;
if($datalearn['Sky']==$_POST['sky']){
$count=$count+1;
}
if($datalearn['AirTemp']==$_POST['airtemp']){
$count=$count+1;
}
if($datalearn['Humidity']==$_POST['humidity']){
$count=$count+1;
}
if($datalearn['Wind']==$_POST['wind']){
$count=$count+1;
}
if($datalearn['Water']==$_POST['water']){
$count=$count+1;
}
if($datalearn['Forecast']==$_POST['forecast']){
$count=$count+1;
}
if($ac==$count){
$data['hasil']="sangat baik untuk olahraga";
}
else{
$data['hasil']="tidak baik untuk olahraga";
}
$this->load->view('header');
$this->load->view('find_s/hasil',$data);
$this->load->view('footer');
}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */



Model 
Dmodel.php


<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');


Class Dmodel extends CI_Model{
function getDataLearning(){
return $this->db->query("select *from data_training;");
}
}

View

uji.php

<h4>Tentukan Fakta yang ada:</h4>
<form name="form1" method="post" action="<?php echo base_url();?>index.php/find_s/hasil">
<table border="0" align="center">
<tr>
<th colspan="3">Sky</th>
<th colspan="3">Airtemp</th>
<th colspan="3">Humidity</th>
<th colspan="3">Wind</th>
<th colspan="3">Water</th>
<th colspan="3">Forecast</th>
</tr>
<tr>
<th colspan="3">
<select name="sky">
<option value="sunny">Sunny
<option value="rainy">Rainy
</select></th>
<th colspan="3">
<select name="airtemp">
<option value="warm">Warm
<option value="cold">Cold
</select></th>
<th colspan="3">
<select name="humidity">
<option value="normal">Normal
<option value="high">High
</select></th>

<th colspan="3">
<select name="wind">
<option value="strong">Strong
<option value="slow">Slow
</select></th>

<th colspan="3">
<select name="water">
<option value="warm">Warm
<option value="cool">Cool
</select></th>

<th colspan="3">
<select name="forecast">
<option value="same">Same
<option value="change">Change
</select></th>
</tr>
</table>
<br><br><br>
<table align="center">
<tr>
<th><input type="submit" value="submit" name="submit"></th>
</tr>
</table>
</form>

hasil.php

            <aside class="right-side">
                <!-- Content Header (Page header) -->
                <section class="content-header">
                    <h1>
                        Data Tables
                        
                    </h1>
                    <ol class="breadcrumb">
                        <li><a href="#"><i class="fa fa-dashboard"></i> Dashboard</a></li>
                        <li><a href="#">Algoritma Find S</a></li>
                        <li class="active">Learning Data</li>
                    </ol>
                </section>

                <!-- Main content -->
                <section class="content">
                    <div class="row">
                        <div class="col-xs-12">
                            <div class="box">
                                <div class="box-header">
                                    <h3 class="box-title">Learning Find S</h3>
                                </div><!-- /.box-header -->
                                <div class="box-body table-responsive">
<h1><?php echo $hasil?><h1>
 </div><!-- /.box-body -->
                            </div><!-- /.box -->
</div>
                    </div>

                </section><!-- /.content -->
            </aside><!-- /.right-side -->

Posting Komentar