Changeset 2124


Ignore:
Timestamp:
Jan 3, 2010 6:47:24 PM (11 years ago)
Author:
wehart
Message:

Misc fixes.

Location:
coopr.fdt/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • coopr.fdt/trunk/coopr/fdt/FDT.py

    r2123 r2124  
    11
    2 from coopr.pyomo import *
     2#from coopr.opt import SolverRegistration, IOptSolver, SolverFactory, SolverManagerFactory, SolverStatus, TerminationCondition
    33from coopr.opt import *
    44import copy
     5from pyutilib.misc import Container, Options
     6import bidict
     7from pyutilib.plugin.core import implements
     8#from coopr.pyomo import sequence, Set, Param, Var, Constraint, Objective, Model, RangeSet, maximize, generate_canonical_repn, as_expr, apply_transformation, xsequence
     9from coopr.pyomo import *
    510import math
    6 from pyutilib.misc import Container
    7 import bidict
    8 from pyutilib.plugin.core import *
    911
    1012class FDT(object):
     
    1315    implements(IOptSolver)
    1416
    15     def __init__(self, options):
    16         """ Constructor method """
     17    def initialize(self, options, instance):
     18        """
     19        Initialize the solver with an Options object
     20        """
    1721        self.options = options
    1822        if self.options.always_prune is None:
     
    2226        if self.options.tol is None:
    2327            self.options.tol = 1e-8
     28        if self.options.lpsolver is None:
     29            self.options.lpsolver = 'cplex'
     30        if self.options.smanager_type is None:
     31            self.options.smanager_type = 'serial'
     32        if self.options.max_pool_size is None:
     33            self.options.max_pool_size = instance.statistics.number_of_binary_variables
    2434        #
    2535        # Create solver
     
    4252            print options.__str__(indent='    ')
    4353
    44     # Solve the specified problem and return
    45     # a SolverResults object
    46     def run(self, instance, **kwds):
     54    def solve(self, *args, **kwds):
     55        """
     56        Solve the specified problem and return a SolverResults object
     57        """
     58        instance = args[0]
     59        if len(args) == 2:
     60            options = args[1]
     61        else:
     62            options = Options()
    4763        for key in kwds:
    48             setattr(self.options, key, kwds[key])
     64            options[key] = kwds[key]
     65        self.initialize(options, instance)
    4966        print "Starting FDT heuristic"
    5067        #
     
    88105        keys = list(self.branching_lp.binary_vars)
    89106        keys.sort()
    90         pool = [Container(values=omega, nfixed=0, integer=self.is_integer(omega, keys, self.options.tol), branch_vars=keys)]
     107        pool = [Container(values=omega, fixed=None, integer=self.is_integer(omega, keys, self.options.tol), branch_vars=keys)]
    91108        if self.options.debug:
    92109            # reduced_instance.display()
     
    100117            did_work=False
    101118            for node in pool:
    102                 node.nfixed = i
     119                node.fixed = i
    103120                if node.integer:
    104121                    tmppool.append(node)
    105122                    continue
    106123                branchinglp = self.generate_branching_lp(node, varmap, keys, reduced_instance)
    107                 print "X",type(branchinglp),self.opt,self.options,self.solver_mngr
    108                 results = self.solver_mngr.solve(branchinglp, opt=self.opt, tee=self.options.tee, timelimit=self.options.timelimit, options=" ".join(self.options.lpsolver_options))
     124                results = self.solver_mngr.solve(branchinglp, opt=self.opt, keepFiles=True, tee=self.options.tee, timelimit=self.options.timelimit, options=" ".join(self.options.lpsolver_options))
    109125                results.write()
    110126                #
     
    115131                    print "Terminating due to error in results"
    116132                    return None
    117                 did_work &= self.process_results(node, results, tmppool)
    118             if did_work and (self.options.always_prune or len(pool) > n):
    119                 pool = self.prune(tmppool, n, omega)
     133                did_work |= self.process_results(node, results, tmppool)
     134            print "Y POOL",did_work,tmppool
     135            if did_work:
     136                if (self.options.always_prune or len(pool) > n):
     137                    pool = self.prune(tmppool, n, omega)
     138                else:
     139                    pool = tmppool
     140            print "POOL after branching on variable",i
    120141            print pool
     142        print "FINAL POOL"
    121143        print pool
    122144        for item in pool:
     
    214236        self.branching_lp.y_fixed.add(1, self.branching_lp.y[i,1] == self.branching_lp.l[1])
    215237        self.branching_lp.y_fixed.construct()
     238        self.branching_lp.presolve()
     239        #
     240        # Print the final LP reformulation
     241        #                   
     242        if self.options.debug:
     243            self.branching_lp.pprint()
    216244        #
    217245        return self.branching_lp
     
    364392        """ Process a branching LP results object and possibly update the pool """
    365393        soln = results.solution()
    366         i = node.fixed
    367394        did_work=False
    368395        if self.options.debug:
    369396            print soln.variable.keys()
    370397        if soln.variable.l[0].value > self.options.tol:
    371             values = []
    372             for i in xsequence(n):
    373                 values.append( soln.variable.x[i,0].value / soln.variable.l[0].value )
    374             tmppool += [ Container(nfixed=node.nfixed+1, values=values, integer=self.is_integer(values, self.options.tol), branch_vars=node.branch_vars) ]
     398            values = {}
     399            for i in self.branching_lp.vars:
     400                values[i] = soln.variable.y[i,0].value / soln.variable.l[0].value
     401            tmppool += [ Container(fixed=None, values=values, integer=self.is_integer(values, node.branch_vars, self.options.tol), branch_vars=node.branch_vars) ]
    375402            did_work=True
    376403        if soln.variable.l[1].value > self.options.tol:
    377             values = []
    378             for i in xsequence(n):
    379                 values.append( soln.variable.x[i,1].value / soln.variable.l[1].value )
     404            values = {}
     405            for i in self.branching_lp.vars:
     406                values[i] = soln.variable.y[i,1].value / soln.variable.l[1].value
    380407            did_work=True
    381             tmppool += [ Container(nfixed=node.nfixed+1, values=values, integer=self.is_integer(values, self.options.tol), branch_vars=node.branch_vars) ]
     408            tmppool += [ Container(fixed=None, values=values, integer=self.is_integer(values, node.branch_vars, self.options.tol), branch_vars=node.branch_vars) ]
     409        print "X POOL",tmppool
    382410        return did_work
    383411       
     412    def prune(self, pool, n, omega):
     413        print "Pool", pool
     414        #
     415        # Create a packing LP model
     416        #
     417        model = Model()
     418        #
     419        model.V = RangeSet(1,n)
     420        #
     421        def xs_rule(i, model):
     422            return omega[i]
     423        model.xs = Param(model.V, initialize=xs_rule)
     424        #
     425        model.Good = RangeSet(1,len(pool))
     426        #
     427        def Sols_rule(i, u, model):
     428            return pool[i-1].values[u]
     429        model.Sols = Param(model.Good, model.V, initialize=Sols_rule)
     430        #
     431        model.lam = Var(model.Good, bounds=(0.0,1.0))
     432        #
     433        def Prunedom_rule(model):
     434            return summation(model.lam)
     435        model.Prunedom = Objective(rule=Prunedom_rule, sense=maximize)
     436        #
     437        def ConvComb_rule(u, model):
     438            return sum([ model.lam[i]*model.Sols[i,u] for i in model.Good]) <= model.xs[u]
     439        model.ConvComb = Constraint(model.V, rule=ConvComb_rule)
     440        #
     441        # Construct and solve this model
     442        #
     443        instance = model.create()
     444        results = self.solver_mngr.solve(instance, opt=self.opt, tee=self.options.tee, timelimit=self.options.timelimit, options=" ".join(self.options.lpsolver_options))
     445        #
     446        # Create new pool using the nonzero lambdas
     447        #
     448        newpool = []
     449        soln = results.solution()
     450        print "Root LP", omega
     451        print "Pool", pool
     452        for i in soln.variable.lam:
     453            print "Packing Info:", i, soln.variable.lam[i].value, pool[i-1].values
     454            if soln.variable.lam[i].value > 0.0:
     455                newpool.append( pool[i-1] )
     456        return newpool
     457
    384458
    385459#
     
    440514                    did_work=True
    441515                    tmppool += [ Container(nfixed=node.nfixed+1, values=values, integer=self.is_integer(values, self.options.tol)) ]
    442                 #tmppool += self.generate_children(results)
    443             if did_work and (self.options.always_prune or len(pool) > n):
    444                 pool = self.prune(tmppool, n, omega)
     516            if did_work:
     517                if (self.options.always_prune or len(pool) > n):
     518                    pool = self.prune(tmppool, n, omega)
     519                else:
     520                    pool = tmppool
    445521            print pool
    446522        print pool
     
    459535            return True
    460536        return False
    461 
    462     def generate_children(self, omega, i):
    463         #for x in omega:
    464             #self.generate_packing_lp(fix_var, omega, ...)
    465         pass
    466537
    467538    def prune(self, pool, n, omega):
  • coopr.fdt/trunk/scripts/fdt

    r2123 r2124  
    2020model = import_file(sys.argv[1]+".py").model
    2121instance = model.create()
    22 fdt = FDT(options)
    23 results = fdt.run(instance)
     22fdt = FDT()
     23results = fdt.solve(instance, options)
    2424results.write()
    2525
Note: See TracChangeset for help on using the changeset viewer.