Martin | 1 Aug 2009 12:23
Picon

sorting an array

Hi,

I would ideally like to sort an multidimensional array by  the first
column, but I can only manage to adjust the whole array. For
example...

>a = array([(1,0,2,5),(1,5,6,2),(1,2,8,7),(1,4,4,6),(1,3,2,3),(1,11,2,0),(1,10,1,3),(1,9,0,4),(1,8,9,6)])
>a
array([[ 1,  0,  2,  5],
       [ 1,  5,  6,  2],
       [ 1,  2,  8,  7],
       [ 1,  4,  4,  6],
       [ 1,  3,  2,  3],
       [ 1, 11,  2,  0],
       [ 1, 10,  1,  3],
       [ 1,  9,  0,  4],
       [ 1,  8,  9,  6]])
>sort(a, axis = 0)
array([[ 1,  0,  0,  0],
       [ 1,  2,  1,  2],
       [ 1,  3,  2,  3],
       [ 1,  4,  2,  3],
       [ 1,  5,  2,  4],
       [ 1,  8,  4,  5],
       [ 1,  9,  6,  6],
       [ 1, 10,  8,  6],
       [ 1, 11,  9,  7]])

When really what i wanted was not for the 3rd and 4th columns to also
be sorted nunmerically in that way. In unix I would reformat the first
(Continue reading)

josef.pktd | 1 Aug 2009 13:44
Picon

Re: sorting an array

On Sat, Aug 1, 2009 at 6:23 AM, Martin<mdekauwe <at> gmail.com> wrote:
> Hi,
>
> I would ideally like to sort an multidimensional array by  the first
> column, but I can only manage to adjust the whole array. For
> example...
>
>>a = array([(1,0,2,5),(1,5,6,2),(1,2,8,7),(1,4,4,6),(1,3,2,3),(1,11,2,0),(1,10,1,3),(1,9,0,4),(1,8,9,6)])
>>a
> array([[ 1,  0,  2,  5],
>       [ 1,  5,  6,  2],
>       [ 1,  2,  8,  7],
>       [ 1,  4,  4,  6],
>       [ 1,  3,  2,  3],
>       [ 1, 11,  2,  0],
>       [ 1, 10,  1,  3],
>       [ 1,  9,  0,  4],
>       [ 1,  8,  9,  6]])
>>sort(a, axis = 0)
> array([[ 1,  0,  0,  0],
>       [ 1,  2,  1,  2],
>       [ 1,  3,  2,  3],
>       [ 1,  4,  2,  3],
>       [ 1,  5,  2,  4],
>       [ 1,  8,  4,  5],
>       [ 1,  9,  6,  6],
>       [ 1, 10,  8,  6],
>       [ 1, 11,  9,  7]])
>
> When really what i wanted was not for the 3rd and 4th columns to also
(Continue reading)

Emmanuelle Gouillart | 1 Aug 2009 13:47
Favicon
Gravatar

Re: sorting an array

	Hi Martin,

	I'm not sure if I understand what you mean, but does the
following solve your problem?

>>> a = np.array([(1,0,2,5),(1,5,6,2),(1,2,8,7),(1,4,4,6),(1,3,2,3),(1,11,2,0),(1,10,1,3),(1,9,0,4),(1,8,9,6)])
>>> a
array([[ 1,  0,  2,  5],
       [ 1,  5,  6,  2],
       [ 1,  2,  8,  7],
       [ 1,  4,  4,  6],
       [ 1,  3,  2,  3],
       [ 1, 11,  2,  0],
       [ 1, 10,  1,  3],
       [ 1,  9,  0,  4],
       [ 1,  8,  9,  6]])
>>> a[:,:2] = np.sort(a[:,:2], axis=0)
>>> a
array([[ 1,  0,  2,  5],
       [ 1,  2,  6,  2],
       [ 1,  3,  8,  7],
       [ 1,  4,  4,  6],
       [ 1,  5,  2,  3],
       [ 1,  8,  2,  0],
       [ 1,  9,  1,  3],
       [ 1, 10,  0,  4],
       [ 1, 11,  9,  6]])

	Cheers,

(Continue reading)

josef.pktd | 1 Aug 2009 14:00
Picon

Re: sorting an array

On Sat, Aug 1, 2009 at 7:47 AM, Emmanuelle
Gouillart<emmanuelle.gouillart <at> normalesup.org> wrote:
>        Hi Martin,
>
>        I'm not sure if I understand what you mean, but does the
> following solve your problem?
>
>>>> a = np.array([(1,0,2,5),(1,5,6,2),(1,2,8,7),(1,4,4,6),(1,3,2,3),(1,11,2,0),(1,10,1,3),(1,9,0,4),(1,8,9,6)])
>>>> a
> array([[ 1,  0,  2,  5],
>       [ 1,  5,  6,  2],
>       [ 1,  2,  8,  7],
>       [ 1,  4,  4,  6],
>       [ 1,  3,  2,  3],
>       [ 1, 11,  2,  0],
>       [ 1, 10,  1,  3],
>       [ 1,  9,  0,  4],
>       [ 1,  8,  9,  6]])
>>>> a[:,:2] = np.sort(a[:,:2], axis=0)
>>>> a
> array([[ 1,  0,  2,  5],
>       [ 1,  2,  6,  2],
>       [ 1,  3,  8,  7],
>       [ 1,  4,  4,  6],
>       [ 1,  5,  2,  3],
>       [ 1,  8,  2,  0],
>       [ 1,  9,  1,  3],
>       [ 1, 10,  0,  4],
>       [ 1, 11,  9,  6]])
>
(Continue reading)

