summaryrefslogtreecommitdiff
path: root/SA_POP/utils/SANetGenerator/Rand_Skeletons.py
blob: 76b1ea0593239ae0efc1d8e4b066a9c8a16e4853 (plain)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#Daniel Mack
#Random Skeleton Generators

import networkx as nx
import random as rand


#Modified Erdos-Renyi Graph Generator 
def erdosGen(SANet, tasks, conds, inDegree, outDegree):
	inD = {}
	outD = {}
	pairs = {}
	haltGen = 0;
	haltOut = 0;
	haltIn  = 0;

	#continue until the graph has met the in/out degreee limits
	while  haltGen == 0:
		ctask = ""
		ccond = ""
		found = 0
		while found == 0:
			#Choose Task
			ctask = tasks[rand.randrange(len(tasks))] 
			#Choose Cond
			ccond = conds[rand.randrange(len(conds))]
			key = ctask + "," + ccond
			#if already been used as either an effect or precondition
			if key in pairs:
				found = 0
			else:
				#valid pair for random pairing
				found = 1
				pairs[key] = 1	

		#choose direction
		EfforPre = rand.randrange(2)

		#If 0 then it's a precondition link
		if EfforPre == 0 and haltIn == 0:
			SANet.add_edge(ccond, ctask)
			if ctask in inD:
				inD[ctask] += 1
			else:
				inD[ctask] = 1	
		elif haltOut == 0:
			#otherwise it's an effect
			SANet.add_edge(ctask, ccond)
			if ctask in outD:
				outD[ctask] += 1
			else:
				outD[ctask] = 1

		#Check for Stopping Conditions 		
		totalIn = 0.0
		totalOut = 0.0
		for each in tasks:
			if each in inD:
				totalIn += inD[each]
			if each in outD:
				totalOut += outD[each]
		avgIn = totalIn/len(tasks)
		avgOut = totalOut/len(tasks)


		if avgIn >= inDegree:
			haltIn = 1 
		if avgOut >= outDegree:
			haltOut = 1
		if haltIn == 1 and haltOut == 1:	
			print avgIn
			print avgOut
			haltGen = 1
				
	return SANet