Skip to content

Commit

Permalink
Gerando uma solucao inicial gulosa mas ainda nao aleatoria
Browse files Browse the repository at this point in the history
  • Loading branch information
hbarbalho committed Oct 2, 2011
1 parent 64c10e6 commit f0333aa
Show file tree
Hide file tree
Showing 21 changed files with 225 additions and 12 deletions.
Binary file modified Default/max_cut
Binary file not shown.
3 changes: 3 additions & 0 deletions Default/src/Edge.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
src/Edge.d src/Edge.o: ../src/Edge.cpp ../src/Edge.h

../src/Edge.h:
Binary file added Default/src/Edge.o
Binary file not shown.
10 changes: 10 additions & 0 deletions Default/src/GA.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
src/GA.d src/GA.o: ../src/GA.cpp ../src/GA.h ../src/Instance.h \
../src/Edge.h ../src/Solution.h

../src/GA.h:

../src/Instance.h:

../src/Edge.h:

../src/Solution.h:
Binary file added Default/src/GA.o
Binary file not shown.
5 changes: 4 additions & 1 deletion Default/src/Instance.d
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
src/Instance.d src/Instance.o: ../src/Instance.cpp ../src/Instance.h
src/Instance.d src/Instance.o: ../src/Instance.cpp ../src/Instance.h \
../src/Edge.h

../src/Instance.h:

../src/Edge.h:
Binary file modified Default/src/Instance.o
Binary file not shown.
9 changes: 8 additions & 1 deletion Default/src/Main.d
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
src/Main.d src/Main.o: ../src/Main.cpp ../src/Instance.h
src/Main.d src/Main.o: ../src/Main.cpp ../src/Instance.h ../src/Edge.h \
../src/GA.h ../src/Solution.h

../src/Instance.h:

../src/Edge.h:

../src/GA.h:

../src/Solution.h:
Binary file modified Default/src/Main.o
Binary file not shown.
3 changes: 3 additions & 0 deletions Default/src/Solution.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
src/Solution.d src/Solution.o: ../src/Solution.cpp ../src/Solution.h

../src/Solution.h:
Binary file added Default/src/Solution.o
Binary file not shown.
15 changes: 12 additions & 3 deletions Default/src/subdir.mk
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,25 @@

# Add inputs and outputs from these tool invocations to the build variables
CPP_SRCS += \
../src/Edge.cpp \
../src/GA.cpp \
../src/Instance.cpp \
../src/Main.cpp
../src/Main.cpp \
../src/Solution.cpp

OBJS += \
./src/Edge.o \
./src/GA.o \
./src/Instance.o \
./src/Main.o
./src/Main.o \
./src/Solution.o

CPP_DEPS += \
./src/Edge.d \
./src/GA.d \
./src/Instance.d \
./src/Main.d
./src/Main.d \
./src/Solution.d


# Each subdirectory must supply rules for building sources it contributes
Expand Down
18 changes: 18 additions & 0 deletions src/Edge.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Edge.cpp
*
* Created on: Oct 2, 2011
* Author: hugo
*/

#include "Edge.h"

Edge::Edge(int _i,int _j,int _cost) {
i = _i;
j = _j;
cost = _cost;
}

Edge::~Edge() {
// TODO Auto-generated destructor stub
}
18 changes: 18 additions & 0 deletions src/Edge.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Edge.h
*
* Created on: Oct 2, 2011
* Author: hugo
*/

#ifndef EDGE_H_
#define EDGE_H_

class Edge {
public:
int i,j,cost;
Edge(int,int,int);
virtual ~Edge();
};

#endif /* EDGE_H_ */
43 changes: 43 additions & 0 deletions src/GA.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* GA.cpp
*
* Created on: Oct 2, 2011
* Author: hugo
*/

#include "GA.h"
#include <stdlib.h>
#include <stdio.h>

Solution* GA::greedySolution(){
printf("Solucao inicial\n");
int *sol = (int*)malloc(sizeof(int)*instance->numVertex());
for(int i=0;i<instance->numVertex();i++){
sol[i]=-1;
}

Edge *edge = instance->bestEdge();
for(int i=1;edge!=NULL;i++){
if(sol[edge->i]<0 && sol[edge->j]<0){
sol[edge->i] = 1;
sol[edge->j] = 0;
}else if(sol[edge->i]<0){
sol[edge->i] = !sol[edge->j];
}else{
sol[edge->j] = !sol[edge->i];
}
edge = instance->bestEdge(i);
}
Solution *solution = new Solution(sol,instance->numVertex());
return solution;
}
GA::GA(Instance *_instance) {
cout << "GA"<<endl;
instance = _instance;
Solution *initial = greedySolution();
initial->print();
}

