All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.chen0040.gp.treegp.program.OperatorSet Maven / Gradle / Ivy

There is a newer version: 1.0.14
Show newest version
package com.github.chen0040.gp.treegp.program;


import com.github.chen0040.gp.commons.EntityContainer;
import com.github.chen0040.gp.services.RandEngine;
import com.github.chen0040.gp.treegp.program.operators.IfGreaterThan;
import com.github.chen0040.gp.treegp.program.operators.IfLessThan;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * Created by xschen on 29/4/2017.
 */
public class OperatorSet extends EntityContainer implements Serializable {

   private static final long serialVersionUID = -6660969377144991417L;


   public OperatorSet(){

   }

   public boolean isReadOnly(){
      return false;
   }

   public OperatorSet makeCopy(){
      OperatorSet clone = new OperatorSet();
      clone.copy(this);
      return clone;
   }

   public void addIfLessThanOperator() {
      addIfLessThanOperator(1.0);
   }

   public void addIfLessThanOperator(double weight)
   {
      add(new IfLessThan(), weight);
   }

   public void addIfGreaterThanOperator() {
      addIfGreaterThanOperator(1.0);
   }

   public void addIfGreaterThanOperator(double weight)
   {
      add(new IfGreaterThan(), weight);
   }

   @Override
   public String toString() {
      StringBuilder sb = new StringBuilder();

      for(int i = 0; i < entities.size(); ++i){
         if(i != 0){
            sb.append("\r\n");
         }
         sb.append("operator[").append(i).append("]: ").append(entities.get(i));
      }
      return sb.toString();
   }

   public Primitive anyOther(int arity, Primitive excluded, RandEngine rand){

      List candidates = new ArrayList<>();
      double weightSum = 0;
      for(int i=0; i < entities.size(); ++i){
         Primitive op = entities.get(i);
         if(op.arity()==arity){
            weightSum += weights.get(op.getIndex());
            candidates.add(op);
         }
      }

      for(int attempts = 0; attempts < 10; attempts++){
         double r = weightSum * rand.uniform();
         double accSum = 0;

         for(int i = 0; i < candidates.size(); ++i){
            Primitive op = candidates.get(i);
            accSum += weights.get(op.getIndex());
            if(r <= accSum){
               if(op == excluded){
                  break;
               } else {
                  return op;
               }
            }
         }
      }

      return excluded;
   }

   /// 
   /// Method that is used by the "RandomBranch" initialization algorithm to obtain a random function node with arity less than s
   /// 
   /// The tree size
   /// 
   public Primitive anyWithArityLessThan(int maxArity, RandEngine randEngine)
   {
      List ops = new ArrayList<>();
      for (int i = 0; i < entities.size(); ++i)
      {
         Primitive op1 = entities.get(i);
         if (op1.arity() < maxArity)
         {
            ops.add(op1);
         }
      }
      if (ops.isEmpty()) return null;
      return ops.get(randEngine.nextInt(ops.size()));
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy