LiamRyan
LiamRyan

Reputation: 1898

Program to interfaces - how to access the calling class efficiently?

I'm creating a genetic algorithm framework and I have a population class which includes a Tournament and an EvolutionStrategy object so that I can plug in different tournament types and evolution strategies.

Currently I have set it so that I create a new EvolutionStrategy object by passing in the Population object from population -

EvolutionStrategy strategy = new BasicStrategy(this);

In BasicStrategy constructor I have

Population pop;
public BasicStrategy(Population pop)
{
    this.pop = pop;
}

I have two questions regarding this

1) Does this create the proper reference variable, allowing updates to the population to be instantly accessible in the EvolutionStrategy or should I be using some form of synchronization ?

2) Is there a better way to do this? It seems wasteful to me to have a reference variable to the strategy in the Population and a reference variable to the population in the Strategy.

Upvotes: 1

Views: 54

Answers (1)

kkonrad
kkonrad

Reputation: 1262

  1. it is fine
  2. I think that you should have one extra class like EvolutionAlgorithm in which you store Population (in terms of objects that are evolving) and EvolutionStrategy separately. When you want to evolve your population you just call evolutionStrategy's method with population as argument (Strategy Pattern). I think that evolutionStartegy doesn't need to store population. Also population can make sense without evolutionStrategy.

Upvotes: 2

Related Questions