-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmp.run
150 lines (110 loc) · 2.53 KB
/
cmp.run
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# OTDM Lab 3: run file
# Marcel, Mengxue
# Autumn 2021
# To run this file:
# $> ampl: include cmp.run;
#########
# SETUP #
#########
reset;
model cmp.mod;
data "./data/moons.dat"; #s1 #spiral #worms #moons
option solver cplex; #gurobi
#display A;
display k,m,n;
#################
# PREPROCESSING #
#################
# Calculate Euclidean distances matrix
#for {i in {1..m}} {
# for {j in {1..m}} {
# let D[i,j] := sqrt((A[j,1] - A[i,1])^2 + (A[j,2] - A[i,2])^2);
# }
#}
# Calculate n-dimensional Euclidean distances matrix D
param partsum default 0;
for {i in {1..m}} {
for {j in {1..m}} {
for {dim in {1..n}} {
let partsum := partsum + (A[j,dim] - A[i,dim])^2
}
let D[i,j] := sqrt(partsum);
let partsum := 0;
}
}
#display D;
#########
# SOLVE #
#########
problem CMP: x, objf, c1, c2, c3;
solve CMP;
#display x;
#display x > './out/x.txt';
###################
# POST-PROCESSING #
###################
var clusters{i in 1..m};
for{i in 1..m} {
for{j in 1..m} {
let clusters[i] := clusters[i] + x[j,i];
}
}
print "";
for{i in 1..m} {
if clusters[i] > 0 then {
print "Cluster", i, "has", clusters[i], "points";
}
}
print "";
for{i in 1..m} {
for{j in 1..m} {
if x[i,j] > 0 then {
print "Point", i, "belongs to cluster", j;
}
}
}
display _total_solve_elapsed_time;
#display clusters;
#display clusters > './out/clusters.txt';
#########
# TRAIN #
#########
# Solve the primal
#reset;
#print "SVM_PRIMAL:";
#model svm-primal.mod;
#data "./data/size2000-seed75421.dat"; #spambase #size100-seed66407 #size2000-seed75421
#option solver cplex; #gurobi
#problem SVM_PRIMAL: w, gamma, s, primal, c1, c2;
#solve SVM_PRIMAL;
#display w, gamma, s;
# Compute w, gamma from the dual solution
#param w {1..n};
#let {j in {1..n}} w[j] := sum{i in {1..m}} lambda[i]*y_train[i]*A_train[i,j];
#display w;
#param gamma;
#for {i in {1..m}} {
# if lambda[i] > 0.01 and lambda[i] < nu*0.99 then {
# # A support vector point was found
# let gamma := 1/y_train[i] - sum{j in {1..n}} w[j]*A_train[i,j];
# break;
# }
#}
#display gamma;
########
# TEST #
########
# Predict values with the test dataset
#param y_pred {1..m};
#let {i in {1..m}} y_pred[i] := gamma + sum{j in {1..n}}w[j]*A_test[i,j];
#let {i in {1..m}} y_pred[i] := if y_pred[i] <= 0 then -1 else 1;
#display y_pred;
# Check misclassifications
#param misclassifications default 0;
#for {i in {1..m}} {
# if y_pred[i] != y_test[i] then
# let misclassifications := misclassifications + 1;
#}
#display misclassifications;
#param accuracy = (m - misclassifications) / m;
#display accuracy;