Example #1
0
 def cgtostring(vec):
     tmpstr = []
     for c in vec:
         if np.absolute(c) < self.prec:
             tmpstr.append(" ")
             continue
         tmpstr.append("$%s$" % (lu.latexify(c)))
     tmpstr = " & ".join(tmpstr)
     return tmpstr
 def tostring(vec, j):
     tmpstr = []
     for m, c in enumerate(vec):
         if np.absolute(c) < self.prec:
             continue
         tmpstr.append("%s |%d,%+d\\rangle" %
                       (lu.latexify(c), j, m - j))
     tmpstr = " + ".join(tmpstr)
     tmpstr = " ".join(["$", tmpstr, "$"])
     return tmpstr
Example #3
0
    K = 4  # Number of trajectories
    n = 20  # dim of solution space
    m = 2  # dim of objective space
    rs = circle_points(K)  # preference

    pmtl_refs = 1. / rs
    pf = create_pf()

    methods = ["EPO", "PMTL"]
    colors = list(mcolors.TABLEAU_COLORS.values())
    print(colors)
    markers = {"EPO": '*', "PMTL": '^'}
    msz = {"EPO": 90, "PMTL": 35}

    for method in methods:
        latexify(fig_width=2.25, fig_height=1.8)
        fig, ax = plt.subplots()
        fig.subplots_adjust(left=.12, bottom=.12, right=.975, top=.975)

        label = 'Pareto Front' if init_type == "easy" and method == "EPO" else ''
        ax.plot(pf[:, 0], pf[:, 1], lw=2, c='k', label=label)

        for k, r in enumerate(rs):
            if init_type == "hard":
                x0 = np.random.uniform(0.15 * (-1)**(k), .5 * (-1)**(k), n)
            else:
                x0 = np.random.uniform(-0.4, 0.4, n)
            l0, _ = concave_fun_eval(x0)
            label = r'$l(\theta^0)$' if init_type == "hard" and k == 0 and method == "PMTL" else ''
            plt.scatter([l0[0]], [l0[1]],
                        c=colors[k],
Example #4
0
from problems.toy_biobjective import circle_points, concave_fun_eval, create_pf
from solvers import epo_search, pareto_mtl_search, linscalar, moo_mtl_search, meta_search

import matplotlib.pyplot as plt
from latex_utils import latexify

if __name__ == '__main__':
    K = 4  # Number of trajectories
    n = 20  # dim of solution space
    m = 2  # dim of objective space
    rs = circle_points(K)  # preference

    pmtl_K = 5
    pmtl_refs = circle_points(pmtl_K, 0, np.pi / 2)
    methods = ['EPO', 'PMTL', 'MOOMTL', 'LinScalar', "Meta"]
    latexify(fig_width=2., fig_height=1.55)
    ss, mi = 0.1, 100
    pf = create_pf()
    for method in methods:
        fig, ax = plt.subplots()
        fig.subplots_adjust(left=.12, bottom=.12, right=.97, top=.97)
        ax.plot(pf[:, 0], pf[:, 1], lw=2, c='k', label='Pareto Front')
        last_ls = []
        for k, r in enumerate(rs):
            r_inv = 1. / r
            ep_ray = 1.1 * r_inv / np.linalg.norm(r_inv)
            ep_ray_line = np.stack([np.zeros(m), ep_ray])
            label = r'$r^{-1}$ ray' if k == 0 else ''
            ax.plot(ep_ray_line[:, 0],
                    ep_ray_line[:, 1],
                    color='k',
Example #5
0
def case1_satisfyingMCF():
    n = args.n  # dim of solution space
    m = args.m  # dim of objective space
    ##construct x0
    x0 = np.zeros(n)
    x0[range(0, n, 2)] = -0.2
    x0[range(1, n, 2)] = -0.2
    eps_set = [0.8, 0.6, 0.4, 0.2]
    color_set = ["c", "g", "orange", "b"]
    latexify(fig_width=2.2, fig_height=1.8)
    l0, _ = concave_fun_eval(x0)
    max_iters = args.max_iters
    relax = True
    pf = create_pf()
    fig = plt.figure()
    fig.subplots_adjust(left=.12, bottom=.12, right=.9, top=.9)
    label = 'Pareto\nFront' if relax else ''
    plt.plot(pf[:, 0], pf[:, 1], lw=2.0, c='k', label=label)
    label = r'$l(\theta^0)$'
    plt.scatter([l0[0]], [l0[1]], c='r', s=40)
    plt.annotate(label, xy=(l0[0] + 0.03, l0[1]), xytext=(l0[0] + 0.03, l0[1]))
    for idx, eps0 in enumerate(eps_set):
        if eps0 == 0.2:
            eps_plot = np.array([[i * 0.1 * 0.903, 0.2] for i in range(11)])
            plt.plot(eps_plot[:, 0],
                     eps_plot[:, 1],
                     color="gray",
                     label=r'$\epsilon$',
                     lw=1,
                     ls='--')
        elif eps0 == 0.4:
            eps_plot = np.array([[i * 0.1 * 0.807, 0.4] for i in range(11)])
            plt.plot(eps_plot[:, 0],
                     eps_plot[:, 1],
                     color="gray",
                     lw=1,
                     ls='--')
        elif eps0 == 0.6:
            eps_plot = np.array([[i * 0.1 * 0.652, 0.6] for i in range(11)])
            plt.plot(eps_plot[:, 0],
                     eps_plot[:, 1],
                     color="gray",
                     lw=1,
                     ls='--')
        elif eps0 == 0.8:
            eps_plot = np.array([[i * 0.1 * 0.412, 0.8] for i in range(11)])
            plt.plot(eps_plot[:, 0],
                     eps_plot[:, 1],
                     color="gray",
                     lw=1,
                     ls='--')
        else:
            print("error eps0")
            exit()
        c = color_set[idx]
        _, res = hco_search(concave_fun_eval,
                            x=x0,
                            deltas=[0.5, 0.5],
                            para_delta=0.5,
                            lr_delta=args.lr_delta,
                            relax=False,
                            eps=[eps0, args.eps1, args.eps2],
                            max_iters=max_iters,
                            n_dim=args.n,
                            step_size=args.step_size,
                            grad_tol=args.grad_tol,
                            store_xs=args.store_xs)
        ls = res['ls']
        alpha = 1.0
        zorder = 1
        # plt.plot(ls[:, 0], ls[:, 1], c=c, lw=2.0, alpha=alpha, zorder=zorder)
        plt.plot(ls[:, 0], ls[:, 1], c=c, lw=2.0)
        print(ls[-1])
        plt.scatter(ls[[-1], 0], ls[[-1], 1], c=c, s=40)
    plt.xlabel(r'$l_1$')
    plt.ylabel(r'$l_2$', rotation="horizontal")
    plt.legend(loc='lower left', handletextpad=0.3, framealpha=0.9)
    ax = plt.gca()
    ax.xaxis.set_label_coords(1.05, -0.02)
    ax.yaxis.set_label_coords(-0.02, 1.02)
    ax.spines['right'].set_color('none')
    ax.spines['top'].set_color('none')
    plt.savefig('figures/satifying' + '.pdf')
    plt.close()
Example #6
0
from latex_utils import latexify
import os


baseline = "indiv"
methods = ["EPO", "PMTL", "LinScalar"]
markers = {"EPO": "*", "PMTL": "^", "LinScalar": "s", "indiv": "o"}
marker_szs = {"EPO": 5, "PMTL": 3.5, "LinScalar": 2}
colors = {"EPO": "g", "PMTL": "b", "LinScalar": "r"}
dataset = "rf1"
model = 'fnn'
niters, nprefs = 100, 10
folder = f"results"
m = 8

latexify(fig_width=3.3, fig_height=1.8)
# Baseline stem plot
file = f"{baseline}_{dataset}_{model}_{niters}.pkl"
results = pkl.load(open(os.path.join(folder, file), 'rb'))
baseline_l = []
for j in results:
    res = results[j]["res"]
    lj = res["training_losses"][-1][j] / (m * 1000)
    baseline_l.append(lj)

markerline, stemlines, baseline = plt.stem(baseline_l, label='Baseline',
                                           use_line_collection=True,
                                           basefmt=' ')
plt.setp(stemlines, 'linewidth', 8, 'alpha', 0.5,
         'color', 'gray', 'zorder', -5)
plt.setp(markerline, 'ms', 8, 'color', 'gray', 'zorder', -5, 'marker', '_')
Example #7
0
from latex_utils import latexify
import os


baseline = "indiv"
methods = ["EPO", "PMTL", "LinScalar"]
markers = {"EPO": "*", "PMTL": "^", "LinScalar": "s", "indiv": "o"}
marker_szs = {"EPO": 5, "PMTL": 3.5, "LinScalar": 2}
colors = {"EPO": "g", "PMTL": "b", "LinScalar": "r"}
dataset = "emotion"
model = 'fnn'
niters, nprefs = 200, 10
folder = f"results"
m = 6

latexify(fig_width=4, fig_height=1.8)
fig = plt.figure()
fig.subplots_adjust(left=0.12, bottom=0.1, right=.78, top=1)
# Baseline stem plot
file = f"{baseline}_{dataset}_{model}_{niters}.pkl"
results = pkl.load(open(os.path.join(folder, file), 'rb'))
baseline_l = []
for j in results:
    res = results[j]["res"]
    lj = res["training_losses"][-1][j] / m
    baseline_l.append(lj)

markerline, stemlines, baseline = plt.stem(baseline_l, label='Baseline',
                                           use_line_collection=True,
                                           basefmt=' ')
plt.setp(stemlines, 'linewidth', 8, 'alpha', 0.5, 'color', 'gray', 'zorder', -5)
Example #8
0
        xs, ys, zs = proj3d.proj_transform(xs3d, ys3d, zs3d, renderer.M)
        self.set_positions((xs[0], ys[0]), (xs[1], ys[1]))
        FancyArrowPatch.draw(self, renderer)


if __name__ == '__main__':
    K = 3  # Number of trajectories
    n = 20  # dim of solution space
    m = 3  # dim of objective space
    rs = sphere_points(K)
    max_iters = 500
    ss = [0.05, 0.2, 0.05, 0.05, 0.02, 0.05]
    colors = list(mcolors.TABLEAU_COLORS.values())
    pf, pf_tri, ls, tri = create_pf()

    latexify(fig_width=4., fig_height=3.6)
    params = {
        'axes.labelsize': 16,
        'axes.titlesize': 16,
        'font.size': 15,
        'legend.fontsize': 18,
        'xtick.labelsize': 12,
        'ytick.labelsize': 12
    }
    mpl.rcParams.update(params)

    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.plot_trisurf(pf[:, 0],
                    pf[:, 1],
                    pf[:, 2],
Example #9
0
File: utils.py Project: fairfl/FCFL
        a.append(a0)
    if side in ["right", "both"]:
        a1 = ax.annotate(caps[1],
                         xy=(xdata[1], ydata[1]),
                         zorder=2,
                         **anno_args)
        a.append(a1)
    return (line, tuple(a))


if __name__ == '__main__':
    theta = np.linspace(-4, 4, 150)
    l1 = [f1(np.array([x])) for x in theta]
    l2 = [f2(np.array([x])) for x in theta]

    latexify(fig_width=3, fig_height=3.0)
    fig, ax = plt.subplots()
    fig.subplots_adjust(left=0.025, bottom=.15, right=.975, top=.975)
    ax.spines['left'].set_position('zero')
    ax.spines['right'].set_color('none')  # turn off the right spine/ticks
    ax.spines['top'].set_color('none')  # turn off the top spine/ticks
    ax.xaxis.tick_bottom()

    ax.plot(theta, l1, color="black", label=r'$l_1(\theta)$', lw=2)
    ax.plot(theta, l2, color="red", label=r'$l_2(\theta)$', lw=2)

    # Comment out the twin axis code below for ppt
    axt = ax.twinx()
    axt.axis("off")
    # add_interval(axt, (-3, -1.05), (0.02, 0.02), "r", "()",
    #              r"$\theta^0 \preccurlyeq -\mathbf{1}/\sqrt{n}$ or " +
        if k != baseline)

    data[dataset]["baseline_loss"] = [
        ([0, max_task1_loss + 0.05], [base_task2_loss, base_task2_loss]),
        ([base_task1_loss, base_task1_loss], [0, max_task2_loss + 0.05]),
    ]

    data[dataset]["baseline_acc"] = [
        ([0, 1], [base_task2_acc, base_task2_acc]),
        ([base_task1_acc, base_task1_acc], [0, 1]),
    ]

    # setup preferences
    data[dataset]["rs"] = data[dataset][methods[0]]["rs"]

latexify(fig_width=3, fig_height=2)
for dataset in datasets:
    fig, ax = plt.subplots(1, 1)
    x_min, x_max = data[dataset]["baseline_loss"][0][0]
    y_min, y_max = data[dataset]["baseline_loss"][1][1]
    ax.set_xlim(left=x_min, right=x_max + 0.05)
    ax.set_ylim(bottom=y_min, top=y_max + 0.05)

    ax.set_xlabel(r"Task 1 Loss")
    ax.set_ylabel(r"Task 2 Loss")
    for i, (xs, ys) in enumerate(data[dataset]["baseline_loss"]):
        ax.plot(xs, ys, lw=1, alpha=0.3, c="k")

    colors = []
    for i, r in enumerate(data[dataset]["rs"]):
        label = r"Preference" if i == 0 else ""