def generate_random_piecewise_linear(gi_min=0, gi_max=1, n_segments=3, ui_min=0, ui_max=1, k=3): d = ui_max - ui_min r = [ui_min + d * round(random.random(), k) for i in range(n_segments - 1)] r.append(ui_min) r.append(ui_max) r.sort() interval = (gi_max - gi_min) / n_segments f = PiecewiseLinear([]) for i in range(n_segments): a = Point(round(gi_min + i * interval, k), r[i]) b = Point(round(gi_min + (i + 1) * interval, k), r[i + 1]) s = Segment("s%d" % (i + 1), a, b) f.append(s) s.p1_in = True s.p2_in = True return f
def solve_cplex(self, aa, pt): self.lp.solve() status = self.lp.solution.get_status() if status != self.lp.solution.status.optimal: raise RuntimeError("Solver status: %s" % status) obj = self.lp.solution.get_objective_value() cfs = CriteriaFunctions() cvs = CriteriaValues() for cs in self.cs: cv = CriterionValue(cs.id, 1) cvs.append(cv) nseg = cs.value x_points = range(nseg) p1 = Point(self.points[cs.id][0], 0) ui = 0 f = PiecewiseLinear([]) for i in x_points: uivar = 'w_' + cs.id + "_%d" % (i + 1) ui += self.lp.solution.get_values(uivar) x = self.points[cs.id][i + 1] p2 = Point(x, ui) s = Segment("s%d" % (i + 1), p1, p2) f.append(s) p1 = p2 s.p1_in = True s.p2_in = True cf = CriterionFunction(cs.id, f) cfs.append(cf) cat = {v: k for k, v in self.cat.items()} catv = CategoriesValues() ui_a = 0 for i in range(1, len(cat)): ui_b = self.lp.solution.get_values("u_%d" % i) catv.append(CategoryValue(cat[i], Interval(ui_a, ui_b))) ui_a = ui_b catv.append(CategoryValue(cat[i + 1], Interval(ui_a, 1))) return obj, cvs, cfs, catv
def solve_cplex(self, aa, pt): self.lp.solve() status = self.lp.solution.get_status() if status != self.lp.solution.status.optimal: raise RuntimeError("Solver status: %s" % status) obj = self.lp.solution.get_objective_value() cfs = CriteriaFunctions() cvs = CriteriaValues() for cs in self.cs: cv = CriterionValue(cs.id, 1) cvs.append(cv) nseg = cs.value x_points = range(nseg) p1 = Point(self.points[cs.id][0], 0) ui = 0 f = PiecewiseLinear([]) for i in x_points: uivar = 'w_' + cs.id + "_%d" % (i + 1) ui += self.lp.solution.get_values(uivar) x = self.points[cs.id][i + 1] p2 = Point(x, ui) s = Segment("s%d" % (i + 1), p1, p2) f.append(s) p1 = p2 s.p1_in = True s.p2_in = True cf = CriterionFunction(cs.id, f) cfs.append(cf) cat = {v: k for k, v in self.cat.items()} catv = CategoriesValues() ui_a = 0 for i in range(1, len(cat)): ui_b = self.lp.solution.get_values("u_%d" % i) catv.append(CategoryValue(cat[i], Interval(ui_a, ui_b))) ui_a = ui_b catv.append(CategoryValue(cat[i + 1], Interval(ui_a, 1))) return obj, cvs, cfs, catv
def solve_glpk(self, aa, pt): self.lp.solve() status = self.lp.status() if status != 'opt': raise RuntimeError("Solver status: %s" % self.lp.status()) obj = self.lp.vobj() cfs = CriteriaFunctions() cvs = CriteriaValues() for cid, points in self.points.items(): cv = CriterionValue(cid, 1) cvs.append(cv) p1 = Point(self.points[cid][0], 0) ui = 0 f = PiecewiseLinear([]) for i in range(len(points) - 1): uivar = 'w_' + cid + "_%d" % (i + 1) ui += self.w[cid][i].primal p2 = Point(self.points[cid][i + 1], ui) s = Segment(p1, p2) f.append(s) p1 = p2 s.p2_in = True cf = CriterionFunction(cid, f) cfs.append(cf) cat = {v: k for k, v in self.cat.items()} catv = CategoriesValues() ui_a = 0 for i in range(0, len(cat) - 1): ui_b = self.u[i].primal catv.append(CategoryValue(cat[i + 1], Interval(ui_a, ui_b))) ui_a = ui_b catv.append(CategoryValue(cat[i + 2], Interval(ui_a, 1))) return obj, cvs, cfs, catv
def solve_glpk(self, aa, pt): self.lp.solve() status = self.lp.status() if status != 'opt': raise RuntimeError("Solver status: %s" % self.lp.status()) obj = self.lp.vobj() cfs = CriteriaFunctions() cvs = CriteriaValues() for cid, points in self.points.items(): cv = CriterionValue(cid, 1) cvs.append(cv) p1 = Point(self.points[cid][0], 0) ui = 0 f = PiecewiseLinear([]) for i in range(len(points) - 1): uivar = 'w_' + cid + "_%d" % (i + 1) ui += self.w[cid][i].primal p2 = Point(self.points[cid][i + 1], ui) s = Segment(p1, p2) f.append(s) p1 = p2 s.p2_in = True cf = CriterionFunction(cid, f) cfs.append(cf) cat = {v: k for k, v in self.cat.items()} catv = CategoriesValues() ui_a = 0 for i in range(0, len(cat) - 1): ui_b = self.u[i].primal catv.append(CategoryValue(cat[i + 1], Interval(ui_a, ui_b))) ui_a = ui_b catv.append(CategoryValue(cat[i + 2], Interval(ui_a, 1))) return obj, cvs, cfs, catv
def generate_random_piecewise_linear(gi_min = 0, gi_max = 1, n_segments = 3, ui_min = 0, ui_max = 1, k = 3): d = ui_max - ui_min r = [ ui_min + d * round(random.random(), k) for i in range(n_segments - 1) ] r.append(ui_min) r.append(ui_max) r.sort() interval = (gi_max - gi_min) / n_segments f = PiecewiseLinear([]) for i in range(n_segments): a = Point(round(gi_min + i * interval, k), r[i]) b = Point(round(gi_min + (i + 1) * interval, k), r[i + 1]) s = Segment("s%d" % (i + 1), a, b) f.append(s) s.p1_in = True s.p2_in = True return f