Example #1
0
    def getQuadraticModel(self, phi):
        # ensure this is an array, I think I was refactoring this to be matrices
        phi = asarray(phi).flatten()

        c = 0
        b = zeros(self.n)
        Q = zeros((self.n, self.n))
        for j in range(self.powers.shape[1]):
            sumpowers = sum(self.powers[:, j])
            if sumpowers == 0:
                c = phi[j] * self.coeff[j]
            elif sumpowers == 1:
                b[self.powers[:, j] > 0] = phi[j] * self.coeff[j]
            elif sumpowers == 2:
                idx1 = -1
                idx2 = -1
                for i in range(self.powers.shape[0]):
                    if self.powers[i, j] == 0:
                        continue
                    if idx1 < 0:
                        idx1 = i
                    else:
                        idx2 = i
                if idx2 < 0:
                    Q[idx1, idx1] = 2 * phi[j] * self.coeff[j]
                else:
                    Q[idx1, idx2] = phi[j] * self.coeff[j]
                    Q[idx2, idx1] = phi[j] * self.coeff[j]
            else:
                pass
        return functions.Quadratic(Q, b, c)
Example #2
0
    def test_simpleValuesMatch(self):
        fun1 = functions.Quadratic(arr([[1, 0], [0, -2]]), arr([3, 2]), 4)
        fun2 = functions.Quadratic(zeros((2, 2)), arr([3, 2]), 4)
        funs = [fun1, fun2]
        set = dfo_model.MultiFunctionModel(funs, self.b, self.center,
                                           self.radius)
        x = arr([3, 4])

        y1 = arr([fun1.evaluate(x), fun2.evaluate(x)])
        y2 = set.interpolate(x)

        self.assertTrue(norm(y1 - y2) < self.tolerance)

        quadmod = set.getQuadraticModels(arr([0, 1], int))
        y2 = quadmod.evaluate(x)

        self.assertTrue(norm(y1 - y2) < self.tolerance)

        newCenter = 10 * (2 * random.random(2) - 1)
        for i in range(50):
            print("testing " + str(i) + " of " + str(50))
            rFactor = self.getRFactor()
            newRadius = set.modelRadius * rFactor
            newCenter = newCenter + set.modelRadius / newRadius
            oldlen = set.history.size()
            pva = set.testNewModelCenter(newCenter)
            newlen = set.history.size()

            self.assertTrue(newlen == oldlen + 1)

            deviation = norm(
                ((pva[:, 0] - pva[:, 1]) / pva[:, 1])) / pva.shape[0]
            self.assertTrue(deviation < self.tolerance)
            set.setNewModelCenter(newCenter)
            set.multiplyRadius(rFactor)
            set.improve('images/test_%04d_improve.png' % i)

            quadmod = set.getQuadraticModels(arr([0, 1], int))
            for j in range(10):
                x = 10 * (2 * random.random(2) - 1)
                y1 = arr([fun1.evaluate(x), fun2.evaluate(x)])
                y2 = quadmod.evaluate(x)

                self.assertTrue(norm(y1 - y2) < self.tolerance)
Example #3
0
    def evaluate(self, x):
        qL = full([self.n, self.n], -infinity)
        qU = full([self.n, self.n], infinity)
        bL = full(self.n, -infinity)
        bU = full(self.n, infinity)
        cL = -infinity
        cU = infinity

        for e in self.history:
            e.updateQBounds(x, qL, qU, self.maxChangeInParams)
            e.updateBBounds(x, bL, bU, self.maxChangeInParams)
            cL, cU = e.updateCBounds(x, cL, cU, self.maxChangeInParams)

        rand = random([self.n, self.n])
        newQ = multiply(rand, qL) + multiply(1 - rand, qU)
        rand = random(self.n)
        newB = multiply(rand, bL) + multiply(1 - rand, bU)
        rand = random()
        newC = rand * cL + (1 - rand) * cU

        self.history.append(Evaluation(x, newQ, newB, newC))
        return functions.Quadratic(2 * newQ, newB, newC)
Example #4
0
    #log = file if hasattr(file,'write') else sys.stderr
    log = sys.stderr
    traceback.print_stack(file=log)
    log.write(warnings.formatwarning(message, category, filename, lineno, line))



Q = arr([[1, 0],[0, 5]])
b = arr([0, 0])
#b = arr([1, 1])
c=0


# write tests

q = functions.Quadratic(Q, b, c)
c1 = functions.Line(arr([1,  1]), -1)
#c2 = functions.Line(arr([1, -1]), 10)
c2 = functions.Wiggles()

equality = []
equality.append(c1)

inequality = []
inequality.append(c2)

x0 = arr([1, 2])

warnings.showwarning = warn_with_traceback
statement = program.Program(q, equality, inequality, x0)
#statement = program.DfoProgram("wiggles", q, equality, inequality, x0, plotImprovements=True)
Example #5
0
from numpy import array
from utilities import functions
from utilities import create_program
import matplotlib.pyplot as plt


quad1 = functions.Quadratic(array([[1, 0], [0, 2]]), array([0, 0]), 5)
quad2 = functions.Quadratic(array([[-1, 0], [0, -2]]), array([10, -1]), -13)
quad3 = functions.Quadratic(array([[-1, 0], [0, -2]]), array([-1, 1]), 50)

func = lambda x: array([quad1.evaluate(x), quad2.evaluate(x), quad3.evaluate(x)])



program, center = create_program.create_program('test', func, 2, 3, num_equality=1)

program.createBasePlotAt(center, 20)
plt.show()



#
#
# from oct2py import octave
# import os
# import numpy as np
# import utilities.cute_dfo_functions as funs
#
# octave.addpath(os.path.dirname(os.path.realpath(__file__)))
#