openmole / mgo

Purely functional genetic algorithms for multi-objective optimisation
71 stars 3 forks source link

Fitness function can not restrain #6

Open liaicheng opened 5 years ago

liaicheng commented 5 years ago

HI, that's my first experience to use multi objectives algorithm with your code. My fitness function as below:

object test_food {

    var p:Vector[Vector[Double]] = Vector.empty[Vector[Double]]
    var size = 0
    def param(vector: Vector[Vector[Double]],size:Int): Unit ={
      this.p = vector
      this.size = size
    }
    def continuous(size: Int) = Vector.fill(size)(C(2, 5.0))
    def discrete = Vector.empty

    def compute(genome: Vector[Double], d: Vector[Int]): Vector[Double] = {
      val genomeSize = genome.size
      var population :Vector[Double] = Vector.empty
      println(genome,d)
      for (i <- 0 to this.size){
        var p_tmp = (genome.map(t => t*this.p(i)(genome.indexOf(t))).sum + this.p(i)(genome.size+1))
        println(p_tmp,genomeSize, this.p(i)(genome.size+1))
        population = population:+ p_tmp
      }
      println("-----------------")
      population
    }
  }

and call function as below:

object FOODNSGAII extends App {

  import algorithm._

  test_food.p = Vector(Vector(5.913,0.455,1.437,7.169,-65 ),Vector( 2.429 ,2.467,4.931,11.969,-25 ),Vector(12.612,4.281,6.485,33.13,-120 ))
  test_food.size=2
  val nsga2 =
    NSGA2(
      mu = 100,
      lambda = 100,
      fitness = test_food.compute,
      continuous = test_food.continuous(3))

  def evolution[M[_]: Generation: Random: cats.Monad: StartTime: IO] =
    nsga2.
      until(afterGeneration(1000)).
      trace((s, is) => println(s.generation)).
      evolution

  val (finalState, finalPopulation) = NSGA2.run(new util.Random(42)) { impl =>
    import impl._
    evolution[DSL].eval
  }

  println(NSGA2.result(nsga2, finalPopulation).mkString("\n"))

}

But the result can't be restrained. Something wrong?

here is the result:

Result(Vector(2.0, 2.0, 2.0),Vector(),Vector(100.47800000000001, 39.574, 195.672)) Result(Vector(2.0000000000000004, 2.0, 2.0),Vector(),Vector(78.646, 39.726, 162.348)) Result(Vector(2.0, 2.000000000000001, 2.0),Vector(),Vector(89.562, 39.650000000000006, 179.01)) Result(Vector(2.0, 2.0000000000000004, 2.0),Vector(),Vector(89.562, 39.650000000000006, 179.01)) Result(Vector(2.000000000000001, 2.0, 2.0),Vector(),Vector(78.646, 39.726, 162.348)) Result(Vector(2.0, 2.000000000000002, 2.0),Vector(),Vector(89.562, 39.650000000000006, 179.01)) Result(Vector(2.0, 2.0000000000000018, 2.0),Vector(),Vector(89.562, 39.650000000000006, 179.01)) Result(Vector(2.0, 2.0000000000000013, 2.0),Vector(),Vector(89.562, 39.650000000000006, 179.01))

romainreuillon commented 5 years ago

Hi @liaicheng,

the fitness reaches the minimum value for you 2 objectives: Vector(2.0, 2.0, 2.0). This is the ideal point of the Pareto front you're computing since you're 2 objectives range from 2.0 to 5.0 and MGO always minimize the fitness. What do you expect ?

Romain

liaicheng commented 5 years ago

@romainreuillon thanks for your replying. Yes, I got it. What i expect is minimize to zero. Anyway , I update my fitness, it can work better.