artofscience / SAOR

Sequential Approximate Optimization Repository
GNU General Public License v3.0
5 stars 1 forks source link

Added a scaling feature #62

Closed Giannis1993 closed 3 years ago

Giannis1993 commented 3 years ago

This PR adds a scaling feature so that objective (and constraints) can be scaled according to a selected strategy. The scaling.py file includes some basic scaling strategies to show the concept. Also includes some minor changes to the examples -- just to be compatible with the changes.


I also added an enforce_convexity to Taylor2.

Giannis1993 commented 3 years ago

How does this look wrt the previous implementation? I think that way the implemented Scaling classes only need update_factor and update_condition methods.

class Scaling(ABC):
    """
    This is the abstract implementation of the Scaling class.
    """

    def __init__(self, num_of_resp, **kwargs):
        self.factor = np.ones(num_of_resp, dtype=float)

    @abstractmethod
    def update_condition(self, **kwargs):
        return NotImplementedError

    @abstractmethod
    def update_factor(self, **kwargs):
        return NotImplementedError

    def scale(self, f=None, df=None, **kwargs):
        if self.update_condition():
            self.update_factor()
        return self.factor * f, (df.T * self.factor).T

class ScalingTemplate(Scaling):
    """
    This is an example of an implemented scaling class. 
    Here, only the objective is scaled wrt its value at the 0th iteration.
    """

    def __init__(self, num_of_resp, **kwargs):
        super().__init__(num_of_resp, **kwargs)
        self.scale_to = np.ones_like(self.factor)

    def update_factor(self, f=None, df=None, **kwargs):
        if f[0] == 0:
            raise ZeroDivisionError(f'Cannot use {self.__class__.__name__} class with objective value 0')
        self.factor[0] = self.scale_to[0] / f[0]
        return self

    def update_condition(self):
        if np.all(self.factor == 1):
            return True
        else:
            return False
artofscience commented 3 years ago

@Giannis1993 can you fix the conflicts, then we can merge this

Giannis1993 commented 3 years ago

@Giannis1993 can you fix the conflicts, then we can merge this

Yeah, I am having some trouble with the enforce_convexity feature that doesn't seem to always work. I will fix it as soon as finish with that and the SphericalTaylor2 and NonSphericalTaylor2 classes.

Giannis1993 commented 3 years ago

Wrote it again and merged it into main.