示例#1
0
def pre(cfg):
    name = cfg["method"]
    sigma = cfg["sigma"]
    scale = cfg["scale"]
    K = cfg["K"]

    kernel = RBFKernel(sigma=sigma,scale=scale)
    fastLogDet = FastIVM(K, kernel, 1.0)

    if name == "Greedy":
        opt = Greedy(K, fastLogDet)
    if name == "IndependentSetImprovement":
        opt = IndependentSetImprovement(K, fastLogDet)
    elif name == "Random":
        opt = Random(K, fastLogDet, cfg["run_id"])
    elif name == "SieveStreaming":
        e = cfg["epsilon"]
        opt = SieveStreaming(K, fastLogDet, 1.0, e)
    elif name == "SieveStreaming++":
        e = cfg["epsilon"]
        opt = SieveStreamingPP(K, fastLogDet, 1.0, e)
    elif name == "Salsa":
        e = cfg["epsilon"]
        opt = Salsa(K, fastLogDet, 1.0, e)
    elif name == "ThreeSieves":
        e = cfg["epsilon"]
        T = cfg["T"]
        opt = ThreeSieves(K, fastLogDet, 1.0, e, "sieve", T)
    return opt
    def __call__(self, X):
        return ivm(X)
    
# Generate some dummy data
X = [
    [0, 0],
    [1, 1],
    [0.5, 1.0],
    [1.0, 0.5],
    [0, 0.5],
    [0.5, 1],
    [0.0, 1.0],
    [1.0, 0.]
]

K = 3
# Test a few optimizers with their default parameters on the FastLogdet class from above
optimizers = [SieveStreaming, Greedy, Random, ThreeSieves] 
for clazz in optimizers:
    kernel = RBFKernel(sigma=1,scale=1)
    ivm = FastLogdet(kernel = kernel, sigma = 1.0)

    opt = clazz(K, ivm)
    opt.fit(X)
    fval = opt.get_fval()
    solution = np.array(opt.get_solution())

    print("Found a solution with fval = {}".format(fval))
    print(solution)