예제 #1
0
파일: solvers.py 프로젝트: qtothec/pyomo
def initialize(**kwds):
    obj = Options(**kwds)
    #
    # Set obj.available
    #
    opt = None
    try:
        opt = SolverFactory(obj.name, solver_io=obj.io)
    except:
        pass
    if opt is None or isinstance(opt, UnknownSolver):
        obj.available = False
    elif (obj.name == "gurobi") and (not GUROBISHELL.license_is_valid()):
        obj.available = False
    elif (obj.name == "baron") and (not BARONSHELL.license_is_valid()):
        obj.available = False
    else:
        obj.available = (opt.available(exception_flag=False)) and (
            (not hasattr(opt, "executable")) or (opt.executable() is not None)
        )
    #
    # Check capabilities
    #
    if obj.available:
        for _c in obj.capabilities:
            if not _c in opt._capabilities:
                raise ValueError("Solver %s does not support capability %s!" % (obj.name, _c))
        #
        # Get version
        #
        obj.version = opt.version()
    return obj
예제 #2
0
파일: solvers.py 프로젝트: wannasmile/pyomo
def initialize(**kwds):
    obj = Options(**kwds)
    #
    # Set obj.available
    #
    opt = None
    try:
        opt = SolverFactory(obj.name, solver_io=obj.io)
    except:
        pass
    if opt is None or isinstance(opt, UnknownSolver):
        obj.available = False
    elif (obj.name == "gurobi") and \
       (not GUROBISHELL.license_is_valid()):
        obj.available = False
    elif (obj.name == "baron") and \
       (not BARONSHELL.license_is_valid()):
        obj.available = False
    else:
        obj.available = \
            (opt.available(exception_flag=False)) and \
            ((not hasattr(opt,'executable')) or \
            (opt.executable() is not None))
    #
    # Check capabilities
    #
    if obj.available:
        for _c in obj.capabilities:
            if not _c in opt._capabilities:
                raise ValueError("Solver %s does not support capability %s!" % (obj.name, _c))
        #
        # Get version
        #
        obj.version = opt.version()
    return obj
예제 #3
0
def initialize(**kwds):
    obj = Options(**kwds)
    #
    # Set the limits for the solver's "demo" (unlicensed) mode:
    #   ( nVars, nCons, nNonZeros )
    obj.demo_limits = (None, None, None)
    if (obj.name == "baron") and \
       (not BARONSHELL.license_is_valid()):
        obj.demo_limits = (10, 10, 50)
    #
    #
    # Set obj.available
    #
    opt = None
    try:
        opt = SolverFactory(obj.name, solver_io=obj.io)
    except:
        pass
    if opt is None or isinstance(opt, UnknownSolver):
        obj.available = False
    elif (obj.name == "gurobi") and \
       (not GUROBISHELL.license_is_valid()):
        obj.available = False
    elif (obj.name == "mosek") and \
       (not MosekDirect.license_is_valid()):
        obj.available = False
    else:
        obj.available = \
            (opt.available(exception_flag=False)) and \
            ((not hasattr(opt,'executable')) or \
            (opt.executable() is not None))
    #
    # Check capabilities, even if the solver is not available
    #
    if not (opt is None or isinstance(opt, UnknownSolver)):
        for _c in obj.capabilities:
            if not _c in opt._capabilities:
                raise ValueError("Solver %s does not support capability %s!" %
                                 (obj.name, _c))
    #
    # Get version
    #
    if obj.available:
        obj.version = opt.version()
    return obj
예제 #4
0
def initialize(**kwds):
    obj = Bunch(**kwds)
    #
    # Set obj.available
    #
    try:
        opt = SolverFactory(obj.name, solver_io=obj.io)
    except:
        opt = None

    if opt is None or isinstance(opt, UnknownSolver):
        obj.available = False
    elif not opt.available(exception_flag=False):
        obj.available = False
    elif hasattr(opt, 'executable') and opt.executable() is None:
        obj.available = False
    elif not opt.license_is_valid() \
         and obj.name not in licensed_solvers_with_demo_mode:
        obj.available = False
    else:
        obj.available = True
    #
    # Set the limits for the solver's "demo" (unlicensed) mode:
    #   ( nVars, nCons, nNonZeros )
    obj.demo_limits = (None, None, None)
    if obj.available:
        if obj.name == "baron" and not opt.license_is_valid():
            obj.demo_limits = (10, 10, 50)
    #
    # Check capabilities, even if the solver is not available
    #
    if not (opt is None or isinstance(opt, UnknownSolver)):
        for _c in obj.capabilities:
            if not _c in opt._capabilities:
                raise ValueError("Solver %s does not support capability %s!" %
                                 (obj.name, _c))
    #
    # Get version
    #
    if obj.available:
        obj.version = opt.version()
    return obj
