def has_cplex_lp():
     from pyomo.environ import SolverFactory
     try:
         cplex = SolverFactory('cplex',keepFiles=True)
         available = (not cplex.executable() is None) and cplex.available(False)
         return available
     except pyutilib.common.ApplicationError:
         return False
Exemplo n.º 2
0
def pyomo_multi_stage(data, prob=None):
    """
    Perform multi-stage optimization with PySP.

    Required:
        prob:       Problem instance
    """
    #
    # Error Checking
    #
    solvername = get_solvername(prob)
    prob_name = prob.getProblemOption('name',0)
    con_name = prob.getProblemOption('constraint', prob_name)
    if len(con_name) > 1:
        raise RuntimeError, "We don't know how to handle side-constraints within PySP"
    else:
        con_name = con_name[0]
    aggr_name = prob.getProblemOption('aggregate', prob_name)
    goal_name = prob.getConstraintOption('goal', con_name)
    if lower(goal_name) != 'ns':
        cost_name = prob.getCostOption('cost file', goal_name)
        if not cost_name is None:
            raise RuntimeError, "Cannot use a cost constraint goal with the %s solver" % solvername
    if aggr_name is not None:
        raise RuntimeError, "Cannot use a aggregation with the %s solver" % solvername
    #
    # Create the Pyomo data files
    #
    create_multistage_data(prob, data)
    #
    # Setup the PySP command-line
    #
    solvername = get_solvername(prob)
    #
    seed = prob.getSolverOption('seed')
    if solvername == 'ef':
        cmd = ['runef']
        cmd.append('--output-solver-log')
    elif solvername == 'ph':
        cmd = ['runph']
        cmd.append('-r')
        cmd.append('1.0')
    cmd.append('--solver')
    #
    # TODO: add an 'ef' solver option that specifies a sub-solver name
    #
    opt = SolverFactory('pico')
    if opt.available(False) and (not opt.executable() is None):
        cmd.append('pico')
    else:
        opt = SolverFactory('cbc')
        if opt.available(False) and (not opt.executable() is None):
            cmd.append('cbc')
        else:
            opt = SolverFactory('glpk')
            if opt.available(False) and (not opt.executable() is None):
                cmd.append('glpk')
    #
    cmd.append('-m')
    cmd.append('%s/multistage' % modeldir)
    cmd.append('-i')
    cmd.append('%s' % data.datadir)
    if not seed is None:
        cmd.append('--scenario-tree-seed')
        cmd.append(str(seed))
    cmd.append('--solution-writer=pywst.sp.pyspsolutionwriter')
    cmd.append('--traceback')
    if solvername == 'ef':
        cmd.append('--solve')
    else:
        cmd.append('--linearize-nonbinary-penalty-terms=5')
    data.command_line = ' '.join(cmd)
    #
    # Run the solver
    #
    print "Running command ..."
    print ' '.join(cmd)
    rc, output = pyutilib.subprocess.run(cmd, tee=True)
    if rc != 0:
        print "ERROR executing the '%s' command" % solvername
        print output
        raise RuntimeError, "Bad return code for multi-stage solver: %d\n%s" % (rc, output)
    #
    # Read the JSON file
    #
    INPUT = open(solvername+'.json','r')
    results = json.load(INPUT)
    INPUT.close()
    stage2 = {}
    soln = []
    for i in results:
        for j in results[i]:
            for k in results[i][j]:
                for l in results[i][j][k]:
                    #print i,j,k,l
                    val = results[i][j][k][l]
                    if i == 'FirstStage' and j == 'RootNode' and k == 'Stage1Sensors' and val == 1.0:
                        soln.append( eval(l) )
                    elif i == 'SecondStage':
                        if not j in stage2:
                            stage2[ j ] = []
                        if k == 'Stage2Sensors' and val == 1.0:
                            loc = eval(l)
                            if not loc in soln:
                                stage2[ j ].append( loc )
    data.solutions = [soln]
    data.stage2_solutions = stage2
    #
    # Remove the data directory
    #
    if not prob.getConfigureOption('keepfiles'):
        shutil.rmtree(data.datadir)
Exemplo n.º 3
0
def get_solver_availability():
    logging.disable(logging.WARNING)
    available = {}
    try:
        opt = SolverFactory('pico')
        available['pico'] = opt.available(False) and (
            not opt.executable() is None)
        del opt
    except pyutilib.common.ApplicationError:
        available['pico'] = False
    try:
        opt = SolverFactory('cplex')
        available['cplex'] = opt.available(False) and (
            not opt.executable() is None)
        del opt
    except pyutilib.common.ApplicationError:
        available['cplex'] = False
    try:
        opt = SolverFactory('cplex_direct')
        available['cplex_direct'] = opt.available(False) and (
            not opt.executable() is None)
        del opt
    except pyutilib.common.ApplicationError:
        available['cplex_direct'] = False
    try:
        opt = SolverFactory('gurobi')
        available['gurobi'] = opt.available(False) and (
            not opt.executable() is None)
        del opt
    except pyutilib.common.ApplicationError:
        available['gurobi'] = False
    try:
        opt = SolverFactory('gurobi_direct')
        available['gurobi_direct'] = opt.available(False) and (
            not opt.executable() is None)
        del opt
    except pyutilib.common.ApplicationError:
        available['gurobi_direct'] = False
    try:
        opt = SolverFactory('glpk')
        available['glpk'] = opt.available(False) and (
            not opt.executable() is None)
        del opt
    except pyutilib.common.ApplicationError:
        available['glpk'] = False
    try:
        opt = SolverFactory('cbc')
        available['cbc'] = opt.available(False) and (
            not opt.executable() is None)
        del opt
    except pyutilib.common.ApplicationError:
        available['cbc'] = False
    try:
        opt = SolverFactory('xpress')
        available['xpress'] = opt.available(False) and (
            not opt.executable() is None)
        del opt
    except pyutilib.common.ApplicationError:
        available['xpress'] = False
    logging.disable(logging.NOTSET)
    return available