-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnext_move.m
95 lines (93 loc) · 3.37 KB
/
next_move.m
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
opt = optimoptions('fmincon','StepTolerance',1e-5);
max_depth = 100;
while true
fid = fopen('info.txt','r');
a = fread(fid);
fclose(fid);
if a == '1'
x0 = csvread('x.csv');
y0 = csvread('y.csv');
output = fmincon(@(t)norm(t),rand(size(x0,2),1),[-1*eye(size(x0,2));eye(size(x0,2))],...
[zeros(size(x0,2),1);ones(size(x0,2),1)],x0,y0,[],[],[],opt);
csvwrite('output.csv',output);
fid = fopen('info.txt','w');
fwrite(fid,'0');
fclose(fid);
elseif a== '2'
x0 = csvread('x.csv');
y0 = csvread('y.csv');
total_bomb = y0(1);
% for dimensional reduction
to_include = [];
x2 = [];
x2_all = [];
for k = 1:size(x0,1)
x2 = [x2;x0(k,:)];
if rank(x2)>rank(x2_all)
x2_all = x2;
to_include = [to_include,k];
end
end
x0 = x0(to_include,:);
y0 = y0(to_include);
for k = 2:size(x0,1)
for j = 2:size(x0,1)
if k~=j
ind1 = find(x0(k,:)==1);
ind2 = find(x0(j,:)==1);
if length(union(ind1,ind2)) == length(ind1) && all(union(ind1,ind2)==ind1)
x0(k,ind2)=0;
y0(k)=y0(k)-y0(j);
elseif length(union(ind1,ind2)) == length(ind2) && all(union(ind1,ind2)==ind2)
x0(j,ind1)=0;
y0(j)=y0(j)-y0(k);
end
end
end
end
if size(x0,1)>1
% we need a
% list all possibilities of x0(2:end)
[pos,ind] = get_all_pos(x0(2,:),y0(2));
for k =3:size(x0,1)
[pos_new,ind_new] = get_all_pos(x0(k,:),y0(k));
[pos,ind] = compare_two_pos(pos,ind,pos_new,ind_new);
pos_ind = unique([pos,ind],'rows');
pos = pos_ind(:,1:size(x0,2));
ind = pos_ind(:,size(x0,2)+1:end);
if size(pos,1)>max_depth
temp = randperm(size(pos,1),size(pos,1)-max_depth);
pos(temp,:)=[];
ind(temp,:)=[];
end
end
% last deal with first equation
weights = zeros(size(pos,1),1);
for k = 1:size(pos,1)
temp_ind = 1:size(x0,2);
temp_ind(ind(k,:)==1)=[];
to_delete = [];
if total_bomb-sum(pos(k,:))>0 && length(temp_ind) >= total_bomb-sum(pos(k,:))
pos(k,temp_ind) = round(total_bomb-sum(pos(k,:)))/length(temp_ind);
weights(k) = nchoosek(length(temp_ind),round(total_bomb-sum(pos(k,:))));
elseif total_bomb-sum(pos(k,:))==0
pos(k,temp_ind) = 0;
weights(k) = 1;
else
to_delete = [to_delete,k];
end
end
pos(to_delete,:)=[];
weights(to_delete) = [];
weights = weights./sum(weights);
% compute possibility of each block
pos_all = pos'*weights;
else
pos_all = total_bomb/size(x0,2)*ones(size(x0,2),1);
end
csvwrite('output.csv',pos_all);
fid = fopen('info.txt','w');
fwrite(fid,'0');
fclose(fid);
end
end