Skip to content

Commit

Permalink
latest, dunno changes
Browse files Browse the repository at this point in the history
  • Loading branch information
apoorvumang committed Sep 7, 2014
1 parent d29d21a commit abbf9da
Show file tree
Hide file tree
Showing 8 changed files with 305 additions and 12 deletions.
11 changes: 8 additions & 3 deletions epuck/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#############################################################################
# Makefile for building: epuck
# Generated by qmake (2.01a) (Qt 4.8.6) on: Mon Sep 1 07:31:23 2014
# Generated by qmake (2.01a) (Qt 4.8.6) on: Mon Sep 1 20:18:49 2014
# Project: epuck.pro
# Template: app
# Command: /usr/bin/qmake-qt4 -spec /usr/share/qt4/mkspecs/linux-g++ -o Makefile epuck.pro
Expand Down Expand Up @@ -50,7 +50,8 @@ SOURCES = main.cpp \
camcapture.cpp \
featuredetection.cpp \
beliefstate.cpp \
algoworker.cpp moc_mainwindow.cpp \
algoworker.cpp \
hungarian.cpp moc_mainwindow.cpp \
moc_cameraworker.cpp \
moc_algoworker.cpp
OBJECTS = main.o \
Expand All @@ -61,6 +62,7 @@ OBJECTS = main.o \
featuredetection.o \
beliefstate.o \
algoworker.o \
hungarian.o \
moc_mainwindow.o \
moc_cameraworker.o \
moc_algoworker.o
Expand Down Expand Up @@ -178,7 +180,7 @@ qmake: FORCE

dist:
@$(CHK_DIR_EXISTS) .tmp/epuck1.0.0 || $(MKDIR) .tmp/epuck1.0.0
$(COPY_FILE) --parents $(SOURCES) $(DIST) .tmp/epuck1.0.0/ && $(COPY_FILE) --parents mainwindow.h cameraworker.h serial.h imgproc.h camcapture.h commondefs.h defines.h featuredetection.h beliefstate.h algoworker.h .tmp/epuck1.0.0/ && $(COPY_FILE) --parents main.cpp mainwindow.cpp cameraworker.cpp serial.cpp camcapture.cpp featuredetection.cpp beliefstate.cpp algoworker.cpp .tmp/epuck1.0.0/ && $(COPY_FILE) --parents mainwindow.ui .tmp/epuck1.0.0/ && (cd `dirname .tmp/epuck1.0.0` && $(TAR) epuck1.0.0.tar epuck1.0.0 && $(COMPRESS) epuck1.0.0.tar) && $(MOVE) `dirname .tmp/epuck1.0.0`/epuck1.0.0.tar.gz . && $(DEL_FILE) -r .tmp/epuck1.0.0
$(COPY_FILE) --parents $(SOURCES) $(DIST) .tmp/epuck1.0.0/ && $(COPY_FILE) --parents mainwindow.h cameraworker.h serial.h imgproc.h camcapture.h commondefs.h defines.h featuredetection.h beliefstate.h algoworker.h hungarian.h .tmp/epuck1.0.0/ && $(COPY_FILE) --parents main.cpp mainwindow.cpp cameraworker.cpp serial.cpp camcapture.cpp featuredetection.cpp beliefstate.cpp algoworker.cpp hungarian.cpp .tmp/epuck1.0.0/ && $(COPY_FILE) --parents mainwindow.ui .tmp/epuck1.0.0/ && (cd `dirname .tmp/epuck1.0.0` && $(TAR) epuck1.0.0.tar epuck1.0.0 && $(COMPRESS) epuck1.0.0.tar) && $(MOVE) `dirname .tmp/epuck1.0.0`/epuck1.0.0.tar.gz . && $(DEL_FILE) -r .tmp/epuck1.0.0


clean:compiler_clean
Expand Down Expand Up @@ -321,6 +323,9 @@ algoworker.o: algoworker.cpp algoworker.h \
beliefstate.h
$(CXX) -c $(CXXFLAGS) $(INCPATH) -o algoworker.o algoworker.cpp

