Skip to content

Commit

Permalink
Graph format has been developed, Graph API created, examples and test…
Browse files Browse the repository at this point in the history
…s have been updated
  • Loading branch information
U-DeBug010-PC\DeBug010 authored and U-DeBug010-PC\DeBug010 committed Apr 28, 2014
1 parent 1869ed9 commit f923168
Show file tree
Hide file tree
Showing 33 changed files with 410 additions and 138 deletions.
22 changes: 12 additions & 10 deletions gcolor/dsatur/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from support import read_sample;

from samples.definitions import GRAPH_SIMPLE_SAMPLES;


def dsatur(data):
"DSATUR algorithm implementation. Performs graph coloring problem."

"(in) data - matrix graph representation"

"Returns vector with assigned colors where each element corresponds to node in the graph"
if (len(data[0]) != len(data)):
raise NameError('Only matrix graph representation is available.');

Expand Down Expand Up @@ -101,8 +101,10 @@ def get_neighbors(node_index, data):
return [ index for index in range(len(data[node_index])) if data[node_index][index] != 0 ];


# graph_matrix_repr = read_sample(GRAPH_SIMPLE_SAMPLES.GRAPH_FIVE_POINTED_STAR);
# coloring = dsatur(graph_matrix_repr);
# print(graph_matrix_repr);
# print(coloring);

# from support import draw_graph, read_graph;
#
# graph = read_graph(GRAPH_SIMPLE_SAMPLES.GRAPH_FULL1);
# coloring = dsatur(graph.data);
# draw_graph(graph, coloring);
# print(graph);
# print(coloring);
35 changes: 17 additions & 18 deletions gcolor/dsatur/examples.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
from support import read_sample;
from support import read_graph, draw_graph;

from samples.definitions import GRAPH_SIMPLE_SAMPLES;

from gcolor.dsatur import dsatur;

def template_graph_coloring(filename):
graph_matrix_repr = read_sample(filename);
coloring = dsatur(graph_matrix_repr);
print("\nGraph: ", filename);
print(coloring);
graph = read_graph(filename);
coloring = dsatur(graph.data);

draw_graph(graph, coloring);

def graph_one_line():
template_graph_coloring(GRAPH_SIMPLE_SAMPLES.GRAPH_ONE_LINE);

def graph_full_1():
def run_all_graph_samples():
template_graph_coloring(GRAPH_SIMPLE_SAMPLES.GRAPH_BROKEN_CIRCLE1);
template_graph_coloring(GRAPH_SIMPLE_SAMPLES.GRAPH_BROKEN_CIRCLE2);
template_graph_coloring(GRAPH_SIMPLE_SAMPLES.GRAPH_FIVE_POINTED_FRAME_STAR);
template_graph_coloring(GRAPH_SIMPLE_SAMPLES.GRAPH_FIVE_POINTED_STAR);
template_graph_coloring(GRAPH_SIMPLE_SAMPLES.GRAPH_FULL1);

def graph_full_2():
template_graph_coloring(GRAPH_SIMPLE_SAMPLES.GRAPH_FULL2);

def graph_one_circle():
template_graph_coloring(GRAPH_SIMPLE_SAMPLES.GRAPH_ONE_CIRCLE1);


graph_one_line();
graph_full_1();
graph_full_2();
graph_one_circle();
template_graph_coloring(GRAPH_SIMPLE_SAMPLES.GRAPH_ONE_CIRCLE2);
template_graph_coloring(GRAPH_SIMPLE_SAMPLES.GRAPH_ONE_CIRCLE3);
template_graph_coloring(GRAPH_SIMPLE_SAMPLES.GRAPH_ONE_CROSSROAD);
template_graph_coloring(GRAPH_SIMPLE_SAMPLES.GRAPH_ONE_LINE);
template_graph_coloring(GRAPH_SIMPLE_SAMPLES.GRAPH_SIMPLE1);
template_graph_coloring(GRAPH_SIMPLE_SAMPLES.GRAPH_TWO_CROSSROADS);

run_all_graph_samples();
10 changes: 5 additions & 5 deletions gcolor/dsatur/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

