DWM 10
DWM 10
Dataset:
Dataset Generator:
import numpy as np
import random
def generate_chess_board(axis_length, n, size):
data = []
step = axis_length / n
if n % 2 == 0:
num_points = size / (n**2/2)
else:
num_points = size / ((n**2)/2 + 1)
for i in xrange(n):
for j in xrange(n):
if i % 2 == j % 2:
for k in xrange(num_points):
data.append([random.randint(i*step,(i+1)*step),random.randint(j*step,(j+1)*step)])
return np.array(data).tolist()
data=generate_chess_board(10,2,100)
file = open("dataset.txt","w")
for item in data:
file.write("%s,\n" % item)
file.close()
Code:
import math
import random
import numpy as np
import sys
import ast
"""
1. Input parameters numlocal and maxneighbor. Initialize i to 1, and mincost to a large number.
2. Set current to an arbitrary node in G_{n,k}
3. Set j to 1.
4. Consider a random neighbor S of current, and based on Equation (5) calculate the cost differential
of the two nodes.
5. If S has a lower cost, set current to S, and go to Step (3).
6. Otherwise, increment j by 1. If j<=maxneighbor,go to Step (4).
7. Otherwise, when j > maxneighbor, compare the cost of current with mincost. If the former is less than
mincost,
set mincost to the cost of current, and set bestnode to current.
8. Increment i by 1. If i > numlocal, output bestnode and halt. Otherwise, go to Step (2).
"""
def clarans_basic(points, numlocal, maxneighbor, mincost,k):
# random.seed(1)
# np.random.seed(1)
i=1
N = len(points)
d_mat = np.asmatrix(np.empty((k,N)))
local_best = []
bestnode = []
while i<=numlocal:
#Step 2 - pick k random medoids from data points - medoids_nr from points
node = np.random.permutation(range(N))[:k]
fill_distances(d_mat, points, node)
cls = assign_to_closest(points, node, d_mat)
cost = total_dist(d_mat, cls)
copy_node = node.copy()
print ' \n New start '
#increase neighbor count
j=1
while j<=maxneighbor:
#Step 4 - pick a random neighbor of current node - i.e change randomly one medoid
#calculate the cost differential of the initial node and the random neighbor
changing_node = copy_node.copy()
idx = pick_random_neighbor(copy_node, N)
update_distances(d_mat, points, copy_node, idx)
cls = assign_to_closest(points, copy_node, d_mat)
new_cost = total_dist(d_mat, cls)
#check if new cost is smaller
if new_cost < cost:
cost = new_cost
local_best = copy_node.copy()
print 'Best cost: ' + str(cost) + ' '
print local_best
j=1
continue
else:
#copy_node = changing_node
j=j+1
if j<=maxneighbor:
continue
elif j>maxneighbor:
if mincost>cost:
mincost = cost
print "change bestnode "
print bestnode
print " into"
bestnode = local_best.copy()
print bestnode
i = i+1
if i>numlocal:
fill_distances(d_mat, points, bestnode)
cls = assign_to_closest(points, bestnode, d_mat)
print "Final cost: " + str(mincost) + ' '
print bestnode
return cls, bestnode
else:
break
dataset=[]
file=open("dataset.txt","r")
for line in file:
lin=line[:-2]
linlist=ast.literal_eval(lin)
dataset.append(linlist)
cls,m=clarans_basic(dataset, 5, 10, 250,8)
Output: