-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxrs.c
More file actions
93 lines (78 loc) · 3.16 KB
/
maxrs.c
File metadata and controls
93 lines (78 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "/usr/include/sqlite3.h"
int maxrs_x_location;
int maxrs_y_location;
int maxrs_x_length;
int maxrs_y_length;
int max_score_in_grid;
float maxrs_score;
int RANGE;
int SCOPE;
int maxrs(void){
sqlite3 *conn = NULL;
sqlite3_stmt *stmt_find_total_score_in_scope = NULL;
sqlite3_stmt *stmt_find_total_score_in_grid = NULL;
int ret, i, j, decision = 0;
int x_reduction, y_reduction, x_length, y_length;
char find_total_score_sql[256];
int total_score_in_grid;
float alpha = 0.5, total_score_in_scope, current_score;
ret = sqlite3_open("study.sqlite", &conn);//SQLite3に接続
if(ret != SQLITE_OK){
printf("database is not open!\n");
return -1;
}
//MaxRSの初期値として、XY座標全体を領域とした場合を求める
sqlite3_prepare_v2(conn, "SELECT total(score) FROM point_table;", 64, &stmt_find_total_score_in_scope, NULL);
while(sqlite3_step(stmt_find_total_score_in_scope) == SQLITE_ROW ){//座標を設定
total_score_in_scope = sqlite3_column_int(stmt_find_total_score_in_scope, 0);
}
maxrs_x_location = 0;
maxrs_y_location = 0;
maxrs_x_length = SCOPE/RANGE;
maxrs_y_length = SCOPE/RANGE;
//maxrs_score = (float)alpha/maxrs_x_length/maxrs_y_length + (1-alpha)*total_score_in_scope;
maxrs_score = 0;
//一つずつ領域を小さくしていき、最適解を求める
for(x_reduction=SCOPE/RANGE*0.7; x_reduction<SCOPE/RANGE; x_reduction++){//x_reductionはSCOPEに比べて何マス分範囲が縮小されているか
for(y_reduction=SCOPE/RANGE*0.7; y_reduction<SCOPE/RANGE; y_reduction++){
x_length = SCOPE/RANGE - x_reduction;//領域のx方向の大きさ
y_length = SCOPE/RANGE - y_reduction;//領域のy方向の大きさ
for(i=0; i<=x_reduction; i++){//x方向に動かす回数
for(j=0; j<=y_reduction; j++){//y方向に動かす回数
sprintf(find_total_score_sql, "select total(number) from grid_table where x_range >= %d and x_range <= %d and y_range >= %d and y_range <= %d;", 0+i, x_length+i-1, 0+j, y_length+j-1);
sqlite3_prepare_v2(conn, find_total_score_sql, 512, &stmt_find_total_score_in_grid, NULL);
while(sqlite3_step(stmt_find_total_score_in_grid) == SQLITE_ROW ){
total_score_in_grid = sqlite3_column_int(stmt_find_total_score_in_grid, 0);//現在のグリッドの合計スコア
}
current_score = (float)alpha/x_length/y_length + (1-alpha)*total_score_in_grid;
//現在のスコアが従来のmaxrs_scoreを超えたら行われる処理
if(current_score > maxrs_score){
maxrs_x_location = i;
maxrs_y_location = j;
maxrs_x_length = x_length;
maxrs_y_length = y_length;
maxrs_score = current_score;
/*decision = 1;//終了処理
break;*/
}
}
if(decision == 1){
break;
}
}
if(decision == 1){
break;
}
}
if(decision == 1){
break;
}
}
sqlite3_finalize(stmt_find_total_score_in_scope);
sqlite3_finalize(stmt_find_total_score_in_grid);
sqlite3_close(conn);
return 0;
}