simulate.py 1.74 KB
Newer Older
navidmokh's avatar
navidmokh committed
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
import random
from InOutput import *
from igraph import *
from Car_Sim import *



def simulate(g,initialCondition,Time_horizon):
	Compute_Order = g.topological_sorting(mode=OUT)
	Current_Vertex = Compute_Order[0]


	remainTime = Time_horizon
	# eng = matlab.engine.start_matlab()
	# print('Matlab Started!')
	# eng.setup(nargout=0)
	Current_Time = 0




	dimensions = len(initialCondition)+1


	simResult = []

	while remainTime>0:
li213's avatar
li213 committed
28
29
		print('-----------------------------------------------------')
		print('Current State', g.vs[Current_Vertex]['label'])
navidmokh's avatar
navidmokh committed
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

		Current_successors = g.successors(Current_Vertex)
		if len(Current_successors)==0:
			Transite_Time = remainTime
			print("Last mode, no more transitions, will stop at %f second" % (Transite_Time))
		else:
			Current_Successor = random.choice(Current_successors)
			edgeid = g.get_eid(Current_Vertex,Current_Successor)
			Current_transtime = g.es[edgeid]["label"]
			Transite_Time = float(random.uniform(Current_transtime[0],Current_transtime[1]))
			print("Will transite to mode %s at %f second" % (g.vs[Current_Successor]["label"], Transite_Time))

		curLabel = g.vs[Current_Vertex]['label']

		Current_Simulation = TC_Simulate(curLabel,initialCondition,Transite_Time)
		


		for i in range (len(Current_Simulation)):
			Current_Simulation_row = [Current_Simulation[i][0] + Current_Time]
			for j in range (1,len(Current_Simulation[0])):
				Current_Simulation_row.append(Current_Simulation[i][j])
			simResult.append(Current_Simulation_row)

		#simResult+=Current_Simulation
		remainTime-=Transite_Time
		initialCondition = Current_Simulation[-1][1:]
		Current_Time = Current_Time + Transite_Time

		Current_Vertex = Current_Successor

	return simResult
	#write_to_file(simResult,'output/TC_Traj.txt','simulation')