def generate(): output_dir = "/tmp/" + str(random.randint(1, sys.maxint)) output_file_name = "/tmp/" + str(random.randint(1, sys.maxint)) try: data = { "app_name": request.form["app_name"], "language": request.form["language"], "company_name": request.form["company_name"], "app_package_name_prefix": request.form["app_package_name_prefix"], "app_class_prefix": request.form["app_class_prefix"], "application_id": request.form["application_id"], "compile_sdk_version": int(request.form["compile_sdk_version"]), "build_tools_version": request.form["build_tools_version"], "min_sdk_version": int(request.form["min_sdk_version"]), "target_sdk_version": int(request.form["target_sdk_version"]), "crashlytics_api_key": request.form["crashlytics_api_key"], "output_dir": output_dir } grow.grow(data) shutil.make_archive(output_file_name, 'zip', output_dir) return send_file(output_file_name + ".zip", mimetype="application/zip", as_attachment=True, attachment_filename=request.form["app_class_prefix"] + ".zip") finally: if os.path.exists(output_dir): shutil.rmtree(output_dir) if os.path.exists(output_file_name): os.remove(output_file_name)
def generate(): output_dir = "/tmp/" + str(random.randint(1, sys.maxint)) output_file_name = "/tmp/" + str(random.randint(1, sys.maxint)) try: data = { "app_name" : request.form["app_name"], "language" : request.form["language"], "company_name" : request.form["company_name"], "app_package_name_prefix" : request.form["app_package_name_prefix"], "app_class_prefix": request.form["app_class_prefix"], "application_id" : request.form["application_id"], "compile_sdk_version" : int(request.form["compile_sdk_version"]), "build_tools_version" : request.form["build_tools_version"], "min_sdk_version" : int(request.form["min_sdk_version"]), "target_sdk_version" : int(request.form["target_sdk_version"]), "crashlytics_api_key" : request.form["crashlytics_api_key"], "output_dir" : output_dir } grow.grow(data) shutil.make_archive(output_file_name, 'zip', output_dir) return send_file(output_file_name + ".zip", mimetype="application/zip", as_attachment=True, attachment_filename=request.form["app_class_prefix"] + ".zip") finally: if os.path.exists(output_dir): shutil.rmtree(output_dir) if os.path.exists(output_file_name): os.remove(output_file_name)
def test(self): tests = [ [6, [1, 2, 3]], [16, [4, 1, 1, 1, 4]], [64, [2, 2, 2, 2, 2, 2]], ] for exp, inp in tests: self.assertEqual(grow(inp), exp)
def test_rand(self): from random import randint from operator import mul reference = lambda a: len(a) and int(0 not in a) and __import__( "functools").reduce(mul, a) def test_case(): return [randint(0, 1000) for _ in range(randint(1, 1000))] for _ in range(100): t = test_case() self.assertEqual(grow(t[:]), reference(t))
def getfitness(templateFiles): """Automated generation of FEM models and execuation of simulations""" global switch, valid_ind, indList, err1, err2, result_GEN while switch: try: job = indList.get(block=False) except Empty: break else: # define the name in the form "index of generation"_"index of individual": flag = 1 name = job[0] GEN = int(name.split('_')[0]) phenotype = decoder(job[1]) if Multi: for x in result_GEN: for y in x: if y[1] == phenotype: job[1].fitness.values = y[2] result_GEN[GEN].append( [job[0], phenotype, job[1].fitness.values]) flag = 0 break if flag == 0: break else: for y in result_GEN: same = 0 for key in y[1].keys(): if y[1][key] != phenotype[key]: same = 0 break same = 1 if same: print("%s is the same as %s." % (name, y[0])) job[1].fitness.values = (y[2], ) flag = 0 break if flag: print("starting the evaluation of %s at %s." % (name, datetime.now())) stdout.flush() for n, templateFile in enumerate(templateFiles): replacedFile = str(templateFile[1]) #replacedFile= replacedFile.replace('NAME', name) for key in phenotype.keys(): replacedFile = replacedFile.replace( key, str(phenotype[key])) f1 = open('T%s_%s' % (name, templateFile[0]), 'w') f1.write(replacedFile + '\n') f1.close() print("start growing %s" % name) stdout.flush() try: grow(name, phenotype) except: switch = 0 err1 = traceback.format_exc() stdout.flush() else: try: fitness = objective(name, phenotype) except: switch = 0 err2 = traceback.format_exc() stdout.flush() else: files = glob("%s*" % name) for file in files: remove(file) job[1].fitness.values = fitness if Multi: result_GEN[GEN].append([ job[0], decoder(job[1]), job[1].fitness.values ]) else: result_GEN.append([ job[0], decoder(job[1]), job[1].fitness.values[0] ]) valid_ind.append(job[1]) print("The evaluation of %s bas completed at %s" % (name, datetime.now())) stdout.flush()
def test_grow(): for exp, inp in tests: assert grow(inp) == exp
# 读取文件并填充到数组中 f = open("testDigits/" + file) for line in f.readlines(): # 去掉末尾的换行符 line = line.strip("\n").strip("\r") # 每一行的数组 row = [] for letter in line: row.append(letter) arr.append(row) f.close() struct = gs.clearStructure(struct) # 填充结构 fs.fillStructure(arr, struct) # 生长 struct = grow.grow(struct) # 评估结果 X_train.append(assess.do(struct)) # 归一化 print "训练完毕,已训练样本" + str(len(X_train)) + "个" print "开始进行归一化..." X_train, normPara = norm.doAll(X_train) print "归一化完毕,开始进行数据测试..." # 查看精确度 # 正确和错误数量 right = 0