-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem1.py
More file actions
48 lines (35 loc) · 1.17 KB
/
problem1.py
File metadata and controls
48 lines (35 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import random
class Problem1:
def __init__(self,graph,nodenum,numberOfColor):
self.gragh = graph
self.nodenum = nodenum
self.numberOfColor = numberOfColor
self.colorGraph = []
def generateStart(self):
colorG = [random.randint(1,int(self.numberOfColor)) for x in range(int(self.nodenum))]
return colorG
def neighbors(self,currentSol):
"""
:param currentSol : current state
:return: the best neighbor for current state
"""
neighbors = []
temp = currentSol[:]
for i in range(0,int(self.nodenum)) :
# print(i,'i')
for j in range(1,int(self.numberOfColor) + 1) :
if temp[i] != j :
temp[i] = j
neighbors.append(temp)
temp = currentSol[:]
return neighbors
def value(self,state):
# print(state)
v = 0
for i in range(int(self.nodenum)) :
for j in range(int(self.nodenum)):
if self.gragh[i][j] == 1 :
if state[i] != state[j] :
v += 1
# print(v)
return v/2