Emmanuelle Gouillart | 1 Aug 2009 16:08
Favicon
Gravatar

Re: Filling a function with values

	Hi Jose,

	I can see two types of answers, depending on what's inside your
function MyFunction. 

	Either the function contains only simple operations like basic
arithmetic operations, or more generally ufuncs (numpy's "universal
functions"), then it's possible that you can take advantage of
broadcasting (see
http://www.scipy.org/Tentative_NumPy_Tutorial#head-081b5e0ff3963e102f4bb39b39261750b507895e
for more information about broadcasting). Here is a small example

>>> import numpy as np
>>> Y, X = np.ogrid[0:5, 0:6]
>>> X
array([[0, 1, 2, 3, 4, 5]])
>>> Y
array([[0],
       [1],
       [2],
       [3],
       [4]])
>>> def add_plus_one(x, y, a=1):
    return x + y + a
... 
>>> add_plus_one(X, Y)
array([[ 1,  2,  3,  4,  5,  6],
       [ 2,  3,  4,  5,  6,  7],
       [ 3,  4,  5,  6,  7,  8],
       [ 4,  5,  6,  7,  8,  9],
(Continue reading)

James Phillips | 1 Aug 2009 16:58
Favicon

Attached multiprocessing parallel Differential Evolution GA code

Below and attached is my first cut at a Python multiprocessing module parallel implementation of the Differential Evolution genetic algorithm.  The code is explicitly in the public domain.

While this does run in parallel processes, I do not see 100% CPU utilization for the child processes on my 4-core test server.  I suspect this is due to locking on the multiprocessing.Array objects, where the entire array is being locked at each access rather than only the requested elements in the array.

Note the "coefficient polishing on" option's use of scipy.optimize.fmin at each new "best solution" during iteration of the genetic algorithm, this vastly speeds up the GA runs.

I did not cross-post to the numpy discussion list, not sure if I should.

     James Phillips
     zunzun AT zunzun.com
     http://zunzun.com


---------------------------------------

#    testParallelDESolver.py
#
#    Placed into the public domain 1 August, 2009
#    James R. Phillips
#    2548 Vera Cruz Drive
#    Birmingham, AL 35235 USA
#    email: zunzun <at> zunzun.com
#    http://zunzun.com

import ParallelDESolver, numpy, time

class TestSolver(ParallelDESolver.ParallelDESolver):

    #some test data
    xData = numpy.array([5.357, 9.861, 5.457, 5.936, 6.161, 6.731])
    yData = numpy.array([0.376, 7.104, 0.489, 1.049, 1.327, 2.077])


    def externalEnergyFunction(self, trial):
        # inverse exponential with offset, y = a * exp(b/x) + c
        predicted = trial[0] * numpy.exp(trial[1] / self.xData) + trial[2]

        # sum of squared error
        error = predicted - self.yData
        return numpy.sum(error*error)



if __name__ == '__main__':

    print
    print '  Running test generating new random numbers and coefficient polisher off, estimated worst case time'
    solver = TestSolver(4, 600, 600, -10, 10, "Rand2Exp", 0.7, 0.6, 0.01, False, False)
    tStart = time.time()
    atSolution, generations, bestEnergy, bestSolution = solver.Solve()
    print ' ', time.time() - tStart, 'seconds elapsed.  atSolution =', atSolution, ':', generations, 'generations passed, bestEnergy =', bestEnergy
    print '  coefficients:',
    for i in bestSolution:
        print i,
    print
    print

    print '  Running test generating new random numbers and coefficient polisher on, estimated medium case time'
    solver = TestSolver(4, 600, 600, -10, 10, "Rand2Exp", 0.7, 0.6, 0.01, False, True)
    tStart = time.time()
    atSolution, generations, bestEnergy, bestSolution = solver.Solve()
    print ' ', time.time() - tStart, 'seconds elapsed.  atSolution =', atSolution, ':', generations, 'generations passed, bestEnergy =', bestEnergy
    print '  coefficients:',
    for i in bestSolution:
        print i,
    print
    print

    print '  Running test recycling random numbers and coefficient polisher on, estimated best case time'
    solver = TestSolver(4, 600, 600, -10, 10, "Rand2Exp", 0.7, 0.6, 0.01, True, True)
    tStart = time.time()
    atSolution, generations, bestEnergy, bestSolution = solver.Solve()
    print ' ', time.time() - tStart, 'seconds elapsed.  atSolution =', atSolution, ':', generations, 'generations passed, bestEnergy =', bestEnergy
    print '  coefficients:',
    for i in bestSolution:
        print i,
    print
    print


-------------------------------------------


#    ParallelDESolver.py
#
#    Placed into the public domain 1 August, 2009
#    James R. Phillips
#    2548 Vera Cruz Drive
#    Birmingham, AL 35235 USA
#    email: zunzun <at> zunzun.com
#    http://zunzun.com


import sys, os, numpy, random, scipy.optimize, multiprocessing


def parallelProcessPoolInitializer(in_sharedMemoryPopulation, in_sharedMemoryPopulationEnergies, in_sharedMemoryBestEnergy, in_sharedMemoryBestSolution, in_sharedMemoryAtSolution, in_sharedMemoryGeneration, in_solver):
    global sharedMemoryPopulation
    global sharedMemoryPopulationEnergies
    global sharedMemoryBestEnergy
    global sharedMemoryBestSolution
    global sharedMemoryAtSolution
    global sharedMemoryGeneration
    global solver

    sharedMemoryPopulation = in_sharedMemoryPopulation
    sharedMemoryPopulationEnergies = in_sharedMemoryPopulationEnergies
    sharedMemoryBestEnergy = in_sharedMemoryBestEnergy
    sharedMemoryBestSolution = in_sharedMemoryBestSolution
    sharedMemoryAtSolution = in_sharedMemoryAtSolution
    sharedMemoryGeneration = in_sharedMemoryGeneration
    solver = in_solver


def parallelProcessFunction(candidate):
    global sharedMemoryPopulation
    global sharedMemoryPopulationEnergies
    global sharedMemoryBestEnergy
    global sharedMemoryBestSolution
    global sharedMemoryAtSolution
    global sharedMemoryGeneration
    global solver

    if sharedMemoryAtSolution.value != 0: # has some other process found a solution?
        return

    eval('solver.' + solver.deStrategy + '(candidate)')
    trialEnergy, atSolution = solver.EnergyFunction(solver.trialSolution)
   
    if solver.polishTheBestTrials == True and trialEnergy < sharedMemoryBestEnergy.value and sharedMemoryGeneration.value > 0: # not the first generation
        # try to polish these new coefficients a bit.
        solver.trialSolution = scipy.optimize.fmin(solver.externalEnergyFunction, solver.trialSolution, disp = 0) # don't print warning messages to stdout
        trialEnergy, atSolution = solver.EnergyFunction(solver.trialSolution) # recalc with polished coefficients
   
    if trialEnergy < sharedMemoryPopulationEnergies[candidate]: # setting two shared memory objects, need to prevent contention here
        sharedMemoryPopulationEnergies[candidate] = trialEnergy
        for i in range(len(solver.trialSolution)):
            sharedMemoryPopulation[candidate * solver.parameterCount + i] = solver.trialSolution[i]

        # If at an all-time low, save to "best"
        if trialEnergy < sharedMemoryBestEnergy.value: # setting two shared memory objects, need to prevent contention here
            #print 'New best energy in generation', sharedMemoryGeneration.value, 'os.getpid() =', os.getpid(), 'new best energy', trialEnergy
            sharedMemoryBestEnergy.value = trialEnergy
            for i in range(len(solver.trialSolution)):
                sharedMemoryBestSolution[i] = solver.trialSolution[i]
           
    # if we've reached a sufficient solution, let the other processes know
    if atSolution == True:
        #print os.getpid(), 'At solution.'
        sharedMemoryAtSolution.value = 1


class ParallelDESolver:

    def __init__(self, parameterCount, populationSize, maxGenerations, minInitialValue, maxInitialValue, deStrategy, diffScale, crossoverProb, cutoffEnergy, useClassRandomNumberMethods, polishTheBestTrials):

        self.polishTheBestTrials = polishTheBestTrials # see the Solve method where this flag is used
        self.maxGenerations      = maxGenerations
        self.parameterCount      = parameterCount
        self.populationSize      = populationSize
        self.cutoffEnergy        = cutoffEnergy
        self.minInitialValue     = minInitialValue
        self.maxInitialValue     = maxInitialValue
        self.deStrategy          = deStrategy # deStrategy is the name of the DE function to use
        self.useClassRandomNumberMethods = useClassRandomNumberMethods

        self.scale = diffScale
        self.crossOverProbability = crossoverProb

        self.generation = 1 # for testing, actual value set on the Solve() method

        if True == self.useClassRandomNumberMethods:
            self.SetupClassRandomNumberMethods(3)
       
       
    def Solve(self):

        # a random initial population in shared memory
        sharedMemoryPopulation = multiprocessing.Array('d', [0.0] * (self.populationSize * self.parameterCount))
        for i in range(self.populationSize * self.parameterCount):
            sharedMemoryPopulation[i] = random.uniform(self.minInitialValue, self.maxInitialValue)

        # initial population assumed to have high energies
        sharedMemoryPopulationEnergies = multiprocessing.Array('d', [1.0E300] * self.populationSize)
       
        # a shared memory "best energy" for internal comparisons
        sharedMemoryBestEnergy = multiprocessing.Value('d', 1.0E300)
       
        # best solution, will be copied into by worker processes
        sharedMemoryBestSolution = multiprocessing.Array('d', [0.0] * self.parameterCount)
       
        # if solution has been reached, we are done
        sharedMemoryAtSolution = multiprocessing.Value('i', 0)

        # allows check for "generation 0"
        sharedMemoryGeneration = multiprocessing.Value('i', 0)


        initialArguments = [sharedMemoryPopulation, sharedMemoryPopulationEnergies, sharedMemoryBestEnergy, sharedMemoryBestSolution, sharedMemoryAtSolution, sharedMemoryGeneration, self]
        processPool = multiprocessing.Pool(initializer=parallelProcessPoolInitializer, initargs=initialArguments)
       
        for sharedMemoryGeneration.value in range(self.maxGenerations):
            #print 'new gen'
            if sharedMemoryAtSolution.value != 0:
                break
            processPool.map(parallelProcessFunction, range(self.populationSize))


        return sharedMemoryAtSolution.value, sharedMemoryGeneration.value, sharedMemoryBestEnergy.value, sharedMemoryBestSolution


    def SetupClassRandomNumberMethods(self, in_RandomSeed):
        #print 'Generating random numbers for recycling'
        numpy.random.seed(in_RandomSeed) # this yields same results each time Solve() is run
        self.nonStandardRandomCount = self.populationSize * self.parameterCount * 3 + 1 # the "+1" makes an odd number of array items
        if self.nonStandardRandomCount < 523: # set a minimum number of random numbers
            self.nonStandardRandomCount = 523
           
        self.ArrayOfRandomIntegersBetweenZeroAndParameterCount = numpy.random.random_integers(0, self.parameterCount-1, size=(self.nonStandardRandomCount))
        self.ArrayOfRandomRandomFloatBetweenZeroAndOne = numpy.random.uniform(size=(self.nonStandardRandomCount))
        self.ArrayOfRandomIntegersBetweenZeroAndPopulationSize = numpy.random.random_integers(0, self.populationSize-1, size=(self.nonStandardRandomCount))
        self.randCounter1 = 0
        self.randCounter2 = 0
        self.randCounter3 = 0


    def GetClassRandomIntegerBetweenZeroAndParameterCount(self):
        self.randCounter1 += 1
        if self.randCounter1 >= self.nonStandardRandomCount:
            self.randCounter1 = 0
        return self.ArrayOfRandomIntegersBetweenZeroAndParameterCount[self.randCounter1]

    def GetClassRandomFloatBetweenZeroAndOne(self):
        self.randCounter2 += 1
        if self.randCounter2 >= self.nonStandardRandomCount:
            self.randCounter2 = 0
        return self.ArrayOfRandomRandomFloatBetweenZeroAndOne[self.randCounter2]
       
    def GetClassRandomIntegerBetweenZeroAndPopulationSize(self):
        self.randCounter3 += 1
        if self.randCounter3 >= self.nonStandardRandomCount:
            self.randCounter3 = 0
        return self.ArrayOfRandomIntegersBetweenZeroAndPopulationSize[self.randCounter3]


    # this class might normally be subclassed and this method overridden, or the
    # externalEnergyFunction set and this method used directly
    def EnergyFunction(self, trial):
        try:
            energy = self.externalEnergyFunction(trial)
        except ArithmeticError:
            energy = 1.0E300 # high energies for arithmetic exceptions
        except FloatingPointError:
            energy = 1.0E300 # high energies for floating point exceptions

        # we will be "done" if the energy is less than or equal to the cutoff energy
        if energy <= self.cutoffEnergy:
            return energy, True
        else:
            return energy, False

    def Best1Exp(self, candidate):
        r1,r2,r3,r4,r5 = self.SelectSamples(candidate, 1,1,0,0,0)
        if True == self.useClassRandomNumberMethods:
            n = self.GetClassRandomIntegerBetweenZeroAndParameterCount()
        else:
            n = random.randint(0, self.parameterCount-1)

        self.trialSolution = sharedMemoryPopulation[candidate * self.parameterCount  :  candidate * self.parameterCount + self.parameterCount]
        i = 0
        while(1):
            if True == self.useClassRandomNumberMethods:
                k = self.GetClassRandomFloatBetweenZeroAndOne()
            else:
                k = random.uniform(0.0, 1.0)
            if k >= self.crossOverProbability or i == self.parameterCount:
                break
            self.trialSolution[n] = self.bestSolution[n] + self.scale * (sharedMemoryPopulation[r1 * self.parameterCount + n] - sharedMemoryPopulation[r2 * self.parameterCount + n])
            n = (n + 1) % self.parameterCount
            i += 1


    def Rand1Exp(self, candidate):
        r1,r2,r3,r4,r5 = self.SelectSamples(candidate, 1,1,1,0,0)
        if True == self.useClassRandomNumberMethods:
            n = self.GetClassRandomIntegerBetweenZeroAndParameterCount()
        else:
            n = random.randint(0, self.parameterCount-1)

        self.trialSolution = sharedMemoryPopulation[candidate * self.parameterCount  :  candidate * self.parameterCount + self.parameterCount]
        i = 0
        while(1):
            if True == self.useClassRandomNumberMethods:
                k = self.GetClassRandomFloatBetweenZeroAndOne()
            else:
                k = random.uniform(0.0, 1.0)
            if k >= self.crossOverProbability or i == self.parameterCount:
                break
            self.trialSolution[n] = sharedMemoryPopulation[r1 * self.parameterCount + n] + self.scale * (sharedMemoryPopulation[r2 * self.parameterCount + n] - sharedMemoryPopulation[r3 * self.parameterCount + n])
            n = (n + 1) % self.parameterCount
            i += 1


    def RandToBest1Exp(self, candidate):
        r1,r2,r3,r4,r5 = self.SelectSamples(candidate, 1,1,0,0,0)
        if True == self.useClassRandomNumberMethods:
            n = self.GetClassRandomIntegerBetweenZeroAndParameterCount()
        else:
            n = random.randint(0, self.parameterCount-1)

        self.trialSolution = sharedMemoryPopulation[candidate * self.parameterCount  :  candidate * self.parameterCount + self.parameterCount]
        i = 0
        while(1):
            if True == self.useClassRandomNumberMethods:
                k = self.GetClassRandomFloatBetweenZeroAndOne()
            else:
                k = random.uniform(0.0, 1.0)
            if k >= self.crossOverProbability or i == self.parameterCount:
                break
            self.trialSolution[n] += self.scale * (self.bestSolution[n] - self.trialSolution[n]) + self.scale * (sharedMemoryPopulation[r1 * self.parameterCount + n] - sharedMemoryPopulation[r2 * self.parameterCount + n])
            n = (n + 1) % self.parameterCount
            i += 1


    def Best2Exp(self, candidate):
        r1,r2,r3,r4,r5 = self.SelectSamples(candidate, 1,1,1,1,0)
        if True == self.useClassRandomNumberMethods:
            n = self.GetClassRandomIntegerBetweenZeroAndParameterCount()
        else:
            n = random.randint(0, self.parameterCount-1)

        self.trialSolution = sharedMemoryPopulation[candidate * self.parameterCount  :  candidate * self.parameterCount + self.parameterCount]
        i = 0
        while(1):
            if True == self.useClassRandomNumberMethods:
                k = self.GetClassRandomFloatBetweenZeroAndOne()
            else:
                k = random.uniform(0.0, 1.0)
            if k >= self.crossOverProbability or i == self.parameterCount:
                break
            self.trialSolution[n] = self.bestSolution[n] + self.scale * (sharedMemoryPopulation[r1 * self.parameterCount + n] + sharedMemoryPopulation[r2 * self.parameterCount + n] - sharedMemoryPopulation[r3 * self.parameterCount + n] - sharedMemoryPopulation[r4 * self.parameterCount + n])
            n = (n + 1) % self.parameterCount
            i += 1


    def Rand2Exp(self, candidate):
        r1,r2,r3,r4,r5 = self.SelectSamples(candidate, 1,1,1,1,1)
        if True == self.useClassRandomNumberMethods:
            n = self.GetClassRandomIntegerBetweenZeroAndParameterCount()
        else:
            n = random.randint(0, self.parameterCount-1)

        self.trialSolution = sharedMemoryPopulation[candidate * self.parameterCount  :  candidate * self.parameterCount + self.parameterCount]
        i = 0
        while(1):
            if True == self.useClassRandomNumberMethods:
                k = self.GetClassRandomFloatBetweenZeroAndOne()
            else:
                k = random.uniform(0.0, 1.0)
            if k >= self.crossOverProbability or i == self.parameterCount:
                break
            self.trialSolution[n] = sharedMemoryPopulation[r1 * self.parameterCount + n] + self.scale * (sharedMemoryPopulation[r2 * self.parameterCount + n] + sharedMemoryPopulation[r3 * self.parameterCount + n] - sharedMemoryPopulation[r4 * self.parameterCount + n] - sharedMemoryPopulation[r5 * self.parameterCount + n])
            n = (n + 1) % self.parameterCount
            i += 1


    def Best1Bin(self, candidate):
        r1,r2,r3,r4,r5 = self.SelectSamples(candidate, 1,1,0,0,0)
        if True == self.useClassRandomNumberMethods:
            n = self.GetClassRandomIntegerBetweenZeroAndParameterCount()
        else:
            n = random.randint(0, self.parameterCount-1)

        self.trialSolution = sharedMemoryPopulation[candidate * self.parameterCount  :  candidate * self.parameterCount + self.parameterCount]
        i = 0
        while(1):
            if True == self.useClassRandomNumberMethods:
                k = self.GetClassRandomFloatBetweenZeroAndOne()
            else:
                k = random.uniform(0.0, 1.0)
            if k >= self.crossOverProbability or i == self.parameterCount:
                break
            self.trialSolution[n] = self.bestSolution[n] + self.scale * (sharedMemoryPopulation[r1 * self.parameterCount + n] - sharedMemoryPopulation[r2 * self.parameterCount + n])
            n = (n + 1) % self.parameterCount
            i += 1


    def Rand1Bin(self, candidate):
        r1,r2,r3,r4,r5 = self.SelectSamples(candidate, 1,1,1,0,0)
        if True == self.useClassRandomNumberMethods:
            n = self.GetClassRandomIntegerBetweenZeroAndParameterCount()
        else:
            n = random.randint(0, self.parameterCount-1)

        self.trialSolution = sharedMemoryPopulation[candidate * self.parameterCount  :  candidate * self.parameterCount + self.parameterCount]
        i = 0
        while(1):
            if True == self.useClassRandomNumberMethods:
                k = self.GetClassRandomFloatBetweenZeroAndOne()
            else:
                k = random.uniform(0.0, 1.0)
            if k >= self.crossOverProbability or i == self.parameterCount:
                break
            self.trialSolution[n] = sharedMemoryPopulation[r1 * self.parameterCount + n] + self.scale * (sharedMemoryPopulation[r2 * self.parameterCount + n] - sharedMemoryPopulation[r3 * self.parameterCount + n])
            n = (n + 1) % self.parameterCount
            i += 1


    def RandToBest1Bin(self, candidate):
        r1,r2,r3,r4,r5 = self.SelectSamples(candidate, 1,1,0,0,0)
        if True == self.useClassRandomNumberMethods:
            n = self.GetClassRandomIntegerBetweenZeroAndParameterCount()
        else:
            n = random.randint(0, self.parameterCount-1)

        self.trialSolution = sharedMemoryPopulation[candidate * self.parameterCount  :  candidate * self.parameterCount + self.parameterCount]
        i = 0
        while(1):
            if True == self.useClassRandomNumberMethods:
                k = self.GetClassRandomFloatBetweenZeroAndOne()
            else:
                k = random.uniform(0.0, 1.0)
            if k >= self.crossOverProbability or i == self.parameterCount:
                break
            self.trialSolution[n] += self.scale * (self.bestSolution[n] - self.trialSolution[n]) + self.scale * (sharedMemoryPopulation[r1 * self.parameterCount + n] - sharedMemoryPopulation[r2 * self.parameterCount + n])
            n = (n + 1) % self.parameterCount
            i += 1


    def Best2Bin(self, candidate):
        r1,r2,r3,r4,r5 = self.SelectSamples(candidate, 1,1,1,1,0)
        if True == self.useClassRandomNumberMethods:
            n = self.GetClassRandomIntegerBetweenZeroAndParameterCount()
        else:
            n = random.randint(0, self.parameterCount-1)

        self.trialSolution = sharedMemoryPopulation[candidate * self.parameterCount  :  candidate * self.parameterCount + self.parameterCount]
        i = 0
        while(1):
            if True == self.useClassRandomNumberMethods:
                k = self.GetClassRandomFloatBetweenZeroAndOne()
            else:
                k = random.uniform(0.0, 1.0)
            if k >= self.crossOverProbability or i == self.parameterCount:
                break
            self.trialSolution[n] = self.bestSolution[n] + self.scale * (sharedMemoryPopulation[r1 * self.parameterCount + n] + sharedMemoryPopulation[r2 * self.parameterCount + n] - sharedMemoryPopulation[r3 * self.parameterCount + n] - sharedMemoryPopulation[r4 * self.parameterCount + n])
            n = (n + 1) % self.parameterCount
            i += 1


    def Rand2Bin(self, candidate):
        r1,r2,r3,r4,r5 = self.SelectSamples(candidate, 1,1,1,1,1)
        if True == self.useClassRandomNumberMethods:
            n = self.GetClassRandomIntegerBetweenZeroAndParameterCount()
        else:
            n = random.randint(0, self.parameterCount-1)

        self.trialSolution = sharedMemoryPopulation[candidate * self.parameterCount  :  candidate * self.parameterCount + self.parameterCount]
        i = 0
        while(1):
            if True == self.useClassRandomNumberMethods:
                k = self.GetClassRandomFloatBetweenZeroAndOne()
            else:
                k = random.uniform(0.0, 1.0)
            if k >= self.crossOverProbability or i == self.parameterCount:
                break
            self.trialSolution[n] = sharedMemoryPopulation[r1 * self.parameterCount + n] + self.scale * (sharedMemoryPopulation[r2 * self.parameterCount + n] + sharedMemoryPopulation[r3 * self.parameterCount + n] - sharedMemoryPopulation[r4 * self.parameterCount + n] - sharedMemoryPopulation[r5 * self.parameterCount + n])
            n = (n + 1) % self.parameterCount
            i += 1


    def SelectSamples(self, candidate, r1, r2, r3, r4, r5):
        if r1:
            while(1):
                if True == self.useClassRandomNumberMethods:
                    r1 = self.GetClassRandomIntegerBetweenZeroAndPopulationSize()
                else:
                    r1 = random.randint(0, self.populationSize-1)
                if r1 != candidate:
                    break
        if r2:
            while(1):
                if True == self.useClassRandomNumberMethods:
                    r2 = self.GetClassRandomIntegerBetweenZeroAndPopulationSize()
                else:
                    r2 = random.randint(0, self.populationSize-1)
                if r2 != candidate and r2 != r1:
                    break
        if r3:
            while(1):
                if True == self.useClassRandomNumberMethods:
                    r3 = self.GetClassRandomIntegerBetweenZeroAndPopulationSize()
                else:
                    r3 = random.randint(0, self.populationSize-1)
                if r3 != candidate and r3 != r1 and r3 != r2:
                    break
        if r4:
            while(1):
                if True == self.useClassRandomNumberMethods:
                    r4 = self.GetClassRandomIntegerBetweenZeroAndPopulationSize()
                else:
                    r4 = random.randint(0, self.populationSize-1)
                if r4 != candidate and r4 != r1 and r4 != r2 and r4 != r3:
                    break
        if r5:
            while(1):
                if True == self.useClassRandomNumberMethods:
                    r5 = self.GetClassRandomIntegerBetweenZeroAndPopulationSize()
                else:
                    r5 = random.randint(0, self.populationSize-1)
                if r5 != candidate and r5 != r1 and r5 != r2 and r5 != r3 and r5 != r4:
                    break

        return r1, r2, r3, r4, r5

Attachment (testParallelDESolver.py): text/x-python, 2373 bytes
Attachment (ParallelDESolver.py): text/x-python, 20 KiB
_______________________________________________
SciPy-User mailing list
SciPy-User <at> scipy.org
http://mail.scipy.org/mailman/listinfo/scipy-user
Esmail | 1 Aug 2009 18:13
Picon
Favicon

gnuplot.py xrange/shifting graph not working

Hi

Need some basic help with Gnuplot.py - it must a simple thing I'm not
getting.

I can't get xrange to work as expected, the axis shifts, but the data
doesn't get plotted accordingly. Best explained with an example. If
you enter the following in gnuplot directly/interactively:

----------------------
gnuplot> set xrange[-5:5]
gnuplot> plot x*x
----------------------

This is what I want to be able to do via a python script where I pass in
the data for x and y in list.

I have the following code:

-------------------

#!/usr/bin/env python

import Gnuplot

g = Gnuplot.Gnuplot(debug=1)

x = range(-5,5)
y = []
for i in x:
     y.append(i*i)

g('set style data linespoint')
#g('set xrange[-5:5]')
g.plot(y)

-------------------

the plot is centered around 5 (tick marks go from 0 to 10). If I use

g('set xrange[-5:5]')

to center it around 0, I only get half of the graph (tick marks go
from -5 to 5).

I also tried g.plot(x, y) w/o luck

Is there a way to get this to work as the interactive gnuplot example
above does?

I think I may be missing something obvious ...

Thanks!
David Cournapeau | 1 Aug 2009 18:21
Picon
Gravatar

Re: [SciPy-User] SciPy Foundation

Hi Joe,

On Sat, Aug 1, 2009 at 2:06 AM, Joe Harrington<jh <at> physics.ucf.edu> wrote:

>
> I define success as popular adoption in preference to commercial
> packages.  I believe in vote-with-your-feet: this goal will not be
> reached until all aspects of the package and its presentation to the
> world exceed those of our commercial competition.  Scipy is now a
> grass roots effort, but that takes it only so far.  Other projects,
> such as OpenOffice and Sage, don't follow this model and do produce
> quality products that compete with commercial offerings, at least on
> open-source platforms.

I am not sure openoffice is a good example, but I share the sentiment
that something is missing in the organization of the community.

I think it is very important to keep in mind that in any open source
project, telling people what to do does not work well. Not everybody
will share the same goals, are interested in scipy in the same way,
etc...  So any structure should help people doing what they want for
scipy's sake, but above all, should not alienate anyone who would have
worked on scipy otherwise. It may just be rhetoric, but saying that
"it would be nice for scipy to have this goal" instead of "we should
do this" matters IMHO.

Some of the things I am missing:
 - no quantifiable feedback from users: if we want to work on a set of
features, we cannot prioritize.  Likewise, we have very little
statistics on usage, platforms, etc... OTOH, this is often hard to
obtain for open source projects.
 - a scipy foundation: several times already, I have been asked
privately to do add some feature to scipy, generally things which
takes a few hours max, in exchange for some money. It is too much of a
hassle to set up things to get money for a few hours work, and
frankly, for a few hours, I would prefer to ask people to give money
to a scipy foundation instead. Something like the R foundation
(http://www.r-project.org/foundation/main.html). A foundation with a
legal status would make the situation much easier w.r.t donations I
believe. It should not be that hard to set up.
 - website: I think the root of the problem is lack of a dedicated
person for it, a person with design skills ideally, to design a
coherent graphic "chart" (not sure about the exact English word),
etc... I don't know how to get volunteers for this: it seems like many
projects manage to have such volunteers.

About the more particular points you raised:

> - Packaging
>  - Personal Package Archive or equivalent for every release of every
>    OS for the full toolstack (There are tools that do this but we
>    don't use them.  NSF requires Metronome - http://nmi.cs.wisc.edu/
>    - for funding most development grants, so right now we're not even
>    on NSF's radar.)
>  - Track record of having the whole toolstack installation "just
>    work" in a few command lines or clicks for *everyone*
>  - Regular, scheduled releases of numpy and scipy
>  - Coordinated releases of numpy, scipy, and stable scikits into PPA system

The problem of packaging is that it is hard to do well, but has no
technically challenging part in it. And it usually does not fall into
the "scratching ones' itch", because once you know how to build the
software, you are done and usually want to start using the damn thing.
Worse, it needs to be done every-time (every release). So this is
fundamentally different than doc: having done a great packaging work
for version N is useless after N+1 is out. It does not make sense to
pay someone to do it once.

Having some infrastructure would help: for example, something which
automatically builds packages on a set of supported platforms. It has
to be 100 % automatic, so that pushing one button get you the sources,
build the package, install it, and test it. This costs money and time
to set up.

> - Public communication
>  - A real marketing plan
>  - Executing on that plan
>  - Web site geared toward multiple audiences, run by experts at that
>    kind of communication
>  - More webinars, conference booths, training, aimed at all levels
>  - Demos, testimonials, topical forums, all showcased

Concerning communication with users, I think that the mailing lists do
not work well. It is ok for development, but it kinda sucks for
helping average users. Since I have been working on the dark side for
numpy/scipy- windows, I have been regularly using stackoverflow to ask
for some obscure windows stuff. stackoverflow is a a mix between a FAQ
and wikipedia. It works extremely well, and the user experience is way
above anything I have seen in this vein. Something like this to use
for scipy/numpy would be extremely useful I believe. It is vastly
superior to ML or wiki for focused problems ("how to do this in
matlab", "how to install on this linux distribution", etc...).

As an example of usage, R has recently used the main website so that
the most upvoted N R questions would be answered by R core developers
(during a R conference I believe). This all feels much better than ML
to me (again, as far as average user usage is concerned, not for
developer communication).

One website to handle all the user community, no need for complicated
forum rules and all (everything works with search and tags).
Stackoverflow works without any fixed hierarchy for many times more
participants that we will ever have, and much broader topics than us.

They will have soon a dedicated solution for custom websites using the
same stack - maybe something can be worked on as a open source
project.

David
_______________________________________________
Scipy-dev mailing list
Scipy-dev <at> scipy.org
http://mail.scipy.org/mailman/listinfo/scipy-dev
Esmail | 1 Aug 2009 18:36
Picon
Favicon

Re: gnuplot.py xrange/shifting graph not working

Well .. technically, this

x = range(-5,5)

should really be

x = range(-5,6)

but still doesn't solve the problem ...
nicky van foreest | 1 Aug 2009 22:19
Picon

Re: gnuplot.py xrange/shifting graph not working

This works for me. Mind that data is a list of lists, while your y is
just a list. Hence gnuplot plotted y as a function of its index, not
of x.

#!/usr/bin/env python

import Gnuplot

g = Gnuplot.Gnuplot()

x = range(-5,6)
data = []
for i in x:
    y.append(i*i)
    data.append([i,i*i])

g('set style data linespoint')
#g('set xrange[-5:5]')
g.plot(data)
g.hardcopy("elephant.ps")

For some reason Gnuplot.Gnuplot(persist = 1) does not work for me.
However, I view my results with gv. For this set

chuck~>cat .gv
GV.antialias:           True
GV.watchFile:           True
GV.watchFileFrequency:  500
GV.version:             gv 3.6.5.91
chuck~>

Hope this helps

Nicky

2009/8/1 Esmail <ebonak <at> hotmail.com>:
> Well .. technically, this
>
> x = range(-5,5)
>
> should really be
>
> x = range(-5,6)
>
> but still doesn't solve the problem ...
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User <at> scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>

Gmane