예제 #5
0
파일: denoise.py 프로젝트: vongostev/QPIP
def denoiseopt(dnmodel,
               mean_lbound=0,
               mean_ubound=0,
               g2_lbound=0,
               g2_ubound=0,
               eps_tol=0,
               solver_name='ipopt',
               solver_path='',
               save_all_nondom_x=False,
               plot=False,
               save_all_nondom_y=False,
               disp=False):
    """
    Interface to epsilon-constrained algorithm
    Made to denoise photon-number or photounting statistics
    from the coherent background.
    It is usable for recovering spectrum of fluctuations, excess noise or
    single-photon signal.

    Parameters
    ----------
    dnmodel : qpip.denoise.DenoisePBaseModel
        A model of the problem to solve.
    mean_lbound : float, optional
        Minimal possible value of noise's mean. The default is 0 and ignored.
        Also ignored if g2 bounds exist.
    mean_ubound : float, optional
        Maximal possible value of noise's mean. The default is 0 and ignored.
        Also ignored if g2 bounds exist.
    g2_lbound : float, optional
        Minimal possible value of noise's g2. The default is 0 and ignored.
        Also ignored if mean bounds exist.
    g2_ubound : float, optional
        Maximal possible value of noise's g2. The default is 0 and ignored.
        Also ignored if mean bounds exist.
    solver_name : str, optional
        Solver to solve the problem.
        The default is ipopt if installed.
    solver_path : str, optional
        Path to the solver executable
    eps_tol : float, optional
        Tolarance of additional variables to finish iterations.
        The default is 0.
    save_all_nondom_x : bool, optional
        Save all nondominate solutions in output.
        The default is False.
    save_all_nondom_y : bool, optional
        Save y vector values for all nondominate solutions.
        The default is False.
    disp : bool, optional
        Display calculation progress.
        Messages have following format:
            'iteration number' 'status (1 is success, 0 is fail)' ['discrepancy', 'noise_mean', 'negentropy', 'noise_g2']
        The default is False.
    plot : bool, optional
        Plot dependency of vars from an iteration number
        after the end of calculations.
        The default is False.

    Returns
    -------
    res : scipy.optimize.OptimizeResult
        see help(qpip.epscon.iterate).

    """
    if g2(dnmodel.P) < 1:
        warn(
            'g2 of the given distribution is less than 1. Algorithm can not use g2 and mean bounds.',
            RuntimeWarning, __file__, 171)

    solver = SolverFactory(solver_name, executable=solver_path, solver_io="nl")
    if solver_path:
        solver.executable = solver_path

    y_vars = ['discrepancy', 'noise_mean', 'negentropy', 'noise_g2']
    x_var = 'p_noise'

    if mean_ubound * g2_ubound or mean_ubound * g2_lbound or mean_lbound * g2_ubound or mean_lbound * g2_lbound:
        mean_ubound = 0
        mean_lbound = 0
        warn(
            'It is not possible to set g2 and mean bounds together. Mean bounds are ignored.',
            RuntimeWarning, __file__, 101)

    if mean_ubound:
        dnmodel.c_low_g2 = Constraint(
            expr=dnmodel.noise_g2 >= noiseg2_by_mean(dnmodel.P, mean_ubound))
        dnmodel.c_top_mean = Constraint(expr=dnmodel.noise_mean <= mean_ubound)

    if mean_lbound:
        dnmodel.c_top_g2 = Constraint(
            expr=dnmodel.noise_g2 <= noiseg2_by_mean(dnmodel.P, mean_lbound))
        dnmodel.c_low_mean = Constraint(expr=dnmodel.noise_mean >= mean_lbound)

    if g2_ubound:
        dnmodel.c_top_g2 = Constraint(expr=dnmodel.noise_g2 <= g2_ubound)
        dnmodel.c_low_mean = Constraint(
            expr=dnmodel.noise_mean >= noisemean_by_g2(dnmodel.P, g2_ubound))

    if g2_lbound:
        dnmodel.c_low_g2 = Constraint(expr=dnmodel.noise_g2 >= g2_lbound)
        dnmodel.c_top_mean = Constraint(
            expr=dnmodel.noise_mean <= noisemean_by_g2(dnmodel.P, g2_lbound))

    mean_bound = mean(dnmodel.P) if not mean_ubound else mean_ubound
    noise_bounds = [[
        max(0, mean_lbound), -np.log(len(dnmodel.NSET)),
        max(1, g2_lbound)
    ], [mean_bound, 0, max(20, g2_ubound)]]

    res = iterate(dnmodel,
                  solver,
                  y_vars,
                  x_var,
                  eps_bounds=noise_bounds,
                  eps_tol=eps_tol,
                  save_all_nondom_x=save_all_nondom_x,
                  save_all_nondom_y=save_all_nondom_y,
                  disp=disp)

    info('Optimization ended in %d iterations with eps_tol %.1e' %
         (res.nit, eps_tol))
    info('Status "{0}", "{1}"'.format(*res.status))

    if plot and res.nit > 1:
        for i in lrange(y_vars):
            plt.plot([e[i] for e in res.ndy], label=y_vars[i])
            plt.legend(frameon=False)
            plt.show()

    return res