hungarian.o: hungarian.cpp hungarian.h
$(CXX) -c $(CXXFLAGS) $(INCPATH) -o hungarian.o hungarian.cpp

moc_mainwindow.o: moc_mainwindow.cpp
$(CXX) -c $(CXXFLAGS) $(INCPATH) -o moc_mainwindow.o moc_mainwindow.cpp

Expand Down
78 changes: 74 additions & 4 deletions epuck/algoworker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ double AlgoWorker::getDistance(CvPoint a, CvPoint b)

void AlgoWorker::process()
{
h.n = 3;
int mat[3][3] = {
{3, 3, 3},
{3, 2, 3},
{3, 2, 3}
};
for(int i = 0; i < 3; ++i)
{
for(int j = 0; j < 3; ++j)
{
h.cost[i][j] = mat[i][j];
}
}
qDebug() << "Answer is " << h.hungarian();
for(int i = 0; i < 3; ++i)
{
qDebug() << h.xy[i];
}
// allocate resources using new here
QTime now = QTime::currentTime();
qsrand(now.msec());
Expand Down Expand Up @@ -920,7 +938,10 @@ void AlgoWorker::onTimeout()
emit printDestination(temp);
// sleep(10);
// usleep(2000000);
currentState = MOVE_TO_CIRCLE;
if(currentAlgo == LYNDON)
currentState = CALCULATE_POINTS_HUNGARIAN;
else
currentState = MOVE_TO_CIRCLE;
for(int i = 0; i<NUMBOTS; ++i)
{
turnLedOff(i);
Expand Down Expand Up @@ -1093,7 +1114,14 @@ void AlgoWorker::onTimeout()
}
}
}
if(count == numBotMoving)
if(currentAlgo == LYNDON)
{
if(count == NUMBOTS)
{
currentState = FINISHED;
}
}
else if(count == numBotMoving)
{
// qDebug() << "inside";
robotActive++;
Expand All @@ -1105,6 +1133,48 @@ void AlgoWorker::onTimeout()
}
break;

case CALCULATE_POINTS_HUNGARIAN:
{
h.n = NUMBOTS;
//calculate the destination points
//assume leader is destinationPoint[0]
const double stableAngle = 2.0*CV_PI/((double)NUMBOTS);
CvPoint myPoint[5];
for(int i = 0; i < NUMBOTS; ++i)
{
myPoint[i] = angleToPoint(pointToAngle(destinationPoint[0]) + stableAngle*i);
}

PointList temp;
for(int i = 0; i < NUMBOTS; ++i)
{
temp.p[i] = myPoint[i];
}
// usleep(2000000);
emit printDestination(temp);

//fill cost matrix
for(int i = 0; i < NUMBOTS; ++i)
{
for(int j = 0; j < NUMBOTS; ++j)
{
h.cost[i][j] = getDistance(cvPoint(localBS.bot[i].x, localBS.bot[i].y), myPoint[j]);
}
}
totDistance = h.hungarian();
for(int i = 0; i < NUMBOTS; ++i)
{
destinationPoint[i] = myPoint[h.xy[i]];
isBotMoving[i] = true;
}
currentState = POSITIONING_ON_CIRCLE_2;
}

break;

case MOVE_TO_POINTS:

break;
case FINISHED:
// qDebug() << "Finished";
qDebug() << "Number of activations: " << numActivations;
Expand All @@ -1113,7 +1183,7 @@ void AlgoWorker::onTimeout()
sleep(1);
moveStopAll();
currentState = MOVE_TO_SAVED_POSITION;
currentAlgo = (Algorithm) ((int)currentAlgo + 1);
currentAlgo = (Algorithm) (((int)currentAlgo + 1)%((int)END_ALGO));
break;

}
Expand All @@ -1130,7 +1200,7 @@ void AlgoWorker::onTimeout()
// usleep(100000);
// qDebug()<< "starting next iteration";
timer->setSingleShot(true);
timer->start(10);
timer->start(100);
// nextIteration();
}

