Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php if ( ! defined('BASEPATH')) exit ('No direct script access allowed');
- /**
- * @author Cahya DSN
- * @email cahyadsn@gmail.com
- * @link http://adzan.16mb..com
- */
- class Main extends CI_Controller {
- private $actual_value=array();
- private $variation=array();
- private $num_of_data;
- private $U=array();
- private $u=array();
- private $un=array();
- private $sorted_un=array();
- private $l,$d1,$d2,$max,$min;
- private $highest_num=array();
- private $u_s1=array();
- private $u_sub1=array();
- private $parameter1=array();
- private $u_s2=array();
- private $u_sub2=array();
- private $parameter2=array();
- private $u_s3=array();
- private $u_sub3=array();
- private $mean=array();
- private $urut=array();
- private $grup=array();
- private $sum_of_mean=array();
- private $forecast_value=array();
- private $fer=array();
- private $hasil=array();
- function __construct()
- {
- parent::__construct();
- $this->load->model('forecast_model','_forecast');
- list($this->actual_value,$this->variation,$this->num_of_data,$this->max,$this->min)=$this->_forecast->get_data_values();
- $this->_public_view = $this->config->item('public_view');
- }
- function index()
- {
- $data['title']="Stock Price Forecasting System";
- $data['current'] = 'HOME';
- $this->load->view('main/index',$data);
- }
- function step1()
- {
- $this->load->helper('form');
- $data['title']="Stock Price Forecasting System - Step 1";
- $data['current']= 'PROCESS';
- $data['actual_value']=$this->actual_value;
- $data['num_of_data'] = $this->num_of_data;
- $data['variation']= $this->variation;
- $data['max'] = $this->max;
- $data['min'] = $this->min;
- $data['U']=$this->print_U();
- $this->load->view('main/step1',$data);
- }
- function step2_process($num_initial_interval)
- {
- $this->get_initial_length($num_initial_interval);
- $this->get_initial_interval($num_initial_interval);
- $this->get_interval_frequency($num_initial_interval);
- $this->sorted_initial_interval();
- $this->calculate_highest_freq($num_initial_interval);
- }
- function step2()
- {
- $this->load->helper('form');
- $data['current']='PROCESS';
- $data['title']="Stock Price Forecasting System - Step 2";
- $num_initial_interval=$this->input->post('num_initial_interval')?$this->input->post('num_initial_interval'):11;
- $data['num_initial_interval']=$num_initial_interval;
- $this->step2_process($num_initial_interval);
- $data['length_initial_interval']=$this->print_initial_length($num_initial_interval);
- $data['list_initial_interval']=$this->print_initial_interval($num_initial_interval);
- $data['list_initial_interval_freq']=$this->print_interval_frequency($num_initial_interval);
- $data['list_sorted_initial_interval']=$this->print_sorted_initial_interval();
- $data['devided_1st']=$this->print_devided_1st();
- $data['devided_2nd']=$this->print_devided_2nd();
- $data['devided_3rd']=$this->print_devided_3rd();
- $data['rebuild_interval']=$this->print_rebuild_interval($num_initial_interval);
- $data['num_new_interval']=$this->get_num_new_interval();
- $this->load->view('main/step2',$data);
- }
- function step3_process($num_initial_interval)
- {
- $this->step2_process($num_initial_interval);
- $this->devided_1st();
- $this->devided_2nd();
- $this->devided_3rd();
- $this->rebuild_interval($num_initial_interval);
- }
- function step3()
- {
- $this->load->helper('form');
- $data['current']='PROCESS';
- $data['title']="Stock Price Forecasting System - Step 3";
- $num_initial_interval=$this->input->post('num_initial_interval')?$this->input->post('num_initial_interval'):11;;
- $this->step3_process($num_initial_interval);
- $data['list_of_fuzzy_sets']=$this->get_fuzzy_sets();
- $data['fuzzification']=$this->get_fuzzification();
- $data['flr']=$this->get_flr();
- $data['flrg']=$this->print_flrg();
- $data['forecast']=$this->print_forecast();
- $this->save_forecast();
- $data['fer']=$this->get_fer();
- $data['afer']=$this->get_afer();
- $this->load->view('main/step3',$data);
- }
- function get_U()
- {
- $this->U['min']=floor($this->min);
- $this->d1=$this->min-$this->U['min'];
- $this->U['max']=ceil($this->max);
- $this->d2=$this->U['max']-$this->max;
- }
- function print_U()
- {
- $str='';
- $this->get_U();
- $str="U=[D<sub>min</sub> - d<sub>1</sub>,D<sub>max</sub> - d<sub>2</sub>]<br>U=[".$this->min." - ".$this->d1.",".$this->max." - ".$this->d2."]<br>U=[".$this->U['min'].",".$this->U['max']."]";
- return $str;
- }
- function get_initial_length($num_initial_interval)
- {
- $this->U['min']=floor($this->min);
- $this->U['max']=ceil($this->max);
- $this->l= ($this->U['max']-$this->U['min'])/$num_initial_interval;
- }
- function print_initial_length($num_initial_interval)
- {
- $str='';
- $str.="l= (U<sub>max</sub>-U<sub>min</sub>)/n<br>" ;
- $str.="l= (".$this->U['max']."-".($this->U['min']<0?"(".$this->U['min'].")":$this->U['min']).")/".$num_initial_interval."<br>" ;
- $str.="l=".$this->l;
- return $str;
- }
- function get_initial_interval($num_initial_interval)
- {
- for($i=0;$i<$num_initial_interval;$i++){
- $this->u[$i]['min']=($i==0?$this->U['min']:$this->u[$i-1]['max']);
- $this->u[$i]['max']=$this->u[$i]['min']+$this->l;
- }
- }
- function print_initial_interval($num_initial_interval)
- {
- $str='';
- for($i=0;$i<$num_initial_interval;$i++){
- $str.= "u<sub>".($i+1)."</sub>=[".$this->u[$i]['min'].",".$this->u[$i]['max']."]<br>";
- }
- return $str;
- }
- function get_interval_frequency($num_initial_interval)
- {
- for($i=0;$i<$this->num_of_data-1;$i++){
- for($j=0;$j<$num_initial_interval;$j++){
- if(!isset($this->un[$j])){
- $this->un[$j]=array();
- $this->un[$j][0]=0;
- $this->un[$j][1]=$j+1;
- }else{
- $this->un[$j][0]+=($this->u[$j]['min']<=$this->variation[$i] && $this->variation[$i]<=$this->u[$j]['max'])?1:0;
- }
- }
- }
- }
- function print_interval_frequency($num_initial_interval)
- {
- $str='';
- for($i=0;$i<$num_initial_interval;$i++){
- $str.="A<sub>".$this->un[$i][1]."</sub>=".$this->un[$i][0]."<br>";
- }
- return $str;
- }
- function sorted_initial_interval()
- {
- $this->sorted_un=$this->un;
- foreach ($this->sorted_un as $key => $row) {
- $num[$key] = $row[0];
- $idx[$key] = $row[1];
- }
- array_multisort($num, SORT_DESC, $idx, SORT_ASC, $this->sorted_un);
- }
- function print_sorted_initial_interval()
- {
- $str='';
- foreach($this->sorted_un as $key=>$val){
- $str.="A<sub>".$val[1]."</sub>=".$val[0]."<br>";
- }
- return $str;
- }
- function calculate_highest_freq($num_initial_interval)
- {
- $c=0;
- for($i=0;$i<$num_initial_interval;$i++){
- if($i==0){
- $this->highest_num[$c]=array();
- $this->highest_num[$c][0]=$this->sorted_un[0][0];
- $this->highest_num[$c][1]=$this->sorted_un[0][1];
- $c++;
- $d=1;
- }else{
- if($this->sorted_un[$i][0]==$this->highest_num[$c-1][0]){
- $d++;
- $this->highest_num[$c-1][$d]=$this->sorted_un[$i][1];
- }else{
- $this->highest_num[$c]=array();
- $this->highest_num[$c][0]=$this->sorted_un[$i][0];
- $this->highest_num[$c][1]=$this->sorted_un[$i][1];
- $c++;
- $d=1;
- }
- }
- }
- }
- function devided_1st()
- {
- $n_1st=count($this->highest_num[0]);
- for($i=1;$i<$n_1st;$i++){
- $u_max[$i-1]=$this->u[$this->highest_num[0][$i]-1]['max'];
- $u_min[$i-1]=$this->u[$this->highest_num[0][$i]-1]['min'];
- $u_l[$i-1]=($u_max[$i-1]-$u_min[$i-1])/4;
- for($j=0;$j<4;$j++){
- $this->u_sub1[$i][$j]['min']=($j==0?$u_min[$i-1]:$this->u_sub1[$i][$j-1]['max']);
- $this->u_sub1[$i][$j]['max']=$this->u_sub1[$i][$j]['min']+$u_l[$i-1];
- }
- $this->u_s1=(isset($this->u_s1)?array_merge($this->u_s1,$this->u_sub1[$i]):$this->u_sub1[$i]);
- unset($this->u[$this->highest_num[0][$i]-1]);
- }
- $this->parameter1=array($n_1st,$u_max,$u_min,$u_l);
- }
- function print_devided_1st()
- {
- $this->devided_1st();
- $str='';
- for($i=1;$i<$this->parameter1[0];$i++){
- $str.="u<sub>".$this->highest_num[0][$i]."</sub>=[".$this->parameter1[2][$i-1].",".$this->parameter1[1][$i-1]."]<ul>";
- for($j=0;$j<4;$j++){
- $str.="<li>u<sub>".$this->highest_num[0][$i].",".($j+1)."</sub>=[".round($this->u_sub1[$i][$j]['min'],2).",".round($this->u_sub1[$i][$j]['max'],2)."]</li>";
- }
- $str.="</ul>";
- }
- return $str;
- }
- function devided_2nd()
- {
- $n_2nd=count($this->highest_num[1]);
- for($i=1;$i<$n_2nd;$i++){
- $u_max2[$i-1]=$this->u[$this->highest_num[1][$i]-1]['max'];
- $u_min2[$i-1]=$this->u[$this->highest_num[1][$i]-1]['min'];
- $u_l2[$i-1]=($u_max2[$i-1]-$u_min2[$i-1])/3;
- for($j=0;$j<3;$j++){
- $this->u_sub2[$i][$j]['min']=($j==0?$u_min2[$i-1]:$this->u_sub2[$i][$j-1]['max']);
- $this->u_sub2[$i][$j]['max']=$this->u_sub2[$i][$j]['min']+$u_l2[$i-1];
- }
- $this->u_s2=(isset($this->u_s2)?array_merge($this->u_s2,$this->u_sub2[$i]):$this->u_sub2[$i]);
- unset($this->u[$this->highest_num[1][$i]-1]);
- }
- $this->parameter2=array($n_2nd,$u_max2,$u_min2,$u_l2);
- }
- function print_devided_2nd()
- {
- $this->devided_2nd();
- $str='';
- for($i=1;$i<$this->parameter2[0];$i++){
- $str.="u<sub>".$this->highest_num[1][$i]."</sub>=[".$this->parameter2[2][$i-1].",".$this->parameter2[1][$i-1]."]<ul>";
- for($j=0;$j<3;$j++){
- $str.="<li>u<sub>".$this->highest_num[1][$i].",".($j+1)."</sub>=[".round($this->u_sub2[$i][$j]['min'],2).",".round($this->u_sub2[$i][$j]['max'],2)."]</li>";
- }
- $str.="</ul>";
- }
- return $str;
- }
- function devided_3rd()
- {
- $n_3rd=count($this->highest_num[2]);
- for($i=1;$i<$n_3rd;$i++){
- $u_max3[$i-1]=$this->u[$this->highest_num[2][$i]-1]['max'];
- $u_min3[$i-1]=$this->u[$this->highest_num[2][$i]-1]['min'];
- $u_l3[$i-1]=($u_max3[$i-1]-$u_min3[$i-1])/2;
- for($j=0;$j<2;$j++){
- $this->u_sub3[$i][$j]['min']=($j==0?$u_min3[$i-1]:$this->u_sub3[$i][$j-1]['max']);
- $this->u_sub3[$i][$j]['max']=$this->u_sub3[$i][$j]['min']+$u_l3[$i-1];
- }
- $this->u_s3=(isset($this->u_s3)?array_merge($this->u_s3,$this->u_sub3[$i]):$this->u_sub3[$i]);
- unset($this->u[$this->highest_num[2][$i]-1]);
- }
- $this->parameter3=array($n_3rd,$u_max3,$u_min3,$u_l3);
- }
- function print_devided_3rd()
- {
- $this->devided_3rd();
- $str='';
- for($i=1;$i<$this->parameter3[0];$i++){
- $str.="u<sub>".$this->highest_num[2][$i]."</sub>=[".$this->parameter3[2][$i-1].",".$this->parameter3[1][$i-1]."]<ul>";
- for($j=0;$j<2;$j++){
- $str.="<li>u<sub>".$this->highest_num[2][$i].",".($j+1)."</sub>=[".round($this->u_sub3[$i][$j]['min'],2).",".round($this->u_sub3[$i][$j]['max'],2)."]</li>";
- }
- $str.="</ul>";
- }
- return $str;
- }
- function rebuild_interval($num_initial_interval)
- {
- for($i=0;$i<$num_initial_interval;$i++){
- if($this->un[$i][0]==0) unset($this->u[$this->un[$i][1]-1]);
- }
- $this->u=array_merge($this->u,$this->u_s1,$this->u_s2,$this->u_s3);
- sort($this->u);
- }
- function print_rebuild_interval($num_initial_interval)
- {
- $this->rebuild_interval($num_initial_interval);
- $str='';
- for($i=0;$i<$this->get_num_new_interval();$i++){
- $str.="u<sub>".($i+1)."</sub>=[".round($this->u[$i]['min'],2).",".round($this->u[$i]['max'],2)."]<br>";
- }
- return $str;
- }
- function get_num_new_interval()
- {
- $c=count($this->u);
- return $c;
- }
- function get_fuzzy_sets()
- {
- $str='';
- for($i=0;$i<$this->get_num_new_interval();$i++){
- $this->mean[$i] = round(($this->u[$i]['max']+$this->u[$i]['min'])/2,2);
- $this->sum_of_mean[$i] = $this->mean[$i];
- $str.="A<sub>".($i+1)."</sub>=>".$this->mean[$i]."<br>";
- }
- return $str;
- }
- function get_fuzzification()
- {
- $str='';
- for($i=0;$i<$this->num_of_data-1;$i++){
- $index_no = 1;
- foreach($this->u as $t){
- if($this->variation[$i] >=$t['min'] && $this->variation[$i]<=$t['max']){
- $str.=round($this->variation[$i],2).' => A<sub>'.$index_no.'</sub><br/>';
- $this->urut[]=$index_no;
- break;
- }
- $index_no++;
- }
- }
- return $str;
- }
- function get_flr()
- {
- $str='';
- for($i=0;$i<(count($this->urut)-1);$i++){
- $str.='A<sub>'.$this->urut[$i].'</sub> => A<sub>'.$this->urut[$i+1].'</sub><br/>';
- $this->grup[]=array($this->urut[$i],$this->urut[$i+1]);
- }
- return $str;
- }
- function get_flrg()
- {
- $first=array();
- $second=array();
- foreach ($this->grup as $key => $row) {
- $first[$key] = $row[0];
- $second[$key] = $row[1];
- }
- array_multisort($first, SORT_ASC, $second, SORT_ASC, $this->grup);
- $hasil=array();
- $c=0;
- for($i=0;$i<count($this->grup);$i++){
- if($i==0){
- $hasil[]=array($this->grup[0][0],array($this->grup[0][1]));
- $c++;
- }else{
- if($hasil[$c-1][0]==$this->grup[$i][0]){
- if($hasil[$c-1][1][count($hasil[$c-1][1])-1]!=$this->grup[$i][1]){
- $hasil[$c-1][1][]=$this->grup[$i][1];
- }
- }else{
- $hasil[]=array($this->grup[$i][0],array($this->grup[$i][1]));
- $c++;
- }
- }
- }
- for($i=0;$i<count($hasil);$i++){
- $this->sum_of_mean[$hasil[$i][0]-1]=0;
- $n=count($hasil[$i][1]);
- for($j=0;$j<$n;$j++){
- $this->sum_of_mean[$hasil[$i][0]-1]+=$this->mean[$hasil[$i][1][$j]-1];
- }
- $this->sum_of_mean[$hasil[$i][0]-1]/=$n;
- }
- return $hasil;
- }
- function print_flrg()
- {
- $this->hasil=$this->get_flrg();
- $str='';$j=0;
- for($i=0;$i<count($this->sum_of_mean);$i++){
- $str.='A<sub>'.($i+1).'</sub>';
- if($this->hasil[$j][0]==$i){
- $str.='=>';
- $n=count($this->hasil[$j][1]);
- for($k=0;$k<$n;$k++){
- $str.=($k==0?'':',').'A<sub>'.$this->hasil[$j][1][$k].'</sub>';
- }
- $j++;
- }
- $str.="=".round($this->sum_of_mean[$i],2)."<br/>";
- }
- return $str;
- }
- function get_forecast()
- {
- for($i=0;$i<$this->num_of_data+1;$i++){
- $forecast_value[$i]=($i>1?$this->actual_value[$i-1]+$this->sum_of_mean[$this->urut[$i-2]-1]:0);
- }
- return $forecast_value;
- }
- function print_forecast()
- {
- $str='';
- $this->forecast_value=$this->get_forecast();
- for($i=0;$i<$this->num_of_data+1;$i++){
- $str.=(isset($this->actual_value[$i])?$this->actual_value[$i]:"?")."=>".round($this->forecast_value[$i],2)."<br>";
- }
- return $str;
- }
- function save_forecast()
- {
- $this->_forecast->save_data_forecast($this->num_of_data,$this->actual_value,$this->forecast_value);
- }
- function chart()
- {
- $record=$this->_forecast->get_forecast();
- $actual_series='';
- $forecast_series='';
- $date_series='';
- $i=0;
- foreach($record as $dt){
- $actual_series.=($i==0?"":",").$dt->actual_value;
- $forecast_series.=($i==0?"":",").$dt->forecast_value;
- $date_series.=($i==0?"'":",'").$dt->date."'";
- $i++;
- }
- $data['actual_series']=$actual_series;
- $data['forecast_series']=$forecast_series;
- $data['date_series']=$date_series;
- $this->load->view('main/chart',$data);
- }
- function get_fer()
- {
- $str='';
- for($i=2;$i<$this->num_of_data;$i++){
- $this->fer[$i]=abs(round((($this->actual_value[$i]-$this->forecast_value[$i])/$this->actual_value[$i]),5));
- $str.="|(A<sub>".($i+1)."</sub>-F<sub>".($i+1)."</sub>)/A<sub>".($i+1)."</sub>|=".$this->fer[$i]."<br>";
- }
- return $str;
- }
- function get_afer()
- {
- $afer=0;
- for($i=2;$i<$this->num_of_data;$i++){
- $afer+=$this->fer[$i];
- }
- $afer/=($this->num_of_data-1);
- return "AFER = ".round(($afer*100),5)."%";
- }
- function delete_timeseries(){
- $this->_forecast->delete();
- redirect('main/index');
- }
- function intro()
- {
- $data['title']="Stock Price Forecasting System";
- $data['current'] = 'INTRO';
- $this->load->view('main/intro',$data);
- }
- function uploads()
- {
- $data['title']="Stock Price Forecasting System";
- $data['current'] = 'UPLOAD';
- $this->load->view('main/upload',$data);
- }
- function upload_file()
- {
- $data['title']="Stock Price Forecasting System";
- $data['current'] = 'UPLOAD';
- $config['upload_path'] = 'upload';
- $config['allowed_types'] = 'xls';
- $config['overwrite'] = true;
- $config['max_size'] = '3000';
- $this->load->library('upload', $config);
- if(!$this->upload->do_upload('xls')){
- $this->session->set_flashdata('error_msg', $this->upload->display_errors());
- redirect('main/uploads',$data);
- }else{
- $data = $this->upload->data();
- $this->load->library('spreadsheet_excel_reader');
- $this->spreadsheet_excel_reader->setOutputEncoding('CP1251');
- $file = $_SERVER['DOCUMENT_ROOT']."/forecasting/upload/".$data['file_name'];
- @$this->spreadsheet_excel_reader->read($file);
- $data = @$this->spreadsheet_excel_reader->sheets[0];
- $query_string = "INSERT INTO time_series (date, actual_value) VALUES ";
- for($i = 2; $i <= $data['numRows']; $i++){
- $tanggal=explode('/',$data['cells'][$i][1]);
- $tgl=$tanggal[2].'-'.$tanggal[1].'-'.$tanggal[0];
- $query_string .="('".$tgl."', ".$data['cells'][$i][2]."), ";
- }
- $sql = substr($query_string, 0, strlen($query_string)-2);
- $this->db->query($sql);
- $this->session->set_flashdata('success_msg','File upload successfully');
- redirect('main/uploads',$data);
- }
- }
- }
- /* End of file main.php */
- /* Location: ./application/controllers/main.php */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement