Optimization Problem with Equal and Inequal constraints

68 views
Skip to first unread message

a3725...@gmail.com

unread,
Apr 18, 2017, 8:29:27 AM4/18/17
to Inspyred
Hi there,
I'm a beginner of Inspyred, had very limited knowledge about GA(use MATLAB before) and want to use this lovely lib to construct an enginnering numerical optimization problem which may be described as follows:

Variables: x[0], x[1], x[2] .... x[n] (n > 10)

Object Funcion: [Single Objective Problem]
Min_Func(x):
'Seems like a black box function'
return Magic_Value

Inequal Constraint:
I_Constraint1(x):
'Seems like a black box function'
return Magic_value_which_must_larger_than_1

I_Constraint2(x):
'Seems like a black box function'
return Magic_value_which_must_larger_than_2
....Some similar Function...

Equal Constraint:
E_Constraint1(x):
'Seems like a black box function'
return Magic_value_which_must_equal_zero

I'm a little bit confused by the code structure of Inspyred, I see that there are some benchmarks like Rastrigin and Branin Hoo Class to be an example of Optimization and run them successfully, but I haven't seen any method to build equal or inequal constraint. What is the right way to build such a problem ?

Many Thanks




Aaron Garrett

unread,
Apr 18, 2017, 1:51:41 PM4/18/17
to Inspyred
Without knowing the specifics, I would guess you'd want to use penalties for those constraints. Here's a goofy example:

import inspyred
import random
import time


def Min_Func(x):
    'Seems like a black box function'
    Magic_Value = sum(x)
    return Magic_Value

def I_Constraint1(x):
    'Seems like a black box function'
    Magic_value_which_must_larger_than_1 = sum([even for even in x[0::2]])
    return Magic_value_which_must_larger_than_1

def I_Constraint2(x):
    'Seems like a black box function'
    Magic_value_which_must_larger_than_2 = x[-1]
    return Magic_value_which_must_larger_than_2

def E_Constraint1(x):
    'Seems like a black box function'
    Magic_value_which_must_equal_zero = x[-1] - x[0]
    return Magic_value_which_must_equal_zero


def my_generator(random, args):
    n = args.get('n', 11)
    return [random.uniform(0, 3) for _ in range(n)]
    
@inspyred.ec.evaluators.evaluator
def my_evaluator(candidate, args):
    penalty = [10, 15, 50]
    minfun = Min_Func(candidate)
    i1 = I_Constraint1(candidate)
    i1 = 0 if i1 > 1 else abs(1 - i1)
    i2 = I_Constraint2(candidate)
    i2 = 0 if i2 > 2 else abs(2 - i2)
    e1 = abs(E_Constraint1(candidate))
    return sum([c * v for c, v in zip([1] + penalty, [minfun, i1, i2, e1])])

    
prng = random.Random()
prng.seed(time.time())
ec = inspyred.ec.EvolutionaryComputation(prng)
ec.selector = inspyred.ec.selectors.tournament_selection
ec.variator = inspyred.ec.variators.gaussian_mutation
ec.replacer = inspyred.ec.replacers.generational_replacement
ec.terminator = inspyred.ec.terminators.evaluation_termination
final_pop = ec.evolve(generator=my_generator,
                      evaluator=my_evaluator,
                      pop_size=100,
                      maximize=False,
                      n=11,
                      bounder=inspyred.ec.Bounder(0, 3),
                      max_evaluations=20000,
                      mutation_rate=0.1,
                      num_elites=1)
                      
# Sort and print the best individual, who will be at index 0.
final_pop.sort(reverse=True)
print(final_pop[0])


--
You received this message because you are subscribed to the Google Groups "Inspyred" group.
To unsubscribe from this group and stop receiving emails from it, send an email to inspyred+u...@googlegroups.com.
To post to this group, send email to insp...@googlegroups.com.
Visit this group at https://groups.google.com/group/inspyred.
For more options, visit https://groups.google.com/d/optout.
--
Aaron Garrett, Ph.D.
Associate Professor
Computer Science
131 Ayers Hall
Jacksonville State University
Jacksonville, AL  36265

Will Tsing

unread,
Apr 22, 2017, 11:29:09 PM4/22/17
to Inspyred
Really thanks Garrett,

    Very detailed and helpful example.

    So here's one more kind of stupid question:
        How to make an exist parameters (baseline reference of an opt problem) be an individual at start ?



Aaron Garrett

unread,
Apr 23, 2017, 10:08:37 AM4/23/17
to Inspyred
Pass it in as a seed.

baseline_candidate = [8, 6, 7, 5, 3, 0, 9]
...
ec.evolve(my_generator,
          my_evaluator,
          pop_size=100,
          seeds=[baseline_candidate],
          ...) 


--
You received this message because you are subscribed to the Google Groups "Inspyred" group.
To unsubscribe from this group and stop receiving emails from it, send an email to inspyred+u...@googlegroups.com.
To post to this group, send email to insp...@googlegroups.com.
Visit this group at https://groups.google.com/group/inspyred.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages