summaryrefslogtreecommitdiff
path: root/SA_POP/utils/SANetGenerator/Rand_Params.py
blob: 23a24489a4a4212896b89f3aa1550635278893f6 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python
# encoding: utf-8
"""
Rand_Params.py
Created by Daniel Mack on 2009-10-27.
Generates Random Parameters for a Skeleton
"""

import sys
import os
import networkx as nx
import random as rand

#Uniform 
def flatGen(SANet, tasks, conds):
	effLinks = {}
	preLinks = {}
	priors = {}
	
	#use the tasks as the starting point for assigning probabilities
	for each in tasks:
		effects = SANet.successors(each)
		effLinks[each] = [] 
		effAr = effLinks[each]
		
		for eff in effects:
			assigned = 0
			#uses uniform for the time being
			edges = SANet.edges(eff)
			#print eff
			#print edges
			#for edge in edges:
			#	#print eff
			#	if edge[1] == each:
			#		#print edge
			#		assigned = 1
			#		weight = rand.uniform(-.8,-1)
			#		#print weight
			#		break
				
			if assigned == 0:
				#dire = rand.uniform(0,2) - 1
			
				#if dire >= 0:
				weight = rand.uniform(.8,1)
				#else:
				#	weight = rand.uniform(-.8,-1)	
				#a tuple for efficient look up
			lnk = (eff, weight)
			effAr.append(lnk)
			
		effLinks[each] = effAr
		
		preconds = SANet.predecessors(each)
		
		#this is uniform at .5 probability across all tasks
		prior = 1.0
		preprob = 0.0
		#must have preconditions
		#TEMP#TEMP#TEMP#TEMPTEMP#TEMP#TEMP#TEMP
		#currently all preconditions must be positive
		#TEMP#TEMP#TEMP#TEMP#TEMP#TEMP#TEMP#TEMP
		if len(preconds) > 0:
			#Value is .5 to the power of the number of preconditions
			prior = pow(.5, len(preconds))
			#Each precondition probability is .5 to the number of preconditions - 1
			#Should work for positive and negative, but for now, all preconditions are positive.
			preprob = pow(.5, (len(preconds) - 1))
			
		priors[each] = prior
		
		#Book keeping for the graph. 
		#Assemble information on precondition links
		for pre in preconds:
			if pre in preLinks:
				temp =  preLinks[pre]
				#A pair of task and precondition probability from the given conditon
				temp.append((each,preprob))
				preLinks[pre] =temp
			else:
				lnks = []
				lnks.append((each, preprob))
				preLinks[pre] = lnks	 
	
	#returns the network structure, the priors on the tasks and hashmaps of the links and their values	
	info = (SANet, priors, preLinks, effLinks)
	return info