GA::~GA() {
// TODO Auto-generated destructor stub
}
22 changes: 22 additions & 0 deletions src/GA.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* GA.h
*
* Created on: Oct 2, 2011
* Author: hugo
*/

#ifndef GA_H_
#define GA_H_
#include "Instance.h"
#include "Solution.h"

class GA {
private:
Instance *instance;
Solution* greedySolution();
public:
GA(Instance*);
virtual ~GA();
};

#endif /* GA_H_ */
37 changes: 31 additions & 6 deletions src/Instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
#include "Instance.h"
#include<fstream>
#include<stdlib.h>
#include<stdio.h>

int compare (const void * a, const void * b)
{
return ( ((Edge*)a)->cost - ((Edge*)b)->cost );
}


Instance::Instance(string arq_name) {
Instance::arq_name = arq_name;
Expand All @@ -21,6 +28,21 @@ Instance::~Instance() {
free(matriz);
}

Edge* Instance::bestEdge(int _i){
if(_i>=num_edges)return NULL;
return order[_i];
}
Edge* Instance::bestEdge(){
return bestEdge(0);
}
int Instance::numEdges(){
return num_edges;
}

int Instance::numVertex(){
return num_vertex;
}

void Instance::loadFile() {
ifstream myReadFile;
myReadFile.open(arq_name.c_str());
Expand All @@ -29,22 +51,25 @@ void Instance::loadFile() {
if (myReadFile.is_open()) {
myReadFile.getline(output, 100);
sscanf(output, "%d %d", &num_vertex, &num_edges);
matriz = (int**) malloc(sizeof(int*) * num_vertex);
order = (Edge**) malloc(sizeof(Edge*) * num_edges);
matriz = (Edge***) malloc(sizeof(Edge**) * num_vertex);
for (i = 0; i < num_vertex; i++) {
matriz[i] = (int *) malloc(sizeof(int) * num_vertex);
matriz[i] = (Edge **) malloc(sizeof(Edge*) * num_vertex);
for (j = 0; j < num_vertex; j++) {
matriz[i][j] = matriz[i][j] ^ matriz[i][j];
matriz[i][j] = NULL;
}
}

int ordePos=0;
while (!myReadFile.eof()) {
myReadFile.getline(output, 100);
sscanf(output, "%d %d %d", &i, &j, &cost);
matriz[i][j] = cost;
matriz[i][j] = new Edge(i,j,cost);
order[ordePos++] = matriz[i][j];
}
qsort (order, num_edges, sizeof(Edge), compare);
} else {
cout << "Not open" << endl;
}
printf("%d:%d\n", num_vertex,num_edges);
myReadFile.close();
cout << num_vertex << ":" << num_edges;
}
8 changes: 7 additions & 1 deletion src/Instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,23 @@

#include <string.h>
#include <iostream>
#include "Edge.h"
using namespace std;

class Instance {
public:
Edge* bestEdge();
Edge* bestEdge(int);
int numEdges();
int numVertex();
Instance(string arq_name);
virtual ~Instance();
private:
string arq_name;
int num_edges;
int num_vertex;
int** matriz;
Edge*** matriz;
Edge** order;
void loadFile();
};

Expand Down
3 changes: 3 additions & 0 deletions src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
using namespace std;

#include "Instance.h"
#include "GA.h"

int main(int argv, char *arg[]){
Instance *instance = new Instance("/home/hugo/workspace/max_cut/instances/set1/g1.rud");
cout << "Fazer GA"<<endl;
GA *ga = new GA(instance);
delete instance;
return 0;
}
23 changes: 23 additions & 0 deletions src/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Solution.cpp
*
* Created on: Oct 2, 2011
* Author: hugo
*/

#include "Solution.h"
#include <stdio.h>

void Solution::print(){
for(int i=0;i<length;i++)
printf("%d ",vertex[i]);
printf("\n");
}
Solution::Solution(int *_vertex, int _length) {
vertex = _vertex;
length = _length;
}

Solution::~Solution() {
// TODO Auto-generated destructor stub
}
20 changes: 20 additions & 0 deletions src/Solution.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Solution.h
*
* Created on: Oct 2, 2011
* Author: hugo
*/

#ifndef SOLUTION_H_
#define SOLUTION_H_

class Solution {
public:
int *vertex;
int length;
void print();
Solution(int*,int);
virtual ~Solution();
};

#endif /* SOLUTION_H_ */

0 comments on commit f0333aa

Please sign in to comment.