EngineersGarage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise
You are here: Home / Topics / Genetic algorithm for security constrained economic dispatch

Genetic algorithm for security constrained economic dispatch

|

Projects › Projects › Genetic algorithm for security constrained economic dispatch

  • This topic has 3 replies, 2 voices, and was last updated 11 years, 4 months ago by Kamwana.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • February 10, 2014 at 7:27 am #2852
    Kamwana
    Participant

    hey guys:

     

    im new to using genetic algorithms(GA).started learning it early december last year and i have come up with an algorithm describing the security constrained economic dispatch but it does not run.i have tried asking around for help but no one has come around it.Any assistance with what i did wrong in writing the algorithm would be highly appreciated.here is the algorithm below:thank you in advance engineers!

    #include <stdio.h>

    #include <stdlib.h>

    #define NUMBER_ORGANISMS 100

    #define NUMBER_GENES     20

    #define ALLELES          4

    #define MUTATION_RATE    0.001

    #define MAXIMUM_FITNESS NUMBER_GENES

    #define FALSE 0

    #define TRUE  1

    // global variables

    char **currentGeneration, **nextGeneration;

    char *modelOrganism;

    int *organismsFitnesses;

    int totalOfFitnesses;

     

    // function declarations

    void AllocateMemory(void);

    int DoOneRun(void);

      void InitializeOrganisms(void);

      int EvaluateOrganisms(void);

      void ProduceNextGeneration(void);

        int SelectOneOrganism(void);

    // functions

    int main(){

      int finalGeneration;

      AllocateMemory();

      finalGeneration = DoOneRun();

      printf(“The final generation was: %dn”,

             finalGeneration);

    }

     

    void AllocateMemory(void){

      int organism;

      currentGeneration =

        (char**)malloc(sizeof(char*) * NUMBER_ORGANISMS);

      nextGeneration =

        (char**)malloc(sizeof(char*) * NUMBER_ORGANISMS);

      modelOrganism =

        (char*)malloc(sizeof(char) * NUMBER_GENES);

      organismsFitnesses =

        (int*)malloc(sizeof(int) * NUMBER_ORGANISMS);

      for(organism=0; organism<NUMBER_ORGANISMS; ++organism){

        currentGeneration[organism] =

          (char*)malloc(sizeof(char) * NUMBER_GENES);

        nextGeneration[organism] =

          (char*)malloc(sizeof(char) * NUMBER_GENES);

      }

    }

    int DoOneRun(void){

      int generations = 1;

      int perfectGeneration = FALSE;

      InitializeOrganisms();

      while(TRUE){

        perfectGeneration = EvaluateOrganisms();

        if( perfectGeneration==TRUE ) return generations;

        ProduceNextGeneration();

        ++generations;

      }

    }

    void InitializeOrganisms(void){

      int organism;

      int gene;

      // initialize the normal organisms

      for(organism=0; organism<NUMBER_ORGANISMS; ++organism){

        for(gene=0; gene<NUMBER_GENES; ++gene){

          currentGeneration[organism][gene] = rand()%ALLELES;

        }

      }

    // initialize the model organism

      for(gene=0; gene<NUMBER_GENES; ++gene){

        modelOrganism[gene] = rand()%ALLELES;

      }

    }

    int EvaluateOrganisms(void){

      int organism;

      int gene;

      int currentOrganismsFitnessTally;

      totalOfFitnesses = 0;

      for(organism=0; organism<NUMBER_ORGANISMS; ++organism){

        currentOrganismsFitnessTally = 0;

        // tally up the current organism’s fitness

        for(gene=0; gene<NUMBER_GENES; ++gene){

          if( currentGeneration[organism][gene]

              == modelOrganism[gene] ){

            ++currentOrganismsFitnessTally;

          }

        }

     

    // save the tally in the fitnesses data structure

        // and add its fitness to the generation’s total

        organismsFitnesses[organism] =

          currentOrganismsFitnessTally;

        totalOfFitnesses += currentOrganismsFitnessTally;

        // check if we have a perfect generation

        if( currentOrganismsFitnessTally == MAXIMUM_FITNESS ){

          return TRUE;

        }

      }

      return FALSE;

    }

    void ProduceNextGeneration(void){

      int organism;

      int gene;

      int parentOne;

      int parentTwo;

      int crossoverPoint;

      int mutateThisGene;

      // fill the nextGeneration data structure with the

      // children

      for(organism=0; organism<NUMBER_ORGANISMS; ++organism){

        parentOne = SelectOneOrganism();

        parentTwo = SelectOneOrganism();

        crossoverPoint  =  rand() % NUMBER_GENES;

        for(gene=0; gene<NUMBER_GENES; ++gene){

     

    // copy over a single gene

          mutateThisGene = rand() % (int)(1.0 / MUTATION_RATE);

          if(mutateThisGene == 0){

            // we decided to make this gene a mutation

            nextGeneration[organism][gene] = rand() % ALLELES;

          } else {

            // we decided to copy this gene from a parent

            if (gene < crossoverPoint){

              nextGeneration[organism][gene] =

                currentGeneration[parentOne][gene];

            } else {

              nextGeneration[organism][gene] =

                currentGeneration[parentTwo][gene];

            }

          }

        }

      }

    // copy the children in nextGeneration into

      // currentGeneration

      for(organism=0; organism<NUMBER_ORGANISMS; ++organism){

        for(gene=0; gene<NUMBER_GENES; ++gene){

          currentGeneration[organism][gene] =

            nextGeneration[organism][gene];

        }

      }

    }

    int SelectOneOrganism(void){

      int organism;

      int runningTotal;

      int randomSelectPoint;

      runningTotal = 0;

      randomSelectPoint = rand() % (totalOfFitnesses + 1);

      for(organism=0; organism<NUMBER_ORGANISMS; ++organism){

        runningTotal += organismsFitnesses[organism];

        if(runningTotal >= randomSelectPoint) return organism;

      }

    }

     

    February 11, 2014 at 11:02 am #11001
    AJISH ALFRED
    Participant

    Hi,

    There are different methods to represent an algorithm, you went for the most difficult one. Would suggest to prepare a flow chart and post it as an image.

    February 11, 2014 at 11:02 am #11002
    AJISH ALFRED
    Participant

    Hi,

    There are different methods to represent an algorithm, you went for the most difficult one. Would suggest to prepare a flow chart and post it as an image.

    February 13, 2014 at 12:12 pm #11018
    Kamwana
    Participant

    Hi Mr.Ajish.Thank you for your response.i will post it after a few hours or on Friday

  • Author
    Posts
Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.
Log In

RSS Recent Posts

  • PIC KIT 3 not able to program dsPIC June 20, 2025
  • Wideband matching an electrically short bowtie antenna; 50 ohm, 434 MHz June 20, 2025
  • using a RTC in SF basic June 20, 2025
  • Relay buzzing after transformer change? June 20, 2025
  • Back to the old BASIC days June 19, 2025

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise