3

I'm trying to solve a problem in R using the package genalg, specifically this package because I need a GA package that is understandable.

The problem is that I must build fire stations near cities, but I'm aiming for the minimum amount of firetrucks. They must be no more than 15 minutes away from one another. The data indicates the time each city is from one another. I have made constraints for each city and ran the IP in excel so I know what the answer should be. I commented out the constraints, because I couldn't figure out how to associate them with the variable I use to store the chromosomes solved for within the GA. I also added in a penalty to try to keep the values in line with the constraints. If anyone knows of a good tutorial for genalg aside from the CRAN book, I'd appreciate any assistance.

Here is the code:

#initilaize library
library(genalg)

#insert data

#set objective
#minimize the sume of x1,x2,x3,x4,x5,x6
datat = data.frame(city1 = c(0, 10, 20, 30, 30, 20), 
                   city2 = c(10, 0, 25, 35, 15, 30),
                   city3 = c(20, 25, 0, 15, 30, 20),
                   city4 = c(30, 35, 15, 0, 15, 25),
                   city5 = c(30, 15, 30, 15, 0, 14),
                   city6 = c(20, 30, 20, 25, 14, 0))

coeff = c(0, 10, 15, 15, 14, 0)

#constraints
#add in a penalty to run function
#how?? relate x to the defined variables
# Failed applications 1 x as a list
#2 x as a variable equal to 6 variables
#3 use x indices instead (zero length?)
#4 changed the dot product to a variable > 0
#5
evalFun = function(x){
  coefftot = x %*% coeff
  # x1 + x2 + x3 + x4 + x5 + x6 >= 1
  # x2 + x4 + x6 >= 2
  # x3 + x4 >= 1
  # x4 + x5 + x6 >= 2
  # x2 + x4 + x5 + x6 >= 3
  # x5 + x6 >= 1
  # if (x5 + x6 < 1) 
  #   #return(0)
  #   x[5] = 1
  if (coefftot <= 0) {
    return(0) else
      return(coefftot)

  }

}



#create function to create shortest path
lpiter = rbga.bin(size = 6,
                  popSize = 100,
                  iters = 100,
                  mutationChance = .01,
                  elitism = T,
                  evalFunc = evalFun)
2
  • Maybe check r-bloggers.com/genetic-algorithms-a-simple-r-example Commented Feb 5, 2018 at 19:32
  • Thanks for the suggestion, I actually checked them before when I was working on knapsack problems. I found the solution was to make the constraints in forms of vectors. Thanks for the help though. Commented Feb 7, 2018 at 19:53

1 Answer 1

0

In order to relate constraints to the variable in the problem I made a vector accociated with the constraints.Then I created an equation to relate them. For example

  # x5 + x6 >= 1
  x56 <- c(0, 0, 0, 0, 1, 1)
  con56 <-  sum(x*x56)

There is probably an easier way, but I associated them this way. If anyone has a more efficient way I'm intersted in hearing it. When running this through an if statement, the replacement is coming up as zero, so I assume there is a more efficient option.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.