Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WorstFitnessConstraints and PenaltyConstraints examples #82

Open
yonachache opened this issue Mar 14, 2021 · 3 comments
Open

WorstFitnessConstraints and PenaltyConstraints examples #82

yonachache opened this issue Mar 14, 2021 · 3 comments

Comments

@yonachache
Copy link

Hi,
Could you please provide an example of how to define and add WorstFitnessConstraints and PenaltyConstraints to the optimization?

Thanks!

@wildart
Copy link
Owner

wildart commented Mar 19, 2021

@johnabs
Copy link

johnabs commented Oct 24, 2021

Can I follow up here? I've tried following the tutorial to add a basic constraint to a binary GA to solve a knapsack problem, but the constraints clearly aren't being applied, despite adding them to the optimize function.

Effectively, I have 50 (for now) binary values ( x0=BitVector(zeros(50)) ) that determine the value and weight of the knapsack, such that my objective function is:

f(x)=sum(-v.*x) (negative for minimization)

and my constraint function is

c(x)=sum(w.*x)

I then construct the x bounds as lx=zeros(50) and ux=ones(50), then the constraint bounds lc=[0.0] and uc=[970.0], which is the maximum allowable weight of the knapsack.

I define my constraint for the Evolutionary.optimize function:

constraint=WorstFitnessConstraints(lx,ux,lc,uc,c)

Then I define my genetic algorithm:

gafs = GA(populationSize=100,selection=tournament(7),mutation=flip,crossover=singlepoint)

and finally try to run the optimization:

rfs = Evolutionary.optimize(f, constraint, x0, gafs, Evolutionary.Options(iterations=10000))

However, this has always converged to all ones, effectively ignoring the constraint. I've also tried using this with PenaltyConstraints with low, medium, and obscenely high values for the penalty, but with no luck.

Any help here would be awesome, as I tried to follow the tutorial to the letter with constraints, but with a bitvector instead of floats, and this is where I currently am stuck :/

P.S. I've also seen an issue using the uniform binary crossover; should I post that as a new issue?

@wildart
Copy link
Owner

wildart commented Oct 28, 2021

and my constraint function is

c(x)=sum(w.*x)

The constraint function has to return array of values, so it should be like this

c(x) = [ sum(w.*x) ]

I added new test to the knapsack problem to show usage of the constraints:

# with a constraint
fitnessFun = n -> sum(utility .* n)
cf(n) = [ sum(mass .* n) ] # constraint function
lc = [0] # lower bound for constraint function
uc = [20] # upper bound for constraint function
con = WorstFitnessConstraints(Int[], Int[], lc, uc, cf)
initpop = BitVector(rand(Bool,length(mass)))
result = Evolutionary.optimize(
x -> -fitnessFun(x), con,
initpop,
GA(
selection = roulette,
mutation = flip,
crossover = singlepoint,
mutationRate = 0.9,
crossoverRate = 0.1,
ɛ = 0.1, # Elitism
populationSize = 50,
));
println("GA:RLT:FL:SP (-objfun) => F: $(minimum(result)), C: $(Evolutionary.iterations(result))")
@test abs(Evolutionary.minimum(result)) == 21.
@test sum(mass .* Evolutionary.minimizer(result)) <= 20

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants