def main_exec(options): App = problem.Problem() if not options.isTemplate: if not os.path.exists(options.projectfile): raise IOError, "Missing configuration file '%s'" % options.projectfile if App.load(options.projectfile): # create output folder if [output prefix] has a path output_dir = os.path.dirname( App.opts['configure']['output prefix']) if len(output_dir) > 0: if not os.path.exists(output_dir): os.mkdir(output_dir) App.setLogger('sim2Impact') else: raise RuntimeError("Invalid YAML configuration file") if options.isTemplate: template_options = { 'impact': { 'erd file': ['Net3.erd'], 'metric': ['MC'] }, 'configure': { 'output prefix': 'Net3' } } App.saveTemplate(options.projectfile, template_options) else: App.run()
def run_problem(filename, arguments): graph = graph_map.GraphMap( _NUM_NODES, arguments.top_k, largest_correlation=arguments.correlation_strength) agents = np.random.randint(_NUM_HUBS, size=_NUM_AGENTS) tasks = np.random.randint(_NUM_HUBS, graph.num_nodes, size=_NUM_TASKS) p = problem.Problem(graph, agents, tasks, num_samples=_NUM_SAMPLES, num_groundtruth_samples=_NUM_SAMPLES_GT, aggregation=problem.MinimumAggregation()) results = { 'lower_bound': ([], []), 'hungarian': ([], []), # 'repeated_hungarian': ([], []), # 'greedy': ([], []), 'random': ([], []), # 'no_correlation_greedy': ([], []), 'closest': ([], []), } p.reset() for algorithm, (costs, correlations) in results.items(): cost = getattr(p, algorithm)(arguments.deployment_size) correlation = p.get_correlation() costs.extend(cost) correlations.append(correlation) store_results(results, filename)
def main(filename, sample_count): seed = time.time() random.seed(seed) target_formula = smt.read_smtlib(filename) variables = target_formula.get_free_variables() var_names = [str(v) for v in variables] var_types = {str(v): v.symbol_type() for v in variables} var_domains = {str(v): (0, 200) for v in variables} # TODO This is a hack domain = problem.Domain(var_names, var_types, var_domains) name = basename(filename).split(".")[0] target_problem = problem.Problem(domain, target_formula, name) # compute_difference(domain, target_formula, target_formula) samples = generator.get_problem_samples(target_problem, sample_count, 1) initial_indices = random.sample(list(range(sample_count)), 20) learner = KCnfSmtLearner(3, 3, RandomViolationsStrategy(5)) dir_name = "../output/{}".format(name) img_name = "{}_{}_{}".format(learner.name, sample_count, seed) # learner.add_observer(plotting.PlottingObserver(data_set.samples, dir_name, img_name, "r0", "r1")) with open("log.txt", "w") as f: learner.add_observer(inc_logging.LoggingObserver(f)) print(parse.smt_to_nested(learner.learn(domain, samples, initial_indices)))
def setUp(self): self.p = problem.Problem() self.n1 = Node((0, 0)) self.n2 = Node((1000, 0)) self.rod = Rod(self.n1, self.n2) self.beam = Beam(self.n1, self.n2)
def main(): prob = problem.Problem(dim=3, prangetup=(-3, 3)) popu = Population(prob, size=10) popu.randominit() print(popu.poparr) print(popu.avg_fitness()) print(popu.fitarr) print(popu.find_expecarr()) print("problem starts here") newsel = misc.Selection() newcros = misc.Crossover() newmuta = misc.Mutation() for i in newsel.select_parent(popu): child1, child2 = newcros.do_crossover(i) child1 = newmuta.mutate( child1, prob.rangetup, switch=True, ) child2 = newmuta.mutate(child2, prob.rangetup) print(child1, child2)
def adapt_domain(target_problem, lb, ub): domain = target_problem.domain var_domains = {} for v in domain.variables: var_domains[v] = (lb, ub) adapted_domain = problem.Domain(domain.variables, domain.var_types, var_domains) return problem.Problem(adapted_domain, target_problem.theory, target_problem.name)
def main(): # validate the params if len(sys.argv) != 2: print("Usage: python3 main.py <NETWORK_FILE>") sys.exit(-1) # read in the network from file net = network.readFromFile(sys.argv[1]) # create the problem model model = problem.Problem(net) # search using BFS bfsSearcher = bfs.BFS(model) bfsSearcher.search() # search using dijkstra dijkstraSearcher = dijkstra.Dijkstra(model) dijkstraSearcher.search() # search using A* astarSearcher = astar.AStar(model) astarSearcher.search() # search using beam search beamSearcher = beams.Beams(model) beamSearcher.search() # search using Iterative Deepening idSearcher = iterativedeepening.IterDeep(model) idSearcher.search()
def loadnet(): p = problem.Problem((20, 40), 'mimo') # x = np.load('./trainingdata/1e5_mimo_c_4x8_x.npy') # y = np.load('./trainingdata/1e5_mimo_c_4x8_y.npy') x = np.load('./trainingdata/1e6_mimo_20x40_x.npy') y = np.load('./trainingdata/1e6_mimo_20x40_y.npy') props = dict(net_type='admm', num_stages=4, problem=p) a = load_net.get_net(4, 'untied', params_init=params_init) optimizer = tf.keras.optimizers.Adam(learning_rate=1e-4) # optimizer = tf.keras.optimizers.SGD(learning_rate=1e-2, momentum=0.9, nesterov=False) # a.compile(optimizer, loss=tf.keras.losses.MeanSquaredError()) a.compile(optimizer, loss=lista.MeanPercentageSquaredError()) a = train_net(a, p, (x, y), props, epochs=100) save_net(a, props) # x = np.load('./trainingdata/1e5_mimo_20x40_x.npy') # y = np.load('./trainingdata/1e5_mimo_20x40_y.npy') xhat = a.predict_on_batch(y) breakpoint() print( 10 * np.log10(np.mean(np.sum( (x - xhat)**2, axis=1) / np.sum(x**2, axis=1))))
def main_exec(options): App = problem.Problem() if not options.isTemplate: if not os.path.exists(options.projectfile): raise IOError, "Missing configuration file '%s'" % options.projectfile if App.load(options.projectfile): # create output folder if [output prefix] has a path output_dir = os.path.dirname( App.opts['configure']['output prefix']) if len(output_dir) > 0: if not os.path.exists(output_dir): os.mkdir(output_dir) App.setLogger('tevasim') else: raise RuntimeError("Invalid YAML configuration file") if options.isTemplate: template_options = { 'network': { 'epanet file': 'Net3.inp' }, 'scenario': { 'end time': 1440, 'location': ['NZD'], 'start time': 0, 'strength': 100, 'type': 'MASS' }, 'configure': { 'output prefix': 'Net3' } } App.saveTemplate(options.projectfile, template_options) else: App.run()
def main_exec(options): App = problem.Problem() if not options.isTemplate: if not os.path.exists(options.projectfile): raise IOError, "Missing configuration file '%s'" % options.projectfile if App.load(options.projectfile): # create output folder if [output prefix] is a path output_dir = App.opts['configure']['output prefix'] if output_dir: output_dir = os.path.dirname(output_dir) if len(output_dir) > 0: if not os.path.exists(output_dir): os.mkdir(output_dir) else: output_dir = '' App.setLogger('booster_msx') App.validate() else: raise RuntimeError("Invalid YAML configuration file") if options.isTemplate: template_options = { 'network': { 'epanet file': 'Net3.inp' }, 'scenario': { 'end time': 1440, 'location': [101], 'msx file': 'Net3_bio.msx', 'msx species': 'BIO', 'species': 'BIO', 'start time': 0, 'strength': 100, 'type': 'MASS' }, 'impact': { 'metric': ['MC'], 'msx species': 'BIO' }, 'booster msx': { 'decon species': 'CLF', 'detection': [111, 127, 179], 'duration': 600, 'max boosters': 2, 'response time': 0, 'strength': 4, 'toxin species': 'BIO', 'type': 'FLOWPACED' }, 'solver': { 'type': 'coliny_ea' }, 'configure': { 'output prefix': 'Net3' } } App.saveTemplate(options.projectfile, template_options) else: App.run()
def integer_division_problem(min=10, max=100): min = int(config['Division']['min']) max = int(config['Division']['max']) div1 = random.randint(min, max) div2 = random.randint(min, max) question = str(div1 * div2) + " / " + str(div1) + " = " answer = div2 return problem.Problem(question, answer, 'Integer Division')
def multiplication_problem(min=10, max=100): min = int(config['Multiplication']['min']) max = int(config['Multiplication']['max']) mul1 = random.randint(min, max) mul2 = random.randint(min, max) question = str(mul1) + " · " + str(mul2) + " = " answer = mul1 * mul2 return problem.Problem(question, answer, 'Multiplication')
def addition_problem(): min = int(config['Addition']['min']) max = int(config['Addition']['max']) add1 = random.randint(min, max) add2 = random.randint(min, max) question = str(add1) + " + " + str(add2) + " = " answer = add1 + add2 return problem.Problem(question, answer, 'Addition')
def main(): mini=np.inf popsize=1100 prob=problem.Problem(fitness_func=problem.katsuura,dim=10,prangetup=(-100,100)) popu=population.Population(prob,size=popsize) popu.randominit() genlim=20000 print(popu.poparr) print("thing starts here") newsel=misc.Selection(3) newcros=misc.Crossover(rate=0.8) #newmuta=misc.Mutation(rate=0.1) mrate=0.2 newterm=misc.Termination(0) print(popu.avg_fitness()) for i in range(genlim): lis=[] if (i==200): print("here it is-----------------------------------------") if (i==5000): print("here it is 500 ------------------------------------") mrate=0.02 #newmuta=misc.Mutation(rate=0.01) popu.set_fitarr() minic=min(popu.fitarr) if mini>minic: mini=minic mininp=popu.poparr[list(popu.fitarr).index(minic)] for tup in newsel.select_parent(popu): child1,child2=newcros.do_crossover(tup) child1=smallstepchange(child1,fac=3,rate=mrate) child2=smallstepchange(child2,fac=3,rate=mrate) lis.append(child1) lis.append(child2) del(popu) arr=np.array(lis) popu=population.Population(prob,poparr=arr,size=popsize) popu.set_fitarr() if np.all(popu.poparr==popu.poparr[0] ): break #these two conditionals have pen-ultimate IMPORTANCE, as my normalization fails heavily if all are same if np.all(popu.fitarr==popu.fitarr[0]): break print(popu.avg_fitness(),i) print(mini,list(mininp)) if newterm.terminate(popul=popu,generationnum=i,generationlim=genlim): print("breaking bad") break print(popu.poparr) print(popu.avg_fitness())
def main_exec(options): App = problem.Problem() if not options.isTemplate: if not os.path.exists(options.projectfile): raise IOError, "Missing configuration file '%s'" % options.projectfile if App.load(options.projectfile): # create output folder if [output prefix] has a path output_dir = os.path.dirname( App.opts['configure']['output prefix']) if len(output_dir) > 0: if not os.path.exists(output_dir): os.mkdir(output_dir) App.setLogger('flushing') App.validate() else: raise RuntimeError("Invalid YAML configuration file") if options.isTemplate: template_options = { 'network': { 'epanet file': 'Net3.inp' }, 'scenario': { 'end time': 1440, 'location': ['NZD'], 'start time': 0, 'strength': 100, 'type': 'MASS' }, 'impact': { 'metric': ['PE'], 'tai file': 'Net3_bio.tai' }, 'flushing': { 'detection': [111, 127, 179], 'close valves': { 'max pipes': 0, 'response time': 0.0 }, 'flush nodes': { 'duration': 480.0, 'feasible nodes': 'NZD', 'max nodes': 2, 'rate': 800.0, 'response time': 0.0 } }, 'solver': { 'type': 'StateMachineLS' }, 'configure': { 'output prefix': 'Net3' } } App.saveTemplate(options.projectfile, template_options) else: App.run()
def create(domain, cnf_or_dnf, formula_count, terms_per_formula, half_space_count, name): if cnf_or_dnf == "cnf": theory = generate_cnf(domain, formula_count, terms_per_formula, half_space_count) elif cnf_or_dnf == "dnf": theory = generate_dnf(domain, formula_count, terms_per_formula, half_space_count) elif cnf_or_dnf == "cnf_strict": theory = generate_strict_cnf(domain, formula_count, terms_per_formula, half_space_count) else: raise RuntimeError("cnf_or_dnf was neither 'cnf' nor 'dnf'") theory_problem = problem.Problem(domain, theory, name) return SyntheticProblem(theory_problem, cnf_or_dnf, formula_count, terms_per_formula, half_space_count)
def main(): popsize = 1100 prob = problem.Problem(fitness_func=problem.katsuura, dim=5, prangetup=(-100, 100)) popu = population.Population(prob, size=popsize) popu.randominit() genlim = 2000 print(popu.poparr) print("thing starts here") newsel = misc.Selection(1) newcros = misc.Crossover(rate=0.9) newmuta = misc.Mutation(rate=0.1) newterm = misc.Termination(1) print(popu.avg_fitness()) for i in range(genlim): lis = [] if (i == 200): print("here it is-----------------------------------------") if (i == 500): print("here it is 500 ------------------------------------") for tup in newsel.select_parent(popu): child1, child2 = newcros.do_crossover(tup) child1 = newmuta.mutate(child1, prob.rangetup, switch=1, iteri=i, switchiter=100, factup=(100, 1000)) child2 = newmuta.mutate(child2, prob.rangetup, switch=1, iteri=i, switchiter=100, factup=(100, 1000)) lis.append(child1) lis.append(child2) del (popu) arr = np.array(lis) popu = population.Population(prob, poparr=arr, size=popsize) popu.set_fitarr() if np.all(popu.poparr == popu.poparr[0]): break #these two conditionals have pen-ultimate IMPORTANCE, as my normalization fails heavily if all are same if np.all(popu.fitarr == popu.fitarr[0]): break print(popu.avg_fitness()) if newterm.terminate(popul=popu, iteri=i, lim=500): print("breaking bad") break print(popu.poparr) print(popu.avg_fitness())
def generateproblem(self, block, first=""): """Generate a problem using the problem class described in problem.py. """ landings, takeoffs = self.classify(block) planes = {} for typ in landings: a = ac.airplane(typ) planes[typ] = ac.airplane(typ) for typ in takeoffs: a = ac.airplane(typ) planes[typ] = ac.airplane(typ) p = pr.Problem(landings, planes, takeoffs, first) return p
def import_problem(name, filename): target_formula = smt.read_smtlib(filename) variables = target_formula.get_free_variables() var_names = [str(v) for v in variables] var_types = {str(v): v.symbol_type() for v in variables} var_domains = {str(v): (0, 200) for v in variables} # TODO This is a hack domain = problem.Domain(var_names, var_types, var_domains) target_problem = problem.Problem(domain, target_formula, name) return target_problem
def subtraction_problem(): min = int(config['Subtraction']['min']) max = int(config['Subtraction']['max']) sub1 = random.randint(min, max) sub2 = random.randint(min, max) if sub1 < sub2: sub1 = sub2 - sub1 sub2 = sub2 - sub1 sub1 = sub2 + sub1 question = str(sub1) + " - " + str(sub2) + " = " answer = sub1 - sub2 return problem.Problem(question, answer, 'Subtraction')
def __init__(self): self.chromosomes = 10 self.DNACount = 6 self.Maxgeneration = 30 self.currentGen = 0 self.mutationRate = .4 self.crossoverRate = .3 self.chromosomesList = [] self.fitness = [] self.prob = [] self.problem = problem.Problem() self.ans = [] self.bestG = [] self.meanG = [] self.worstG = []
def read(self, file): """Return a Problem from the information in the given file.""" statement = problem.Statement(self._read_title(), self._read_description(), self._read_input(), self._read_output(), self._read_examples(), self._read_aux_files(), self._read_tags(), self._read_tutorial(), self._read_notes()) evaluation = problem.Evaluation(self._read_tests(), self._read_solutions(), self._read_limits()) return problem.Problem(self._read_id(), statement, evaluation)
def validator(request): problem_dir = RUN_DIR / 'test/problems/identity' os.chdir(problem_dir) h = hashlib.sha256(bytes(Path().cwd())).hexdigest()[-6:] tmpdir = Path(tempfile.gettempdir()) / ('bapctools_' + h) tmpdir.mkdir(exist_ok=True) p = problem.Problem(Path('.'), tmpdir) validator = validate.OutputValidator(p, RUN_DIR / 'support' / request.param) print(util.ProgressBar.current_bar) bar = util.ProgressBar('build', max_len=1) validator.build(bar) bar.finalize() yield (p, validator) os.chdir(RUN_DIR)
def main(): start_time = time.time() prob = problem.Problem("maps/map_%d.input" % MAPNO) search_gap = SEARCH_SPACE // total_ranks search_start = search_gap * current_rank if current_rank == 0: if VERBOSE: print('Map', MAPNO, 'loaded. Search space:', SEARCH_SPACE) print('-' * 8) for i in range(1, total_ranks): comm.send(True, dest=i, tag=0) # start signal else: comm.recv(source=0, tag=0) # wait for start signal if VERBOSE: print('running from', search_start, 'to', search_start + search_gap, 'on rank', current_rank) sys.stdout.flush() this_best = run_subset_dc(prob, search_start, search_gap, current_rank) if CONTROL_DISTRIBUTIONS else \ run_subset(prob, search_start, search_gap, current_rank) if current_rank > 0: comm.send(this_best, dest=0, tag=420) else: # consolidate best = this_best for i in range(1, total_ranks): rec_best = comm.recv(source=i, tag=420) if rec_best[0] < best[0]: best = rec_best if VERBOSE: print('-' * 8) if CONTROL_DISTRIBUTIONS: print('Done. Best score:', best[0], 'seed:', best[1], 'distro:', best[2]) if WRITE_SOLUTION: _run_solve(prob, best[1], best[2]) else: print('Done. Best score:', best[0], 'seed:', best[1]) if WRITE_SOLUTION: _run_solve(prob, best[1]) if WRITE_SOLUTION: prob.write_solution('maps/map_%d.output' % MAPNO) return time.time() - start_time # runtime
def build_compound_problem(fnames): """mirrors build_problem from check for multiple functions""" printout('Building compound problem for %s' % fnames) last_compound_problem_req[0] = list(fnames) p = problem.Problem(None, name=', '.join(fnames)) fun_tag_pairs = [] all_tags = {} for (i, fn) in enumerate(fnames): i = len(fnames) - i [pair] = pairings[fn] next_tags = {} scripts = get_problem_inline_scripts(pair) for (pair_tag, fname) in pair.funs.items(): tag = '%s_%d_%s' % (fname, i, pair_tag) tag = syntax.fresh_name(tag, all_tags) next_tags[pair_tag] = tag p.add_entry_function(functions[fname], tag) p.hook_tag_hints[tag] = pair_tag p.replay_inline_script(tag, scripts[pair_tag]) fun_tag_pairs.append((next_tags, pair)) p.pad_merge_points() p.do_analysis() free_hyps = [] for (tags, pair) in fun_tag_pairs: (inp_eqs, _) = pair.eqs free_hyps += check.inst_eqs(p, (), inp_eqs, tags) err_vis_opts = rep_graph.vc_options([0, 1, 2], [1]) err_vis_vc = tuple([(n, err_vis_opts) for n in p.loop_heads() if p.node_tags[n][0] == tags['C']]) err_vis = (('Err', err_vis_vc), tags['C']) free_hyps.append(rep_graph.pc_false_hyp(err_vis)) addr_map = {} for n in p.nodes: if not p.node_tags[n][0].endswith('_ASM'): continue if type(p.node_tags[n][1]) == tuple: (fname, data) = p.node_tags[n][1] if (logic.is_int(data) and is_addr(data) and not fname.startswith("instruction'")): assert data not in addr_map, data addr_map[data] = n return (p, free_hyps, addr_map, fun_tag_pairs)
def load_net(folder, scen, num_layers, tied, *args, params=None): # args = ln.get_args() # scen = args[0].scen # folder = './nets/' + args[0].folder A = np.load(folder + '/A.npy') n1,n2 = A.shape p = problem.Problem((n1,n2-n1), scen, partition=True, N_part=n2-n1) if params is None: a = admm.ADMMNet(p, num_layers, tied, *args) else: a = admm.ADMMNet(p, num_layers, tied, *args, params_init=params) obj = a.load_weights(folder + '/weights') obj.expect_partial() return a, p
def get_fake_problems(*args, **kwargs): res = [] for pdata in _data: p = problem.Problem(pdata['type'], pdata['reason']) for field in set(pdata.keys()) - set(['type', 'reason']): if field == 'path': # path is immutable continue setattr(p, field, pdata[field]) setattr(p, '_persisted', True) setattr(p, '_probdir', pdata['path']) setattr(p, '_proxy', FakeProxy()) res.append(p) return res
def useRandomRestartHillClimbingApporach(noofQueensOnBoard, choiceOfTries): total = 0 fail_steps = 0 sample_sequences = [] sample_problems = [] for i in range(choiceOfTries): print("Try #{}".format(i + 1)) result = calculationperformer.random_restart( problem.Problem(noofQueensOnBoard).__class__, noofQueensOnBoard) total += result['outcome'] fail_steps += len(result['solution']) if result['outcome']: if (result['problem'] not in sample_problems) and (len(sample_problems) < 4): sample_problems.append(result['problem']) sample_sequences.append(result['solution']) printFinalResult('Random Restart Hill Climbing', sample_sequences, choiceOfTries, total)
def modify(contestID): contest_ID = contestID print("modify contest page.") if request.method == "GET": contents = session.query( pm.Problem).filter(pm.Problem.contestID == contest_ID).all() return render_template("modify.html", cont=contents) else: print("modify this contest.") session.query( pm.Problem).filter(pm.Problem.contestID == contest_ID).delete() session.commit() contest_name = request.form["contest_name"] start_date = request.form["start_date"] start_time = request.form["start_time"] end_date = request.form["end_date"] end_time = request.form["end_time"] problems_url = request.form.getlist("problem_url") pr = session.query(pm.Problem).all() for url in problems_url: if len(url) == 0: continue pr = pm.Problem() pr.problemURL = url pr.problem = url.split('/')[5] + url.split('/')[6] pr.contest = contest_name pr.contestID = contest_ID pr.participant = "springroll" pr.start_time = start_date + " " + start_time + ":00" pr.end_time = end_date + " " + end_time + ":00" pr.penalty = 0 pr.last_updated = start_date + " " + start_time + ":00" print(url) session.add(pr) session.commit() return redirect(url_for('contest', contestID=contest_ID))
def useSteepHillClimbingApproach(noofQueensOnBoard, choiceOfTries): # Steepest Ascent without sideway move method total = 0 fail_steps = 0 sample_sequences = [] sample_problems = [] for i in range(choiceOfTries): print("Try #{}".format(i + 1)) result = calculationperformer.steepest_ascent( problem.Problem(noofQueensOnBoard)) total += result['outcome'] fail_steps += len(result['solution']) if result['outcome']: if (result['problem'] not in sample_problems) and (len(sample_problems) < 4): sample_problems.append(result['problem']) sample_sequences.append(result['solution']) printFinalResult('Steepest Hill Climbing', sample_sequences, choiceOfTries, total)