1 '''
2 Created on 08.06.2010
3
4 @author: Robert Fischbach, (adepted to GUI by Tobias Heimpold)
5
6 Generating solutions randomly distributed over the solution space
7 without any dependencies between them.
8 '''
9
10 import numpy, os
11 from dsc_suite.tools.toolpaths import FOLDER_FOR_DATA
12 from time import strftime, time
13
14
15
16
17
19 """ Sample solution space randomly, evenly distributed and without dependencies.
20 parameter:
21 - functions: dictionary with functions from datastructure
22 essential keys:
23 - "randomSolution" : returns a random representation
24 - "costEvaluation" : returns a list of the calculated costs for the given
25 representation
26
27 - parameters : dictionary with algorithm parameters
28 essential keys:
29 - "samples" : number of generated random representations
30
31 - file_info: dictionary with information about the file names of the data files
32 essential keys:
33 - "trial_name" : name of trial given in GUI
34 - "file_name_list" : additional information included in file name (can be empty string)
35
36 - time_check: boolean
37 True: estimates runtime with reduced samples
38 False: full calculation with saving data to files
39 """
40 import psyco
41 psyco.full()
42 start_time = time()
43
44 representation = functions["randomSolution"]()
45 costs = functions["costEvaluation"](representation)
46 cost_data = []
47 for cost in costs:
48 cost_data.append([])
49
50 if time_check:
51 samples = parameters["samples"]
52 parameters["samples"] = 100
53
54 i = 0
55 while i < parameters["samples"]:
56 i += 1
57
58 representation = functions["randomSolution"]()
59
60 costs = functions["costEvaluation"](representation)
61
62 for cost in costs:
63 cost_data[costs.index(cost)].append(cost)
64
65 trial_name = file_info["trial_name"]
66 file_name_list = file_info["file_name_list"]
67 path = '/' + trial_name + '/Monte Carlo/'
68 if not os.path.exists(FOLDER_FOR_DATA + path):
69 os.makedirs(FOLDER_FOR_DATA + path)
70 filename_list = []
71 for list in cost_data:
72 file_name_begin = file_name_list[cost_data.index(list)]
73 i = 1
74 filename = file_name_begin + ' %02i S%i.npy' % (i, parameters["samples"])
75 while os.path.exists(FOLDER_FOR_DATA + path + filename):
76 i += 1
77 filename = file_name_begin + ' %02i S%i.npy' % (i, parameters["samples"])
78 filename_list.append(path + filename)
79 if not time_check:
80 numpy.save(FOLDER_FOR_DATA + path + filename, numpy.float64(list))
81 end_time = time()
82 psyco.stop()
83
84 if time_check:
85 runtime = (end_time - start_time) / parameters["samples"] * samples
86 parameters["samples"] = samples
87 return runtime
88 else:
89 return filename_list
90