class Async_Optimize(object):
    def __init__(self):
        self.callback = lambda *x: None
        self.runsignal = Event()
        self.SENTINEL = "SENTINEL"

    def async_callback_collect(self, q):
        while self.runsignal.is_set():
            try:
                for ret in iter(lambda: q.get(timeout=1), self.SENTINEL):
                    self.callback(*ret)
                self.runsignal.clear()
            except Empty:
                pass

    def opt_async(self, f, df, x0, callback, update_rule=PolakRibiere,
                   messages=0, maxiter=5e3, max_f_eval=15e3, gtol=1e-6,
                   report_every=10, *args, **kwargs):
        self.runsignal.set()
        c = None
        outqueue = None
        if callback:
            outqueue = Queue()
            self.callback = callback
            c = Thread(target=self.async_callback_collect, args=(outqueue,))
            c.start()
        p = _CGDAsync(f, df, x0, update_rule, self.runsignal, self.SENTINEL,
                 report_every=report_every, messages=messages, maxiter=maxiter,
                 max_f_eval=max_f_eval, gtol=gtol, outqueue=outqueue, *args, **kwargs)
        p.start()
        return p, c

    def opt(self, f, df, x0, callback=None, update_rule=FletcherReeves,
                   messages=0, maxiter=5e3, max_f_eval=15e3, gtol=1e-6,
                   report_every=10, *args, **kwargs):
        p, c = self.opt_async(f, df, x0, callback, update_rule, messages,
                            maxiter, max_f_eval, gtol,
                            report_every, *args, **kwargs)
        while self.runsignal.is_set():
            try:
                p.join(1)
                if c: c.join(1)
            except KeyboardInterrupt:
                # print "^C"
                self.runsignal.clear()
                p.join()
                if c: c.join()
        if c and c.is_alive():
#             self.runsignal.set()
#             while self.runsignal.is_set():
#                 try:
#                     c.join(.1)
#                 except KeyboardInterrupt:
#                     # print "^C"
#                     self.runsignal.clear()
#                     c.join()
            print("WARNING: callback still running, optimisation done!")
        return p.result
Exemplo n.º 2
0
class Solver(object):
    UNSOLVED = -1
    SOLVED = 0
    SOLVING = 1
    UNSOLVABLE = 2
    r"""
    @attention: Always reference the model thus: solver.model.xxx
    """
    def __init__(self, model, count=1):
        self._lock = RLock()
        self._model = model
        #    Context:
        self._abort = Event()
        self._abort.clear()
        self._solvers = Context(count, self._getModel, self._abort, self._lock)
        self._isAborted = False
    def _getModel(self):
        with self.lock():
            return self._model
    model = property(_getModel)
    def __str__(self):
        pre="Unsolved"
        with self.lock():
            if len(list(self._model.unknowns()))==0:
                pre="> SOLVED < "
            return "\n".join(["%(PRE)sSolver:"%{"PRE":pre}, str(self._model)])
    def lock(self):
        return self._lock
    def serialize(self):
        #    Retrieve the model ready for storing:
        with self.lock():
            return self._model.serialize()
    def deserialize(self, data):
        #    Load the model ready for use:
        with self.lock():
            self._model = self.model.deserialize(data)
    def clone(self):
        with self.lock():
            try:
                self.abort()
            except Aborted, _e:
                pass
            self._abort.set()
            m = self.model.clone()
            self._isAborted = False
            return Solver(m)
Exemplo n.º 3
0
class PipelineSolver(object):
    def __init__(self, model, abort, lock):
        self.model = model
        self.abort = abort
        self.lock = lock
        self.event = Event()
        self.event.clear()
        self.args = []
        self.unsolvable = False
        self.solved = False
        model.addParentListener(self)
        self._strategies = StrategiesFactory(model, self._checkAbort, lambda: self.solved, lambda: self.unsolvable)
        self._resetPipeline()
    def notification(self, who, event=None, args=None):
        if who==grid.NAME:
            if event in [iModelNotifications.ALL(), iModelNotifications.SOLVED(), iModelNotifications.UNSOLVABLE]:
                self.args = []
            if event==iModelNotifications.SOLVED():
                self.solved = True
            if event==iModelNotifications.UNSOLVABLE():
                self.unsolvable = True
        if event==iModelNotifications.CUSTOM():
            return
        self.args.append((event, args))
    def __call__(self):
        try:
            if self.unsolvable:
                raise UnSolveable(self.model)
            if self.solved or self.model.solved:
                raise Solved(self.model)
            eventCount = self._eventCount()
            if len(self.pipeline)>0 or eventCount>0:
                self._executePipeline(eventCount)
            else:
                print "out of rules!"
                raise UnSolveable(self.model)
        except (Solved, UnSolveable, AbortAck), e:
            if isinstance(e, Solved):
                self.solved = True
                self.model.solved = True
            elif isinstance(e, UnSolveable):
                self.unsolvable = True
                self.model.unsolvable = True
            self.args = []
            self.pipeline = []
            raise
Exemplo n.º 4
0
class Async_Optimize(object):
    def __init__(self):
        self.callback = lambda *x: None
        self.runsignal = Event()
        self.SENTINEL = "SENTINEL"

    def async_callback_collect(self, q):
        while self.runsignal.is_set():
            try:
                for ret in iter(lambda: q.get(timeout=1), self.SENTINEL):
                    self.callback(*ret)
                self.runsignal.clear()
            except Empty:
                pass

    def opt_async(self,
                  f,
                  df,
                  x0,
                  callback,
                  update_rule=PolakRibiere,
                  messages=0,
                  maxiter=5e3,
                  max_f_eval=15e3,
                  gtol=1e-6,
                  report_every=10,
                  *args,
                  **kwargs):
        self.runsignal.set()
        c = None
        outqueue = None
        if callback:
            outqueue = Queue()
            self.callback = callback
            c = Thread(target=self.async_callback_collect, args=(outqueue, ))
            c.start()
        p = _CGDAsync(f,
                      df,
                      x0,
                      update_rule,
                      self.runsignal,
                      self.SENTINEL,
                      report_every=report_every,
                      messages=messages,
                      maxiter=maxiter,
                      max_f_eval=max_f_eval,
                      gtol=gtol,
                      outqueue=outqueue,
                      *args,
                      **kwargs)
        p.start()
        return p, c

    def opt(self,
            f,
            df,
            x0,
            callback=None,
            update_rule=FletcherReeves,
            messages=0,
            maxiter=5e3,
            max_f_eval=15e3,
            gtol=1e-6,
            report_every=10,
            *args,
            **kwargs):
        p, c = self.opt_async(f, df, x0, callback, update_rule, messages,
                              maxiter, max_f_eval, gtol, report_every, *args,
                              **kwargs)
        while self.runsignal.is_set():
            try:
                p.join(1)
                if c: c.join(1)
            except KeyboardInterrupt:
                # print "^C"
                self.runsignal.clear()
                p.join()
                if c: c.join()
        if c and c.is_alive():
            #             self.runsignal.set()
            #             while self.runsignal.is_set():
            #                 try:
            #                     c.join(.1)
            #                 except KeyboardInterrupt:
            #                     # print "^C"
            #                     self.runsignal.clear()
            #                     c.join()
            print("WARNING: callback still running, optimisation done!")
        return p.result