from gcolor.dsatur import dsatur;

from support import read_sample;
from support import read_sample, read_graph;

from samples.definitions import GRAPH_SIMPLE_SAMPLES;

class Test(unittest.TestCase):
def templateTestColoring(self, filename):
graph_matrix_repr = read_sample(filename);
map_coloring = dsatur(graph_matrix_repr);
graph = read_graph(filename);
map_coloring = dsatur(graph.data);

# Check number of colors
assigned_colors = set(map_coloring);
Expand All @@ -19,8 +19,8 @@ def templateTestColoring(self, filename):
assert color_number in assigned_colors;

# Check validity of colors
for index_node in range(len(graph_matrix_repr)):
color_neighbors = [ map_coloring[index] for index in range(len(graph_matrix_repr[index_node])) if graph_matrix_repr[index_node][index] != 0 and index_node != index];
for index_node in range(len(graph.data)):
color_neighbors = [ map_coloring[index] for index in range(len(graph.data[index_node])) if graph.data[index_node][index] != 0 and index_node != index];
#print(index_node, map_coloring[index_node], color_neighbors, assigned_colors, map_coloring, "\n\n");
assert map_coloring[index_node] not in color_neighbors;

Expand Down
6 changes: 3 additions & 3 deletions gcolor/histeresis/examples.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from gcolor.histeresis import hysteresisgcolor;

from support import read_sample, draw_dynamics;
from support import draw_dynamics, read_graph;
from samples.definitions import GRAPH_SIMPLE_SAMPLES;

def template_graph_coloring(filename, alpha, eps, steps, time, title = None):
if (title is None): title = filename;

graph_matrix_repr = read_sample(filename);
network = hysteresisgcolor(graph_matrix_repr, alpha, eps);
graph = read_graph(filename);
network = hysteresisgcolor(graph.data, alpha, eps);

(t, dyn) = network.simulate(steps, time);
draw_dynamics(t, dyn, x_title = "Time", y_title = "State");
Expand Down
8 changes: 4 additions & 4 deletions gcolor/sync/examples.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from gcolor.syncgcolor import syncgcolor;
from gcolor.sync import syncgcolor;

from nnet.sync import solve_type;

from support import read_sample;
from support import draw_dynamics;
from support import read_graph;

from samples.definitions import GRAPH_SIMPLE_SAMPLES;

