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_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
def generate_random_plinear_preference_function(crits, ap_worst, ap_best): cfs = CriteriaFunctions() for crit in crits: worst = ap_worst.performances[crit.id] best = ap_best.performances[crit.id] if crit.direction == -1: worst, best = best, worst r = sorted([random.uniform(worst, best) for i in range(2)]) a = Point(float("-inf"), 0) b = Point(r[0],0) c = Point(r[1],1) d = Point(float("inf"), 1) f = PiecewiseLinear([Segment('s1', a, b), Segment('s2', b, c), Segment('s3', c, d)]) cf = CriterionFunction(crit.id, f) cfs.append(cf) return cfs
from pymcda.types import CriterionValue, CriteriaValues from pymcda.types import PiecewiseLinear, Segment, Point from pymcda.types import CriterionFunction, CriteriaFunctions c1 = Criterion("c1") c2 = Criterion("c2") c3 = Criterion("c3") c = Criteria([c1, c2, c3]) cv1 = CriterionValue("c1", 0.5) cv2 = CriterionValue("c2", 0.25) cv3 = CriterionValue("c3", 0.25) cvs = CriteriaValues([cv1, cv2, cv3]) f1 = PiecewiseLinear([ Segment('s1', Point(0, 0), Point(2.5, 0.2)), Segment('s2', Point(2.5, 0.2), Point(5, 1), True, True) ]) f2 = PiecewiseLinear([ Segment('s1', Point(0, 0), Point(2.5, 0.8)), Segment('s2', Point(2.5, 0.8), Point(5, 1), True, True) ]) f3 = PiecewiseLinear([ Segment('s1', Point(0, 0), Point(2.5, 0.5)), Segment('s2', Point(2.5, 0.5), Point(5, 1), True, True) ]) cf1 = CriterionFunction("c1", f1) cf2 = CriterionFunction("c2", f2) cf3 = CriterionFunction("c3", f3) cfs = CriteriaFunctions([cf1, cf2, cf3]) app = QtGui.QApplication(sys.argv)