Expand Down
6 changes: 4 additions & 2 deletions epuck/algoworker.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "camcapture.h"
#include "featuredetection.h"
#include "commondefs.h"
#include "hungarian.h"


//non simulation values
Expand All @@ -27,8 +28,8 @@
#define DISTANCE_THRESHOLD_FINE 8.0
#define FINAL_REACHED_THRESHOLD 18.0

enum AlgoState {SAVE_CURRENT_POSITION, MOVE_TO_SAVED_POSITION, MAKE_CIRCLE, MOVE_TO_CIRCLE, POSITIONING_ON_CIRCLE_1, POSITIONING_ON_CIRCLE_2, FINISHED};
enum Algorithm {MIDPOINT, NEW_ALGO};
enum AlgoState {SAVE_CURRENT_POSITION, MOVE_TO_SAVED_POSITION, MAKE_CIRCLE, MOVE_TO_CIRCLE, POSITIONING_ON_CIRCLE_1, POSITIONING_ON_CIRCLE_2, FINISHED, CALCULATE_POINTS_HUNGARIAN, MOVE_TO_POINTS};
enum Algorithm {MIDPOINT, NEW_ALGO, LYNDON, END_ALGO};
enum ActivationAlgorithm {PROBABILISTIC_0, PROBABILISTIC_1, PROBABILISTIC_HALF, DINING_PHILOSOPHER};
enum MoveDirection {FORWARD, BACKWARD, LEFT, RIGHT, STOP};
class AlgoWorker : public QObject
Expand Down Expand Up @@ -108,6 +109,7 @@ public slots:
bool isOneStableCW(int n);
CvPoint getClosest(CvPoint p1, CvPoint p2, int n);
void getNeighbourIndicesCircle(int n, int* indexList);
Hungarian h;
};

#endif // ALGOWORKER_H
Binary file modified epuck/epuck
Binary file not shown.
6 changes: 4 additions & 2 deletions epuck/epuck.pro
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ SOURCES += main.cpp\
camcapture.cpp \
featuredetection.cpp \
beliefstate.cpp \
algoworker.cpp
algoworker.cpp \
hungarian.cpp

HEADERS += mainwindow.h \
cameraworker.h \
Expand All @@ -33,7 +34,8 @@ HEADERS += mainwindow.h \
defines.h \
featuredetection.h \
beliefstate.h \
algoworker.h
algoworker.h \
hungarian.h

FORMS += mainwindow.ui

2 changes: 1 addition & 1 deletion epuck/epuck.pro.user
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 3.0.1, 2014-09-01T19:32:42. -->
<!-- Written by QtCreator 3.0.1, 2014-09-01T23:35:22. -->
<qtcreator>
<data>
<variable>ProjectExplorer.Project.ActiveTarget</variable>
Expand Down
163 changes: 163 additions & 0 deletions epuck/hungarian.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
#include "hungarian.h"
using namespace std;

void Hungarian::update_labels()
{
int x, y, delta = INF; //init delta as infinity
for (y = 0; y < n; y++) //calculate delta using slack
if (!T[y])
delta = min(delta, slack[y]);
for (x = 0; x < n; x++) //update X labels
if (S[x]) lx[x] -= delta;
for (y = 0; y < n; y++) //update Y labels
if (T[y]) ly[y] += delta;
for (y = 0; y < n; y++) //update slack array
if (!T[y])
slack[y] -= delta;
}
void Hungarian::add_to_tree(int x, int prevx)
//x - current vertex,prevx - vertex from X before x in the alternating path,
//so we add edges (prevx, xy[x]), (xy[x], x)
{
S[x] = true; //add x to S
prev[x] = prevx; //we need this when augmenting
for (int y = 0; y < n; y++) //update slacks, because we add new vertex to S
if (lx[x] + ly[y] - cost[x][y] < slack[y])
{
slack[y] = lx[x] + ly[y] - cost[x][y];
slackx[y] = x;
}
}
void Hungarian::init_labels()
{
memset(lx, 0, sizeof(lx));
memset(ly, 0, sizeof(ly));
for (int x = 0; x < n; x++)
for (int y = 0; y < n; y++)
lx[x] = max(lx[x], cost[x][y]);
}

