Clustering

78 views
Skip to first unread message

sahebe...@gmail.com

unread,
Apr 26, 2017, 6:28:47 AM4/26/17
to AMPL Modeling Language
Hi all,

I must use Kmeans clustering algorithm in part of my project. As you may know, in this algorithm, based on the number of clusters we are going to create, we make random cluster head in the network. In the first stage, we measure up the distance between the sensor points and these cluster heads so that those sensors which are located in a shorter distance of there cluster heads are placed in one cluster. In the second stage, the average of sensor points of each cluster are calculated and through this point being determined, the cluster head in each cluster is changed to the point . Now, the first and the second stage are repeated as many times as the new clusters center equals with the old clusters center, the previous stage, and this needs using a while circle.
I have done the calculations in the first round. however, I have some problems using the while circle. please help me.
WBR

param L:=90;         #the length of network
param CH:=2;        #the number of cluster
param S:=15;        #the number of senser

set VCH:= {1..CH};       #set of cluster heads
param X_CH { VCH }:=( L*Uniform01());
param Y_CH { VCH }:=( L*Uniform01());

set VS:= {1..S};          #set of sensor nodes
param X_S { VS }:=( L*Uniform01());
param Y_S { VS }:=( L*Uniform01()); 

param distance {i in VCH,j in VS}:=sqrt((X_CH[i]-X_S[j])^2+(Y_CH[i]-Y_S[j])^2);  #distance between the sensor nodes and the cluster heads

set minn {i in VCH}={j in VS : distance[i,j] = min{ii in VCH} distance [ii,j]};     #the first stage

param avg1:= (sum {i in minn[1]} X_S[i] + sum {i in minn[1]} Y_S[i]) / card(minn[1]);     #the second stage
param avg2:= (sum {i in minn[2]} X_S[i] + sum {i in minn[2]} Y_S[i]) / card(minn[2]);     #the second stage



Robert Fourer

unread,
Apr 27, 2017, 9:46:56 AM4/27/17
to am...@googlegroups.com
The AMPL statements that you show do not have any obvious errors. Can you be more specific about the problems you are having? It would help to give an example of the AMPL statements you tried to write, and an explanation of why they did not work. Also give the complete text of any error messages, and data if needed to run the example.

Bob Fourer
am...@googlegroups.com

=======

From: am...@googlegroups.com [mailto:am...@googlegroups.com] On Behalf Of sahebe...@gmail.com
Sent: Wednesday, April 26, 2017 5:29 AM
To: AMPL Modeling Language
Subject: [AMPL 13932] Clustering

I must use Kmeans clustering algorithm in part of my project. As you may know, in this algorithm, based on the number of clusters we are going to create, we make random cluster head in the network. In the first stage, we measure up the distance between the sensor points and these cluster heads so that those sensors which are located in a shorter distance of there cluster heads are placed in one cluster. In the second stage, the average of sensor points of each cluster are calculated and through this point being determined, the cluster head in each cluster is changed to the point . Now, the first and the second stage are repeated as many times as the new clusters center equals with the old clusters center, the previous stage, and this needs using a while circle.

I have done the calculations in the first round. however, I have some problems using the while circle. please help me.

sahebe...@gmail.com

unread,
Apr 30, 2017, 7:06:22 AM4/30/17
to AMPL Modeling Language
Hi Bob,
I do not have any problems in the first and second stage that I sent to you but the kmeans clustering that I explained to you needs to a while circle. I do not know how write the while circle and I want you help me in this section. I really need your help
Thanks for your previous helps
WBR

Robert Fourer

