def solve_multiple_frontier(saving=False): bbax=bax() frontiers = {fn: Frontier(fn) for fn in SWB_functions} best = {fn: None for fn in objective_functions} indiv = [Person(theta[i],maxCoord = sizeRegion) for i in range(nIndiv)] fac = [Facility(maxCoord = sizeRegion) for i in range(nFac)] if loading: dist = pickle.load(open(dumpFileName,'rb')) else: dist = np.array([[person.dist(facility) for facility in fac] for person in indiv]) total_subsets = bbax.enumerator.choose(nFac,nSelectedFac) for ind, gp in enumerate(bbax.bax_gen(nFac,nSelectedFac)): if ind % 1000 == 0: num_in_each_frontier = [len(frontier.instances) for fn, frontier in frontiers.items()] print(f"Processed {ind} subsets out of {total_subsets}") print(num_in_each_frontier) gp = gp.toList() r = [min((dist[i][j] for j in gp)) for i in range(nIndiv)] # print(len(r)) yv = np.zeros((nFac),dtype=int) yv[gp] = 1 instance = Instance(rvals=r, yvals=yv) for fn,frontier in frontiers.items(): frontier.consider(instance) best = {fn : instance if instance.beats(best_instance,fn) else best_instance for fn,best_instance in best.items()} # if saving: # save_data(dat) return best,frontiers,dist
def solve_enumerative(obj_fn): bbax = bax() yvals = np.zeros((nFac), dtype=int) rvals = np.zeros((nIndiv)) uvals = np.full((nIndiv), -np.inf) for ind, gp in enumerate(bbax.bax_gen(nFac, nSelectedFac)): gp = gp.toList() xlist = [ gp[np.argmin([dist[i][j] for j in gp])] for i in range(nIndiv) ] r = [dist[i][xlist[i]] for i in range(nIndiv)] u = obj_fn(r) print(f"Obj is {sum(u)}") if sum(u) > sum(uvals): uvals = u rvals = r yvals = np.zeros((nFac), dtype=int) yvals[gp] = 1 print(f"Best obj is {sum(uvals)}") fstar = sum(uvals) prob_success = [ 1 / (1 + exp(-beta[0] - beta[1] * theta[i] - beta[2] * rvals[i])) for i in range(nIndiv) ] print(f"Solved enumeratively") return yvals, rvals, uvals, prob_success, fstar, obj_fn.extra_info
def solve_multiple_frontier(saving=False): bbax = bax() frontiers = { fn: [Frontier(fn) for trial in range(nTrials)] for fn in SWB_functions } all_best = {fn: None for fn in objective_functions} for trial in range(nTrials): best = {fn: None for fn in objective_functions} indiv = [Person(theta[i], maxCoord=sizeRegion) for i in range(nIndiv)] fac = [Facility(maxCoord=sizeRegion) for i in range(nFac)] dist = np.array([[person.dist(facility) for facility in fac] for person in indiv]) for ind, gp in enumerate(bbax.bax_gen(nFac, nSelectedFac)): gp = gp.toList() r = [min((dist[i][j] for j in gp)) for i in range(nIndiv)] # print(len(r)) yv = np.zeros((nFac), dtype=int) yv[gp] = 1 instance = Instance(rvals=r, yvals=yv) for fn in frontiers: frontier = frontiers[fn][trial] if not frontier.dominates(instance): frontier.append(instance) # for fn, best_instance in best.items(): # if instance.beats(best_instance,fn): # best[fn] = instance best = { fn: instance if instance.beats(best_instance, fn) else best_instance for fn, best_instance in best.items() } for fn, combined_instance in all_best.items(): all_best[fn] = best[fn].combine(combined_instance) print(f"Solved {trial+1}/{nTrials} enumeratively") # if saving: # save_data(dat) return all_best, frontiers
def solve_multiple_enumerative(saving=False): bbax = bax() all_yvals = {fn: [] for fn in objective_functions} all_rvals = {fn: [] for fn in objective_functions} all_uvals = {fn: [] for fn in objective_functions} all_fstar = {fn: [] for fn in objective_functions} all_prob_success_vals = {fn: [] for fn in objective_functions} for trial in range(nTrials): indiv = np.random.uniform(0, sizeRegion, (nIndiv, 2)) fac = np.random.uniform(0, sizeRegion, (nFac, 2)) dist = np.array([[ (indiv[i, 0] - fac[j, 0])**2 + (indiv[i, 1] - fac[j, 1])**2 for j in range(0, nFac) ] for i in range(0, nIndiv)]) # nIndiv X nFac yvals = {fn: np.zeros((nFac), dtype=int) for fn in objective_functions} rvals = {fn: np.zeros((nFac), dtype=int) for fn in objective_functions} uvals = {fn: np.full((nIndiv), -np.inf) for fn in objective_functions} fstar = {fn: 0 for fn in objective_functions} prob_success_vals = {fn: 0 for fn in objective_functions} for ind, gp in enumerate(bbax.bax_gen(nFac, nSelectedFac)): gp = gp.toList() # print(locals()) # print(f"gp: {gp}, gp_dists: {[dist[i][j] for j in gp]}, ") xlist = [ gp[np.argmin([dist[i][j] for j in gp])] for i in range(nIndiv) ] r = [dist[i][xlist[i]] for i in range(nIndiv)] for f in objective_functions: u = f(r) # u = [-np.log(1+np.exp(-beta[0] - beta[1]*theta[i] - beta[2]*r[i])) for i in range(nIndiv)] if sum(u) > sum(uvals[f]): uvals[f] = u rvals[f] = r yvals[f] = np.zeros((nFac), dtype=int) yvals[f][gp] = 1 for f in objective_functions: fstar[f] = sum(uvals[f]) prob_success_vals[f] = prob_success(rvals[f], beta, theta) all_yvals[f].extend(yvals[f]) all_rvals[f].extend(rvals[f]) all_uvals[f].extend(uvals[f]) all_fstar[f].append(fstar[f]) all_prob_success_vals[f].extend(prob_success_vals[f]) print(f"Solved {trial+1}/{nTrials} enumeratively") for f in objective_functions: all_yvals[f] = tuple( all_yvals[f]) # so they are hashable later for grouping plots dat = { "yvals": all_yvals, "rvals": all_rvals, "uvals": all_uvals, "fstar": all_fstar, "prob_success_vals": all_prob_success_vals } if saving: save_data(dat) return dat