void Hungarian::augment() //main function of the algorithm
{
if (max_match == n) return; //check wether matching is already perfect
int x, y, root; //just counters and root vertex
int q[N_HUNGARIAN], wr = 0, rd = 0; //q - queue for bfs, wr,rd - write and read
//pos in queue
memset(S, false, sizeof(S)); //init set S
memset(T, false, sizeof(T)); //init set T
memset(prev, -1, sizeof(prev)); //init set prev - for the alternating tree
for (x = 0; x < n; x++) //finding root of the tree
if (xy[x] == -1)
{
q[wr++] = root = x;
prev[x] = -2;
S[x] = true;
break;
}

for (y = 0; y < n; y++) //initializing slack array
{
slack[y] = lx[root] + ly[y] - cost[root][y];
slackx[y] = root;
}
while (true) //main cycle
{
while (rd < wr) //building tree with bfs cycle
{
x = q[rd++]; //current vertex from X part
for (y = 0; y < n; y++) //iterate through all edges in equality graph
if (cost[x][y] == lx[x] + ly[y] && !T[y])
{
if (yx[y] == -1) break; //an exposed vertex in Y found, so
//augmenting path exists!
T[y] = true; //else just add y to T,
q[wr++] = yx[y]; //add vertex yx[y], which is matched
//with y, to the queue
add_to_tree(yx[y], x); //add edges (x,y) and (y,yx[y]) to the tree
}
if (y < n) break; //augmenting path found!
}
if (y < n) break; //augmenting path found!

update_labels(); //augmenting path not found, so improve labeling
wr = rd = 0;
for (y = 0; y < n; y++)
//in this cycle we add edges that were added to the equality graph as a
//result of improving the labeling, we add edge (slackx[y], y) to the tree if
//and only if !T[y] && slack[y] == 0, also with this edge we add another one
//(y, yx[y]) or augment the matching, if y was exposed
if (!T[y] && slack[y] == 0)
{
if (yx[y] == -1) //exposed vertex in Y found - augmenting path exists!
{
x = slackx[y];
break;
}
else
{
T[y] = true; //else just add y to T,
if (!S[yx[y]])
{
q[wr++] = yx[y]; //add vertex yx[y], which is matched with
//y, to the queue
add_to_tree(yx[y], slackx[y]); //and add edges (x,y) and (y,
//yx[y]) to the tree
}
}
}
if (y < n) break; //augmenting path found!
}

if (y < n) //we found augmenting path!
{
max_match++; //increment matching
//in this cycle we inverse edges along augmenting path
for (int cx = x, cy = y, ty; cx != -2; cx = prev[cx], cy = ty)
{
ty = xy[cx];
yx[cy] = cx;
xy[cx] = cy;
}
augment(); //recall function, go to step 1 of the algorithm
}
}//end of augment() function

int Hungarian::hungarian()
{
//fix cost matrix
for(int i = 0; i < n; ++i)
{
for(int j = 0; j < n; ++j)
{
cost[i][j] = 10000 - cost[i][j];
}
}
int ret = 0; //weight of the optimal matching
max_match = 0; //number of vertices in current matching
memset(xy, -1, sizeof(xy));
memset(yx, -1, sizeof(yx));
init_labels(); //step 0
augment(); //steps 1-3

for(int i = 0; i < n; ++i)
{
for(int j = 0; j < n; ++j)
{
cost[i][j] = 10000 - cost[i][j];
}
}
for (int x = 0; x < n; x++) //forming answer there
ret += cost[x][xy[x]];
return ret;
}

//main(void)
//{

// /* Fill up the cost matrix cost(i,j) cost of making ith worker do jth job */

// /* call Hungarian() */

// /* To know job assigned to ith worker , read xy[i] */

//}
Loading

0 comments on commit abbf9da

Please sign in to comment.