0

I am working on an optimization problem. I have X number of ambulance locations, where X ranges from 1-39.

There are 43 numbers [Ambulance Locations] to choose from (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39) , we choose 3 of them since I have 3 ambulances.

To encode my problem for `programming`, I am working on Chromosome representation:
I can only put my ambulance in three locations among 1-39 locations (Restriction).

Assume that I want to put my Ambulance on the 5th, 19th, and 31 positions. I am doing something like this for a better presentation:

Chromosome 1 = [5, 19, 31] --> 3 Ambulances

Chromosome 1= [000010000000000000100000000000100000000] - Chromosome Presentation

In the above presentation, I am turning on 5-bit, 19-bit, and 31-bit.

To achieve mutation, what can be a good way to randomly change present positions to other positions keeping the range only between 1-39 locations (Restriction)?

14
  • random.sample(range(1,40),3)? would give you 3 bit positions at random ... that you could encode with test_num = sum(1<<i for i in random.sample(range(1,40),3)) is that what you mean? then you could print it with f"{test_num:039b}" Commented Oct 5, 2022 at 2:47
  • @JoranBeasley Yes. This is something very close. It is giving me 3 binary number positions among 39 positions. Since I want to do mutation, I do not want to repeat the sequence of the generated 3 bits. I am following this formula (Combinations without repetition (Is order important: No) - hackmath.net/en/calculator/combinations-and-permutations? n=39&k=3&order=0&repeat=0). How can I avoid repetition of the generated 3 bits, keeping order is not important? for instance I want 100 samples of f"{test_num:039b}" Commented Oct 5, 2022 at 11:05
  • @JoranBeasley This is very interesting. Why are you using 1<<i ? Can you explain it a bit? Commented Oct 5, 2022 at 14:15
  • it shifts a 1 that many positions into a string... summing them will give you all 3 ones.. you would need to then keep track of combos that you had seen and just retry .... or get all the combos of 3 number from that list using something like itertools.combinations and just shuffle that and pop items off the top until its empty Commented Oct 5, 2022 at 15:49
  • @JoranBeasley Very well. Then, I think the resultant string of 39 bits will always be unique, with no duplicates? For instance, if I generate 100 or 200 strings using test_num = sum(1<<i for i in random.sample(range(1,40),3)) . These all will be unique. Am I wrong? Commented Oct 5, 2022 at 15:58

0

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.