Expand All @@ -12,8 +12,8 @@ def template_graph_coloring(positive_weight, negative_weight, filename, reductio
if (title is None): title = filename;
print("\nGraph Coloring: ", title);

graph_matrix_repr = read_sample(filename);
network = syncgcolor(graph_matrix_repr, positive_weight, negative_weight, reduction);
graph = read_graph(filename);
network = syncgcolor(graph.data, positive_weight, negative_weight, reduction);

(t, dyn) = network.process(order = 0.999, solution = solve_type.FAST, collect_dynamic = True);
draw_dynamics(t, dyn, x_title = "Time", y_title = "Phase", y_lim = [0, 2 * 3.14]);
Expand Down
26 changes: 13 additions & 13 deletions samples/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ class FCPS_SAMPLES:
SAMPLE_WING_NUT = samples.__path__[0] + "\\SampleWingNut.txt";

class GRAPH_SIMPLE_SAMPLES:
GRAPH_BROKEN_CIRCLE1 = samples.__path__[0] + "\\graphs\\GraphBrokenCircle1.txt";
GRAPH_BROKEN_CIRCLE2 = samples.__path__[0] + "\\graphs\\GraphBrokenCircle2.txt";
GRAPH_FIVE_POINTED_FRAME_STAR = samples.__path__[0] + "\\graphs\\GraphFivePointedFrameStar.txt";
GRAPH_FIVE_POINTED_STAR = samples.__path__[0] + "\\graphs\\GraphFivePointedStar.txt";
GRAPH_ONE_CIRCLE1 = samples.__path__[0] + "\\graphs\\GraphOneCircle1.txt";
GRAPH_ONE_CIRCLE2 = samples.__path__[0] + "\\graphs\\GraphOneCircle2.txt";
GRAPH_ONE_CIRCLE3 = samples.__path__[0] + "\\graphs\\GraphOneCircle3.txt";
GRAPH_ONE_CROSSROAD = samples.__path__[0] + "\\graphs\\GraphOneCrossroad.txt";
GRAPH_ONE_LINE = samples.__path__[0] + "\\graphs\\GraphOneLine.txt";
GRAPH_TWO_CROSSROADS = samples.__path__[0] + "\\graphs\\GraphTwoCrossroads.txt";
GRAPH_FULL1 = samples.__path__[0] + "\\graphs\\GraphFull1.txt"
GRAPH_FULL2 = samples.__path__[0] + "\\graphs\\GraphFull2.txt"
GRAPH_SIMPLE1 = samples.__path__[0] + "\\graphs\\GraphSimple1.txt"
GRAPH_BROKEN_CIRCLE1 = samples.__path__[0] + "\\graphs\\GraphBrokenCircle1.grpr";
GRAPH_BROKEN_CIRCLE2 = samples.__path__[0] + "\\graphs\\GraphBrokenCircle2.grpr";
GRAPH_FIVE_POINTED_FRAME_STAR = samples.__path__[0] + "\\graphs\\GraphFivePointedFrameStar.grpr";
GRAPH_FIVE_POINTED_STAR = samples.__path__[0] + "\\graphs\\GraphFivePointedStar.grpr";
GRAPH_ONE_CIRCLE1 = samples.__path__[0] + "\\graphs\\GraphOneCircle1.grpr";
GRAPH_ONE_CIRCLE2 = samples.__path__[0] + "\\graphs\\GraphOneCircle2.grpr";
GRAPH_ONE_CIRCLE3 = samples.__path__[0] + "\\graphs\\GraphOneCircle3.grpr";
GRAPH_ONE_CROSSROAD = samples.__path__[0] + "\\graphs\\GraphOneCrossroad.grpr";
GRAPH_ONE_LINE = samples.__path__[0] + "\\graphs\\GraphOneLine.grpr";
GRAPH_TWO_CROSSROADS = samples.__path__[0] + "\\graphs\\GraphTwoCrossroads.grpr";
GRAPH_FULL1 = samples.__path__[0] + "\\graphs\\GraphFull1.grpr"
GRAPH_FULL2 = samples.__path__[0] + "\\graphs\\GraphFull2.grpr"
GRAPH_SIMPLE1 = samples.__path__[0] + "\\graphs\\GraphSimple1.grpr"

class IMAGE_SIMPLE_SAMPLES:
IMAGE_THREE_OBJECT1 = samples.__path__[0] + "\\images\\ImageSimple1.png";
Expand Down
16 changes: 16 additions & 0 deletions samples/graphs/GraphBrokenCircle1.grpr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
c DESCRIPTION: 6 Nodes, 7 Edges
c OPTIMAL NUMBER OF COLORS: 2

m 0 1 0 0 0 1
m 1 0 1 0 0 0
m 0 1 0 1 0 1
m 0 0 1 0 1 0
m 0 0 0 1 0 1
m 1 0 1 0 1 0

r 0 1
r 1 2
r 2 2
r 3 1
r 2 0
r 1 0
6 changes: 0 additions & 6 deletions samples/graphs/GraphBrokenCircle1.txt

This file was deleted.

14 changes: 14 additions & 0 deletions samples/graphs/GraphBrokenCircle2.grpr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
c DESCRIPTION: 5 Nodes, 6 Edges
c OPTIMAL NUMBER OF COLORS: 3

m 0 1 0 0 1
m 1 0 1 0 0
m 0 1 0 1 1
m 0 0 1 0 1
m 1 0 1 1 0

r 0 1
r 1 2
r 3 2
r 4 1
r 2 0
5 changes: 0 additions & 5 deletions samples/graphs/GraphBrokenCircle2.txt

This file was deleted.

24 changes: 24 additions & 0 deletions samples/graphs/GraphFivePointedFrameStar.grpr
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
c DESCRIPTION: 10 Nodes, 10 Edges
c OPTIMAL NUMBER OF COLORS: 3

m 0 1 1 0 0 1 0 0 0 0
m 1 0 0 0 0 0 1 1 0 0
m 1 0 0 1 0 0 0 0 1 0
m 0 0 1 0 1 0 0 1 0 0
m 0 0 0 1 0 1 1 0 0 0
m 1 0 0 0 1 0 0 0 0 1
m 0 1 0 0 1 0 0 0 1 0
m 0 1 0 1 0 0 0 0 0 1
m 0 0 1 0 0 0 1 0 0 1
m 0 0 0 0 0 1 0 1 1 0

r 2 3
r 2 2.6
r 0 2.4
r 0.5 2
r 3 2
r 4 2.4
r 1 1
r 2.5 1
r 0.5 0.5
r 3.5 0.5
10 changes: 0 additions & 10 deletions samples/graphs/GraphFivePointedFrameStar.txt

This file was deleted.

14 changes: 14 additions & 0 deletions samples/graphs/GraphFivePointedStar.grpr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
c DESCRIPTION: 5 Nodes, 5 Edges
c OPTIMAL NUMBER OF COLORS: 3

m 0 0 1 1 0
m 0 0 0 1 1
m 1 0 0 0 1
m 1 1 0 0 0
m 0 1 1 0 0

r 1 2.5
r 0 2
r 0.5 0
r 1.5 0
r 2 2
5 changes: 0 additions & 5 deletions samples/graphs/GraphFivePointedStar.txt

This file was deleted.

18 changes: 18 additions & 0 deletions samples/graphs/GraphFull1.grpr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
c DESCRIPTION: 6 Nodes
c OPTIMAL NUMBER OF COLORS: 7

m 0 1 1 1 1 1 1
m 1 0 1 1 1 1 1
m 1 1 0 1 1 1 1
m 1 1 1 0 1 1 1
m 1 1 1 1 0 1 1
m 1 1 1 1 1 0 1
m 1 1 1 1 1 1 0

r 0 2
r 0.5 3.5
r 2 4
r 3.5 3.5
r 4 2
r 3.5 0.5
r 2 0
7 changes: 0 additions & 7 deletions samples/graphs/GraphFull1.txt

This file was deleted.

20 changes: 20 additions & 0 deletions samples/graphs/GraphFull2.grpr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
c DESCRIPTION: 8 Nodes
c OPTIMAL NUMBER OF COLORS: 8

m 0 1 1 1 1 1 1 1
m 1 0 1 1 1 1 1 1
m 1 1 0 1 1 1 1 1
m 1 1 1 0 1 1 1 1
m 1 1 1 1 0 1 1 1
m 1 1 1 1 1 0 1 1
m 1 1 1 1 1 1 0 1
m 1 1 1 1 1 1 1 0

r 0 2
r 0.5 3.5
r 2 4
r 3.5 3.5
r 4 2
r 3.5 0.5
r 2 0
r 0.5 0.5
8 changes: 0 additions & 8 deletions samples/graphs/GraphFull2.txt

This file was deleted.

14 changes: 14 additions & 0 deletions samples/graphs/GraphOneCircle1.grpr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
c DESCRIPTION: 5 Nodes, 5 Edges
c OPTIMAL NUMBER OF COLORS: 3

m 0 1 0 0 1
m 1 0 1 0 0
m 0 1 0 1 0
m 0 0 1 0 1
m 1 0 0 1 0

r 0 1
r 1 2
r 2 2
r 3 1
r 1.5 0
5 changes: 0 additions & 5 deletions samples/graphs/GraphOneCircle1.txt

This file was deleted.

16 changes: 16 additions & 0 deletions samples/graphs/GraphOneCircle2.grpr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
c DESCRIPTION: 6 Nodes, 6 Edges
c OPTIMAL NUMBER OF COLORS: 2

m 0 1 0 0 0 1
m 1 0 1 0 0 0
m 0 1 0 1 0 0
m 0 0 1 0 1 0
m 0 0 0 1 0 1
m 1 0 0 0 1 0

r 0 1
r 1 2
r 2 2
r 3 1
r 2 0
r 1 0

0 comments on commit f923168

Please sign in to comment.