unread,
Apr 30, 2017, 4:47:04 PM4/30/17
to am...@googlegroups.com
There is no "while" statement in AMPL for circling (looping) through a series of commands. Instead this functionality is provided by the AMPL "repeat" statement. Complete instructions for writing "repeat" loops are given in Chapter 13 of the AMPL book (http://ampl.com/BOOK/CHAPTERS/16-script.pdf) starting on page 262.

Bob Fourer
am...@googlegroups.com


-----Original Message-----
From: am...@googlegroups.com [mailto:am...@googlegroups.com] On Behalf Of sahebe...@gmail.com
Sent: Sunday, April 30, 2017 6:06 AM
To: AMPL Modeling Language

sahebe...@gmail.com

unread,
May 11, 2017, 10:36:13 AM5/11/17
to AMPL Modeling Language, 4...@ampl.com
Hi again Bob;
I could not solve this problem. I need your help. I studied the file that you sent about while looping and considering that I write this code but I have problem yet.
I want param AVG= avg in i level - avg in i-1 level. For example if i=8, AVG=avg in level 8- avg in level 7. I do not know how write this code in n and n-1 !!
I hope you can help me.
WBR


param L:=90;         #the length of network
param CH:=2;        #the number of clusters
param S:=15;        #the number of sensors


set VCH:= {1..CH};       #set of cluster heads
param X_CH { VCH }:=( L*Uniform01());
param Y_CH { VCH }:=( L*Uniform01());

set VS:= {1..S};          #set of sensor nodes
param X_S { VS }:=( L*Uniform01());
param Y_S { VS }:=( L*Uniform01()); 

param distance {i in VCH,j in VS}:=sqrt((X_CH[i]-X_S[j])^2+(Y_CH[i]-Y_S[j])^2);  #distance between the sensor nodes and the cluster heads

set minn{i in VS} default {};          #the first stage

param avg{i in VS} default 0;        #calculate of sensors average

param AVG;                               ##### I have problem in this part, I have written it wrongly#####
for {i in VS}{ let AVG:= avg[i]-(avg[i]-1);}

for {i in VS}{
repeat {
let minn[i]:= {j in VCH : distance[i,j] = min{ii in VS} distance [ii,j]};
let avg[i]:= (sum {j in minn[i]} X_CH[j] + sum {j in minn[i]} Y_CH[j]) / card(minn[i]);
if (AVG > 0.01) then break;
}
}
display avg,minn;

Robert Fourer

unread,
May 12, 2017, 12:14:46 PM5/12/17
to am...@googlegroups.com
You should write "let AVG := avg[i] - avg[i-1];" inside your loop. Also you will need to define "param avg {0..S}" and then initialize avg[0], so that avg[i-1] is defined when i equals 1.

Bob Fourer
am...@googlegroups.com

=======

From: am...@googlegroups.com [mailto:am...@googlegroups.com] On Behalf Of sahebe...@gmail.com
Sent: Thursday, May 11, 2017 9:36 AM
To: AMPL Modeling Language
Cc: 4...@ampl.com
Subject: Re: [AMPL 14017] Clustering

I could not solve this problem. I need your help. I studied the file that you sent about while looping and considering that I write this code but I have problem yet.
I want param AVG= avg in i level - avg in i-1 level. For example if i=8, AVG=avg in level 8- avg in level 7. I do not know how write this code in n and n-1 !!

Message has been deleted

sahebe...@gmail.com

unread,
May 13, 2017, 11:49:46 AM5/13/17
to AMPL Modeling Language
Thanks for your help.
Do you have any kmeans clustering code?

AMPL Support

unread,
May 15, 2017, 7:52:42 AM5/15/17
to am...@googlegroups.com
A search on "k-means clustering AMPL" finds an AMPL model and sample data in this blog:

http://data-mining-notebook.blogspot.com/2014/02/k-means-clustering-using-ampl.html

AMPL Support Services
sup...@ampl.com


-----Original Message-----
From: am...@googlegroups.com [mailto:am...@googlegroups.com] On Behalf Of sahebe...@gmail.com
Sent: Saturday, May 13, 2017 6:38 AM
To: AMPL Modeling Language
Subject: RE: [AMPL 14026] Clustering

Thanks for your help. Do you any kmeans clustering code in AMPL?


Reply all
Reply to author
Forward
0 new messages