/**
  * @param InputInterface $input
  * @param OutputInterface $output
  * @return int
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $stmt = $this->db->query("SELECT stop_lat, stop_lon FROM stops WHERE stop_code != ''");
     $space = new Space(2);
     while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
         $space->addPoint([$row['stop_lat'] * 10000, $row['stop_lon'] * 10000]);
     }
     $clusters = $space->solve(40, Space::SEED_DASV);
     $stmt = $this->db->prepare("UPDATE stops SET zone_id = :cluster WHERE stop_lat = :lat AND stop_lon = :lon");
     foreach ($clusters as $i => $cluster) {
         foreach ($cluster as $station) {
             list($x, $y) = $station->getCoordinates();
             $stmt->execute(['cluster' => $i, 'lat' => $x / 10000, 'lon' => $y / 10000]);
         }
     }
     return 0;
 }
Exemple #2
0
 public function __construct(Space $space, array $coordinates)
 {
     $this->space = $space;
     $this->dimention = $space->getDimention();
     $this->coordinates = $coordinates;
 }
 /**
  * Display a listing of the resource.
  *
  * @return Response
  */
 public function target(Request $request)
 {
     try {
         $mecanexusers = MecanexUser::all();
         // filter by demographics
         if ($request->gender_id != 0) {
             $mecanexusers = $mecanexusers->where("gender_id", (int) $request->gender_id);
         }
         if ($request->age_id != 0) {
             $mecanexusers = $mecanexusers->where("age_id", (int) $request->age_id);
         }
         if ($request->education_id != 0) {
             $mecanexusers = $mecanexusers->where("education_id", (int) $request->education_id);
         }
         if ($request->occupation_id != 0) {
             $mecanexusers = $mecanexusers->where("occupation_id", (int) $request->occupation_id);
         }
         if ($request->country_id != 0) {
             $mecanexusers = $mecanexusers->where("country_id", (int) $request->country_id);
         }
         // filter by preference on terms
         // keep mecanex users whose term profile score is more than 0.7 when normalized by the max term value of the user
         $statusCode = 200;
         if ($request->terms != []) {
             $all_terms = Term::all();
             foreach ($all_terms as $term) {
                 if (strpos($request->terms, $term->term) !== false) {
                     $mecanexusers = $mecanexusers->filter(function ($user) use($term) {
                         $myquery = DB::select(DB::raw(' SELECT MAX(profile_score) as mymax
                                                        FROM users_terms_profilescores
                                                        WHERE mecanex_user_id=' . $user->id . ''));
                         if ($myquery[0]->mymax == 0) {
                             // don't take into account users that have not provided any information
                             return false;
                         }
                         return $user->profilescore[$term->id - 1]['pivot']['profile_score'] / $myquery[0]->mymax > 0.7;
                         // minus one because terms start from 0 where id starts from 1
                     });
                     //                        $response[] = $mecanexusers[1]->profilescore[$term->id-1]['pivot']['profile_score'];
                     //                        $myquery = DB::select(DB::raw(' SELECT MAX(profile_score) as mymax
                     //                                                           FROM users_terms_profilescores
                     //                                                           WHERE mecanex_user_id='. $mecanexusers[2]->id .''));
                 }
             }
         }
         $array_of_users = array();
         // If no mecanexusers exist from the filters selected, we return a random list of videos
         if ($mecanexusers->isEmpty()) {
             $response = [];
             $response['message'][] = "No information for the target group";
             $statusCode = 200;
             return $response;
         }
         foreach ($mecanexusers as $mecanexuser) {
             $array = array();
             $terms = $mecanexuser->profilescore;
             foreach ($terms as $term) {
                 $temp = (double) $term['pivot']['profile_score'];
                 array_push($array, $temp);
             }
             // normalize with maximum because that way the clustering seems more accurate
             $maximum = max($array);
             if ($maximum != 0) {
                 for ($i = 0; $i < count($array); $i++) {
                     $array[$i] = $array[$i] / $maximum;
                 }
             }
             array_push($array_of_users, $array);
         }
         /** return the array of users so that we can see whether the profiles have normalized
          * values so that they can be clustered properly
          */
         $space = new Space(14);
         $num_of_clusters = 5;
         foreach ($array_of_users as $point) {
             $space->addPoint($point);
         }
         $clusters = $space->solve($num_of_clusters);
         $all_users = [];
         //            foreach ($clusters as $i => $cluster)
         //                printf("Cluster %d [%d,%d]: %d points\n", $i, $cluster[0], $cluster[1], count($cluster));
         foreach ($clusters as $i => $cluster) {
             $terms = [];
             for ($j = 0; $j < 14; $j++) {
                 array_push($terms, $cluster[$j]);
             }
             $all_users[] = ['cluster_id' => $i, 'cluster_terms' => $terms, 'num_of_users' => count($cluster)];
         }
         $statusCode = 200;
         $response = $all_users;
         return $response;
     } catch (Exception $e) {
         $statusCode = 400;
     } finally {
         return Response::json($response, $statusCode);
     }
 }
 /**
  * Return clusterheads and corresponding videos for filtered users.
  *
  * @return Clusterheads and videos
  */
 public function professional(Request $request)
 {
     try {
         $mecanexusers = MecanexUser::all();
         // filter by demographics
         if ($request->gender_id != 0) {
             $mecanexusers = $mecanexusers->where("gender_id", (int) $request->gender_id);
         }
         if ($request->age_id != 0) {
             $mecanexusers = $mecanexusers->where("age_id", (int) $request->age_id);
         }
         if ($request->education_id != 0) {
             $mecanexusers = $mecanexusers->where("education_id", (int) $request->education_id);
         }
         if ($request->occupation_id != 0) {
             $mecanexusers = $mecanexusers->where("occupation_id", (int) $request->occupation_id);
         }
         if ($request->country_id != 0) {
             $mecanexusers = $mecanexusers->where("country_id", (int) $request->country_id);
         }
         if ($request->num != 0) {
             $num = $request->num;
         } else {
             $num = 50;
         }
         // filter by preference on terms
         // keep mecanex users whose term profile score is more than 0.7 when normalized by the max term value of the user
         if ($request->terms != []) {
             $all_terms = Term::all();
             foreach ($all_terms as $term) {
                 if (strpos($request->terms, $term->term) !== false) {
                     $mecanexusers = $mecanexusers->filter(function ($user) use($term) {
                         $myquery = DB::select(DB::raw(' SELECT MAX(profile_score) as mymax
                                                        FROM users_terms_profilescores
                                                        WHERE mecanex_user_id=' . $user->id . ''));
                         if ($myquery[0]->mymax == 0) {
                             // don't take into account users that have not provided any information
                             return false;
                         }
                         return $user->profilescore[$term->id - 1]['pivot']['profile_score'] / $myquery[0]->mymax > 0.7;
                         // minus one because terms start from 0 where id starts from 1
                     });
                     //                        $response[] = $mecanexusers[1]->profilescore[$term->id-1]['pivot']['profile_score'];
                     //                        $myquery = DB::select(DB::raw(' SELECT MAX(profile_score) as mymax
                     //                                                           FROM users_terms_profilescores
                     //                                                           WHERE mecanex_user_id='. $mecanexusers[2]->id .''));
                 }
             }
         }
         $array_of_users = array();
         // If no mecanexusers exist from the filters selected, we return a random list of videos
         if ($mecanexusers->isEmpty()) {
             if ($request->videos == null) {
                 $videos = Video::orderByRaw('RAND()')->take($num)->get();
             } else {
                 $reqvideos = "'" . str_replace(",", "','", $request->videos) . "'";
                 //                    $videos = Video::where('video_id','in',['EUS_025A722EA4B240D8B6F6330A8783143C'])->orderByRaw('RAND()')->take($num)->get();
                 $videos = DB::select(DB::raw(' SELECT *
                                         FROM videos
                                         WHERE video_id IN (' . $reqvideos . ') ORDER BY RAND()  LIMIT ' . $num . ''));
             }
             $response = [];
             foreach ($videos as $video) {
                 $response['videos'][] = $video->video_id;
             }
             $statusCode = 200;
             return $response;
         }
         foreach ($mecanexusers as $mecanexuser) {
             $array = array();
             $terms = $mecanexuser->profilescore;
             foreach ($terms as $term) {
                 $temp = (double) $term['pivot']['profile_score'];
                 array_push($array, $temp);
             }
             array_push($array_of_users, $array);
         }
         /** return the array of users so that we can see whether the profiles have normalized
          * values so that they can be clustered properly
          */
         //            return $array_of_users;
         $space = new Space(14);
         $num_of_clusters = 5;
         foreach ($array_of_users as $point) {
             $space->addPoint($point);
         }
         $clusters = $space->solve($num_of_clusters);
         $statusCode = 200;
         $all_users = [];
         //            foreach ($clusters as $i => $cluster)
         //                printf("Cluster %d [%d,%d]: %d points\n", $i, $cluster[0], $cluster[1], count($cluster));
         foreach ($clusters as $i => $cluster) {
             $terms = [];
             for ($j = 0; $j < 14; $j++) {
                 array_push($terms, $cluster[$j]);
             }
             $all_users[] = ['cluster_id' => $i, 'cluster_terms' => $terms, 'num_of_users' => count($cluster)];
         }
         $cluster_list = [];
         foreach ($all_users as $user) {
             $cluster_list[] = ['cluster_id' => $user['cluster_id'], 'video_ids' => $this->recommend_video($user['cluster_terms'], $request), 'cluster_terms' => $user['cluster_terms'], 'num_of_users' => $user['num_of_users']];
         }
         //            $response = $cluster_list[0]['video_ids'][0]->id;
         $video_list = [];
         $response = [];
         $num_of_videos = 0;
         $total_num_of_videos = count($cluster_list[0]['video_ids']);
         for ($i = 0; $i < $total_num_of_videos; $i++) {
             for ($j = 0; $j < $num_of_clusters; $j++) {
                 $video = $cluster_list[$j]['video_ids'][$i];
                 if (!in_array($video->video_id, $video_list, true)) {
                     $video_list[] = $video->video_id;
                     $response['videos'][] = $video->euscreen_id;
                     $num_of_videos += 1;
                 }
                 if ($num_of_videos == $total_num_of_videos) {
                     break;
                 }
             }
             if ($num_of_videos == $total_num_of_videos) {
                 break;
             }
         }
         return $response;
     } catch (Exception $e) {
         $statusCode = 400;
     } finally {
         return Response::json($response, $statusCode);
     }
 }