def example(): name = "Hello" ctor = Method("Hello", [ Argument("double", "dummy"), Argument("double", "x", 1.0), Argument("int", "j", 1), Argument("char *", "name", "hee"), Argument("char *", "password", 0), ], ["std::cout << x << std::endl;", "m_x = x;", "m_arr = new double[10];"]) dtor = Method("~Hello", [], ["delete [] m_arr;"]) call = Method("operator()", [Argument("char *", "someone")], ['''std::cout << "hello " << someone << std::endl;'''], type="void") m_x = Member("double", "m_x") m_arr = Member("double *", "m_arr") parentClass = Class('Parent') klass = Class(name, public_methods=[ctor, dtor, call], private_members=[m_x, m_arr], namespace="Greeting::Goodwill", headers_dependent_on=["iostream"], parents=[parentClass]) assert klass.constructors() == [ctor] return klass
def __init__(self, spec): Method.__init__(self) plan = ExperimentPlan(Context.SimulationChain) print "MethodDOE: ", spec plan.Import(open(spec["Plan"])) self.Plan = plan #self._Event = threading.Event() #self._Event.set() self._Sem = threading.Semaphore(0)
def test(self): method = Method() method.id = 'a' method.startTime = -1. method.equilibrationTime = 1. method.recordingTime = 2. method.maximumSteps = 1e9 method.numberOfFrames = 100 method.solverParameter = 0.01 stream = StringIO() writeMethodXml(method, stream)
def __init__(self, spec): Method.__init__(self) plan = ExperimentPlan(Context.SimulationChain) print "MethodDOE: ", spec plan.Import(open(spec["Plan"])) self.Plan = plan if spec.has_key("InfillCriteriaFunction"): self.InfillCriteriaFunction = spec["InfillCriteriaFunction"] else: self.InfillCriteriaFunction = AdaptiveDOECriteria.SimpleLOO self.MaxIterations = spec["Iterations"] self.NewSitesPerIteration = spec["NewSitesPerIteration"] self._Sem = threading.Semaphore(0)
def test_execute_void_no_parameters(self): from VM import VM vm = VM() m = Method() m.name = 'TestMethod' m.returnType = Types.Void m.parameters = [] vm.methods.append(m) self.assertEqual(vm.current_method(), None) vm.execute_method(m) self.assertEqual(vm.current_method(), m) r = Ret() r.execute(vm) self.assertEqual(vm.current_method(), None)
def get_method(self): m = Method() m.methodDefinition = self; m.locals = copy.deepcopy(self.locals) #m.parameters= copy.deepcopy(self.parameters) m.instructions = self.instructions m.maxStack = self.maxStack m.returnType = self.returnType m.attributes = self.attributes return m
def __init__(self, spec): Method.__init__(self) self.Chromosome = Factory.Chromosome(Context.SimulationChain) self.ResEval = ChainResponseEvaluator() self.ResEval.Start() self.PenEval = Factory.CreatePenaltyEvaluator(spec) self.FitnessEval = EA.FitnessEvaluator() optiConfig = spec["OptimizerConfig"] popSize = optiConfig["PopulationSize"] self.OptimizerConfig = optiConfig postIterCallback = lambda gen, pop: self.PostIteration(gen, pop) self.BestExperiment = None if spec["Optimizer"] == "GA": self.Optimizer = EA.MonoObjectiveGA(self.Chromosome, self.ResEval, self.PenEval, self.FitnessEval, popSize, 2) self.Optimizer.PostIterationCallback = postIterCallback self.HistoryFile = open("%s.history.dat" % Context.Name, "w") elif spec["Optimizer"] == "SurrogateGA": Context.Database.Import(open(optiConfig["DOE"])) pop = Factory.PopulationFromPlan(self.Chromosome, Context.Database) self.Optimizer = EA.MonoObjectiveSurrogateGA( self.Chromosome, self.ResEval, self.PenEval, self.FitnessEval, pop) elif spec["Optimizer"] == "ParetoGA": self.Optimizer = EA.SPEA2(self.Chromosome, self.ResEval, self.PenEval, optiConfig["PopulationSize"], optiConfig["ArchiveSize"]) elif spec["Optimizer"] == "ParetoSurrogateGA": Context.Database.Import(open(optiConfig["DOE"])) pop = Factory.PopulationFromPlan(self.Chromosome, Context.Database) self.Optimizer = EA.ParetoSurrogateGA( self.Chromosome, self.ResEval, self.PenEval, pop, popSize, optiConfig["ArchiveSize"], optiConfig["Update"], optiConfig["UpdateTruncation"])
def test_execute_instance_clears_stack_with_void_return_type(self): from VM import VM vm = VM() m = Method() m.name = 'TestMethod' m.attributes.append(MethodDefinition.AttributeTypes['instance']) m.returnType = Types.Void m.maxStack = 99 m.parameters = [] vm.methods.append(m) vm.stack.push(111) vm.execute_method(m) vm.stack.push(124) vm.stack.push(987) r = Ret() r.execute(vm) self.assertEqual(vm.current_method(), None) self.assertEqual(vm.stack.count(), 1) self.assertEqual(vm.stack.pop(), 111)
def test_execute_int_no_parameters_returns_value_on_stack(self): from VM import VM vm = VM() m = Method() m.name = 'TestMethod' m.returnType = Types.Int32 m.parameters = [] vm.methods.append(m) v = Variable(888) self.assertEqual(vm.current_method(), None) self.assertEqual(vm.stack.get_frame_count(), 0) vm.execute_method(m) vm.stack.push(v) self.assertEqual(vm.current_method(), m) # fixme - test return value too r = Ret() r.execute(vm) self.assertEqual(vm.current_method(), None) self.assertEqual(vm.stack.get_frame_count(), 1) self.assertEqual(vm.stack.pop(), v)
def testTimeInhomogeneous(self): model = StateModel() model.id = 'model' model.speciesIdentifiers.append('s1') model.species['s1'] = StateSpecies('', 'species 1', '13') model.speciesIdentifiers.append('s2') model.species['s2'] = StateSpecies('', 'species 2', '17') model.reactions.append( StateReaction('r1', 'reaction 1', [StateSpeciesReference('s1')], [StateSpeciesReference('s2')], True, '2+sin(t)')) model.reactions.append( StateReaction( 'r2', 'reaction 2', [StateSpeciesReference('s1'), StateSpeciesReference('s2')], [StateSpeciesReference('s1', 2)], False, '1+exp(-t)')) error = model.evaluateInhomogeneous() assert not error method = Method() output = TimeSeriesFrames() solver = FirstReaction(makeModel(model, method), 2**64) simulator = makeTimeSeriesUniform(solver, model, output)
from Dataset import Dataset from Method import Method from Evaluation import Evaluation from Result import Result from Setting import Setting import numpy as np #--------------------------- run the exp ---------------------------- if 1: k = 5 fold = 10 dataset = Dataset('', '') dataset.file_folder_path = '../data/input/' method = Method('', '') method.k = k evaluation = Evaluation('') result = Result('', '') result.k = k setting = Setting('', '', dataset, method, result, evaluation) setting.fold = fold setting.load_classify_save() if 1: fold = 10 k = 5 result = Result('', '')
def main(): from Species import Species from Method import Method from SpeciesReference import SpeciesReference print('-' * 79) print('Time Homogeneous') model = Model() model.id = 'model' model.compartments['C1'] = Value('Cell', '1') model.speciesIdentifiers.append('s1') model.species['s1'] = Species('C1', 'species 1', '13') model.speciesIdentifiers.append('s2') model.species['s2'] = Species('C1', 'species 2', '17') model.reactions.append( Reaction('r1', 'reaction 1', [SpeciesReference('s1')], [SpeciesReference('s2')], True, '1.5')) model.reactions.append( Reaction('r2', 'reaction 2', [SpeciesReference('s1'), SpeciesReference('s2')], [SpeciesReference('s1', 2)], True, '2.5')) writeModelXml(model) print('') print(model.writeSpeciesTable()) print('') print(model.writeReactionsTable()) # The model must be evaluated for writing SBML or the C++ code. model.evaluate() writeModelSbml(model, open('model.xml', 'w')) method = Method() writeModelMathematica(model, method, open('model.nb', 'w')) print('') print(model.makePropensitiesNumberOfReactions()) print(model.makePropensitiesConstructor()) print(model.makePropensitiesMemberFunctions(True)) print('') model.writeCmdl(sys.stdout) print('-' * 79) print('Time Inhomogeneous') model = Model() model.id = 'model' model.speciesIdentifiers.append('s1') model.species['s1'] = Species('', 'species 1', '13') model.speciesIdentifiers.append('s2') model.species['s2'] = Species('', 'species 2', '17') model.reactions.append( Reaction('r1', 'reaction 1', [SpeciesReference('s1')], [SpeciesReference('s2')], True, '2+sin(t)')) model.reactions.append( Reaction('r2', 'reaction 2', [SpeciesReference('s1'), SpeciesReference('s2')], [SpeciesReference('s1', 2)], False, '1+exp(-t)')) writeModelXml(model) print('') print(model.writeSpeciesTable()) print('') print(model.writeReactionsTable()) # The model must be evaluated for writing SBML or the C++ code. model.evaluateInhomogeneous() writeModelSbml(model, open('model.xml', 'w')) print('') print(model.makeInhomogeneousPropensities(True)) print('')
def main(opt): # Load default values and env env, envName, nEp, nStep, nRFF, kernelType, lowS, highS, glue, sigma, alphaQ, betaQ, alphaU, betaU = gen_env( opt) print(opt) # Generate directory for experiment if opt.enableLogFile: expName, i = "{}_{}".format(opt.expName, envName), 1 while os.path.exists("exps/{}{}".format(expName, i)): i += 1 dirName = "exps/{}{}".format(expName, i) os.makedirs(dirName) # Run experiment allRets = [] for repI in range(opt.nRepeat): tm = TransitionMemory(nEp, nStep, glue.dS(), glue.dA()) # Agt, Pol if opt.featureType == "RFF": # Random Fourier Features ftmap = RFF(nRFF, glue.dS() + glue.dA(), sigma, kernelType) elif opt.featureType == "FBF": # Fourier basis features ftmap = FBF(glue.dS() + glue.dA(), opt.fourierBasisOrder) agtHlp = AgentHelper(glue, ftmap) if opt.agent == "method": agent = Method(agtHlp, gamma=opt.gamma, alphaQ=alphaQ, betaQ=betaQ, alphaU=alphaU, betaU=betaU, tm=tm, maxVIiter=opt.maxVIiterQ, kappa=opt.kappa) elif opt.agent == "QLearning": lr = ConstantRate(opt.learningRate) agent = QLearning(agtHlp, gamma=opt.gamma, learningRate=lr) else: raise ValueError("Unknown agent {}".format(opt.agent)) policy = eGreedyPolicy(agtHlp, agent.score, opt.epsilon) # Runner runner = EnvRunner(glue, policy, agent, tm, opt.verbose) # Go runner.run(nEp, nStep, opt.stopWhenSolved) # Keep track of returns idx = np.cumsum([0] + tm.getEpisodeLengths()) ss, _, rr, __ = tm.getTransitions() sMin, sMax = np.min(ss, 0), np.max(ss, 0) print("State min/max:\n{}\n{}".format(sMin, sMax)) rets = [np.sum(rr[idx[i - 1]:idx[i]]) for i in range(1, len(idx))] print("Repeat", repI, "finished.") print("Returns:\n", rets) allRets.append(rets) # Parse all variables to file del rr, _, ss, __ if opt.enableLogFile: filename = "{}/vars_{}.json".format(dirName, uuid.uuid4().hex) with open(filename, 'w') as f: json.dump({k: repr(v) for k, v in vars().items()}, f, indent=4, sort_keys=True)
def parse_methods(self): """ Parse <method> Nodes """ from Method import Method for meth in self.find_children(self.ns(self.NS_CORE, 'method')): self.methods.append(Method(self._namespace, meth))
def ParseMethodName(text): from settings import PrivateDefinition,PublicDefinition from settings import ProtectedDefinition,StaticDefinition from settings import OverrideDefinition,AbstractDefinition from settings import VirtualDefinition Parameters=[] original = text if (text.find("(")>-1): Parameters=ParseParameters(text[text.find("(")+1:text.find(")")]) text = text[:text.find("(")] text=text+" " original = text m = Method() words = text.split() for word in words: if (word==PrivateDefinition.replace(" ","")): m.Private=True text=text.replace(word,"") elif (word==PublicDefinition.replace(" ","")): m.Public =True text=text.replace(word,"") elif (word==ProtectedDefinition.replace(" ","")): m.Protected=True text=text.replace(word,"") elif (word==StaticDefinition.replace(" ","")): m.Static=True text=text.replace(word,"") elif (word==OverrideDefinition.replace(" ","")): m.Override=True text=text.replace(word,"") elif (word==AbstractDefinition.replace(" ","")): m.Abstract=True text=text.replace(word,"") elif (word==VirtualDefinition.replace(" ","")): m.Virtual=True text=text.replace(word,"") elif (word in DataTypes): m.ReturnType=word text=text.replace(word,"") if len(text.split())==2: m.ReturnType=text.split()[0] m.Name=text.split()[1] elif len(text.split())==1: m.ReturnType = "" m.Name=text.split()[0] else: raise ValueError("nerozpoznane paramtre v nazve metody v pocte "+str(len(text.split()))+" text: ",text) m.Parameters = Parameters return m
def write(output_file_path,method_instance,tree_function,weather,surface_fuels,simulation,slices): # function to handle recursive file creation/calling of writer (function that actually does the writing) # INPUT # all inputs not given here are the same as writer function - look at writer function documentation below # tree_function (list or string) - if given as a list file path will be recursively created and output for each element in list # for what tree_function actually is look at writer documentation # surface_fuels (list or list of lists) - if a list is given at any index of surface_fuels file path, and output will be recursively # created for each element. For further documentation look under writer. # weather (list or list of lists) - if a list is given at any index of surface_fuels file path, and output will be recursively # created for each element. For further documentation look under writer. # method_instance (switch case) - can be instance, string, list or a list containing any combination of all three # (Method instance) - instance of the Method class should hold tree_list with positions # OR # (method='csv' info) - [[x,y],inputToMethod.csv] or [[x,y],path\To\Folder\Containing\Csv\Files] # - [[x,y],inputToMethod.csv] (list) - x,y and .csv file given - Method instance created from input # OR # - [[x,y],path\To\Folder\Containing\Csv\Files] (list) - x,y and file path given - Method instances created from every .csv file in file path # OR # (method='load' info) - 'Path\To\Folder\Containing\Pkl\Files' or .pkl file # - 'Path\To\Folder\Containing\Pkl\Files' (str) - path to folder containing .pkl files, Method instance created using load from every .pkl file in path # - .pkl file (str) - Method instance created using load from .pkl file # OR # (list) - list containing any combination of the three above # output_file_path (string) - path to desired output root file. Files will be created in that folder. # OUTPUT # all output created by writer function # ----note---- # max recursion depth is 1000 by default, if you want to create more than 1000 fds input documents # you will have to reset max recursion depth (may God have mercy on your soul) bat_file = open(output_file_path + '\Run.bat', "wb") def list_me(x): if type(x) == list: if len(x) == 2 and (((type(x[0]) == list) or (type(x[0]) == tuple)) and (type(x[1]) == str)): return [x] # in case method_instance input is just one list of type method='csv' info else: return x else: return [x] method_final = [] for i in list_me(method_instance): if isinstance(i,Method): method_final.append(i) elif type(i) == str: # file path to folder containing .pkl files if list(i[1])[-1] == '\\' or list(i[1])[-1] == '/': i = i + '*.pkl' else: i = i + '\*.pkl' files = glob.glob(i) for j in files: method_final.append( Method(file_name=j) ) elif type(i) == list: # list [[x,y], path to .csv file] or [[x,y], path to file containing lots of .csv files] if ((type(i[0]) == list) or (type(i[0]) == tuple)) and (type(i[1]) == str): r = re.compile(r'\w+') # regular expression grabs all alpha numeric characters if r.findall(i[1])[-1] == 'csv': # check to see if it is a csv file method_final.append( Method(i[0][0],i[0][1],i[1]) ) else: if list(i[1])[-1] == '\\' or list(i[1])[-1] == '/': i[1] += '*.csv' else: i[1] += '\*.csv' files = glob.glob(i[1]) for j in files: method_final.append( Method(i[0][0],i[0][1],j) ) else:sys.exit('inputs to write do not match formating convention') def recursion(output_file_path,method_instance,tree_function,weather,surface_fuels,simulation,slices): if type(method_instance) == list: for i in method_instance: output_file_path += ('\\' + i.file_name) if not os.path.exists(output_file_path): os.makedirs(output_file_path) i.plot_tree_list(output_file_path) # place plot of tree_list in output file i.plot_vertical_fuel_profile(output_file_path) # place plot of vertical fuel profile in output file #i.write_full_csv(output_file_path) recursion(output_file_path,i,tree_function,weather,surface_fuels,simulation,slices) output_file_path = output_file_path[:-len('\\' + i.file_name)] elif type(tree_function) == list: # tree for i in tree_function: output_file_path += ('\\' + i.upper()) recursion(output_file_path,method_instance,i,weather,surface_fuels,simulation,slices) output_file_path = output_file_path[:-len('\\' + i.upper())] elif type(weather[0]) == list: # humidity for i in weather[0]: output_file_path += ('\Humidity_%s_na' % str(i)) # humidity is a dimensionless ratio na = not applicable recursion(output_file_path,method_instance,tree_function,[i,weather[1],weather[2]],surface_fuels,simulation,slices) output_file_path = output_file_path[:-len('\Humidity_%s_na' % str(i))] elif type(weather[1]) == list: # wind for i in weather[1]: output_file_path += ('\Wind_%s_ms' % str(i)) # ms = m/s recursion(output_file_path,method_instance,tree_function,[weather[0],i,weather[2]],surface_fuels,simulation,slices) output_file_path = output_file_path[:-len('\Wind_%s_ms' % str(i))] elif type(weather[2]) == list: #init_temp for i in weather[2]: output_file_path += ('\Init_temp_%s_dC' % str(i)) # dC = degrees Celsius recursion(output_file_path,method_instance,tree_function,[weather[0],weather[1],i],surface_fuels,simulation,slices) output_file_path = output_file_path[:-len('\Init_temp_%s_dC' % str(i))] elif type(surface_fuels[0]) == list: #sf_moisture for i in surface_fuels[0]: output_file_path += ('\Sf_moisture_%s_na' % str(i)) # moisture content is a dimensionless ratio na = not applicable recursion(output_file_path,method_instance,tree_function,weather,[i,surface_fuels[1],surface_fuels[2]],simulation,slices) output_file_path = output_file_path[:-len('\Sf_moisture_%s_na' % str(i))] elif type(surface_fuels[1]) == list: #sf_ht for i in surface_fuels[1]: output_file_path += ('\Sf_ht_%s_m' % str(i)) # m = meters recursion(output_file_path,method_instance,tree_function,weather,[surface_fuels[0],i,surface_fuels[2]],simulation,slices) output_file_path = output_file_path[:-len('\Sf_ht_%s_m' % str(i))] elif type(surface_fuels[2]) == list: #sf_load for i in surface_fuels[2]: output_file_path += ('\Sf_load_%s_kgM' % str(i)) #kgM = kg per meter squared recursion(output_file_path,method_instance,tree_function,weather,[surface_fuels[0],surface_fuels[1],i],simulation,slices) output_file_path = output_file_path[:-len('\Sf_load_%s_kgM' % str(i))] else: bat_file.write(output_file_path + '\n') writer(output_file_path,method_instance,tree_function,weather,surface_fuels,simulation,slices) recursion(output_file_path,method_final,list_me(tree_function),weather,surface_fuels,simulation,slices)
if __name__ == '__main__': x = 30 y = 30 tree_function = ['green']#['green','red','mpb_truth'] humidity = .43 # humidity of the air (mass H20/volume air+H20) wind = 2 #[2,3,4] # wind speed from the "x" wall (m/s) init_temp = 25 #[20,30] # initial temp of the simulation, this will be the air temp as well as initial veg temp (degrees C) sf_moisture = 0.3 # surface fuel moisture (weight H20/weight dry) sf_ht = .5 # [.5,.6] # surface fuel (m) sf_load = [7.] # surface fuel (kg/m^2) mesh = 5 # number of meshes in simulation (see note 2 in Write.py) (int) sim_time = 5 # time of simulation (s) dt_part = .1 # dump simulation data every dt_part seconds (used in "&DUMP DT_PART" line)(s) dt_slice = .5 # dump slice every dt_slice seconds (used in "&DUMP DT_SLCF" line) (s) weather = [humidity,wind,init_temp] surface_fuels = [sf_moisture,sf_ht,sf_load] simulation = [mesh,sim_time,dt_part,dt_slice] #file_name2 = [[x,y],'E:\Projects\STANDFIRE\Data\PLOT1.csv'] #a = Method(x=50,y=50,file_name='E:\Projects\STANDFIRE\Data\PLOT1621331010602.csv',TreePlaceMethod='sr') a = Method(x=50,y=50,file_name='E:\Projects\STANDFIRE\Data\PLOT1.csv',TreePlaceMethod='sr') #print a.canopy_bulk_density_rein() write('E:\Projects\STANDFIRE\Data',a,tree_function,weather,surface_fuels,simulation,None)
def tearDown(self): Method.tearDown(self)
print('-----------------------------\t') ## method settting method = 'r0=random0.1' train_flag = True train_flag = False RLmethod = Method( method, env.action_dim, # 动作的维度 env.ob_dim, # 状态的维度 env.a_bound, # 动作的上下限 e_greedy_end=0.05, # 最后的探索值 0.1倍幅值 e_liner_times=1500 * 50, # 探索值经历多少次学习变成e_end epilon_init=0.5, # 表示1倍的幅值作为初始值 LR_A=0.00001, # Actor的学习率 LR_C=0.001, # Critic的学习率 GAMMA=0.9, # 衰减系数 TAU=0.01, # 软替代率,例如0.01表示学习eval网络0.01的值,和原网络0.99的值 MEMORY_SIZE=3000, # 记忆池容量 BATCH_SIZE=128, # 批次数量 units_a=1000, # Actor神经网络单元数 units_c=1000, # Crtic神经网络单元数 actor_learn_start=10000, # Actor开始学习的代数 tensorboard=True, # 是否存储tensorboard train=train_flag # 训练的时候有探索 ) ############################### training #################################### if RLmethod.train: for i in range(max_Episodes):
## method settting method = 'r0=1,4' # tensorboard --logdir="2 QUAD/6 DL_OP/1 DDPG_OP/logs" train_flag = True train_flag = False RLmethod = Method( method, env.lambda_dim, # 动作的维度 env.ob_dim, # 状态的维度 e_greedy_end=0.000, # 最后的探索值 0.1倍幅值 e_liner_times=max_Episodes * 0.2, # 探索值经历多少次学习变成e_end epilon_init=1, # 表示1倍的幅值作为初始值 LR_A=0.0001, # Actor的学习率 LR_C=0.0001, # Critic的学习率 GAMMA=0.9, # 衰减系数 TAU=0.01, # 软替代率,例如0.01表示学习eval网络0.01的值,和原网络0.99的值 MEMORY_SIZE=20000, # 记忆池容量 BATCH_SIZE=500, # 批次数量 units_a=300, # Actor神经网络单元数 units_c=300, # Crtic神经网络单元数 actor_learn_start=10000, # Actor开始学习的代数 tensorboard=True, # 是否存储tensorboard train=train_flag # 训练的时候有探索 ) RLmethod.constant = env.constant ############################### training #################################### if RLmethod.train: