2

How to initialize a multidimensional array by loop operation in VB .Net?

I have some arrays called 'Room', 'Subject', and 'Population'.

  1. Array 'Room', contains the name of the room, i.e.

    {"RoomA", "RoomB"}
    
  2. Array 'Subject', contains the name of the subject, i.e.

    {"English", "Mathematic", "Physics", "Biology"}
    
  3. Array 'Population' is a multidimensional-array that contains randomize room taken from array 'Room'. i.e. {("RoomA", "RoomA", "RoomA", "RoomA"), ("RoomA", "RoomA", "RoomA", "RoomB"), ("RoomB", "RoomA", "RoomB", "RoomB"), .....}. The length of the array is based on the generate value and the length of the subject.

I've tried this code but it's no luck:

Dim i, j As Integer
Dim TotalSubject As Integer = Subject.Count() 
Dim TotalRoom As Integer = Room.Count() 

Dim Population(,) As String
ReDim Population(generate, TotalSubject) 'Give the 'Population' bound

For i = 0 To generate
    For j = 0 To TotalSubject
        Dim Randomize As Integer = Rnd() * TotalRoom
        Population(i, j) = Room(Randomize) '----- ERROR HERE -----'
    Next j
Next i

But while I'm execute the code, it got error message sounds "IndexOutOfRangeException was unhandled" pointed to the above marked 'ERROR HERE' code. Can you help me solved this problem?

This code is for initialize the starting value use Compact Genetic Algorithm for an optimization problem to optimize the usage of the classroom. Does anyone know this method? If yes, would you like to share the knowledge with me? I've tried to search several material for that but still can't implement it onto a code.

2 Answers 2

2

The error is with Room(Randomize) not with your array.

You state that Room is an Array but your code includes Room.Count(). Arrays have a Length, not a count. Therefore my guess is that Room is some sort of collection. Next, your random number Randomize (assigned by Rnd() * TotalRoom) has a range of 0 to TotalRoom, where TotalRoom is equal to Room.Count(). So if your collection is zero based then you are getting index out of bound when you randomly generate a value equal to your collection count. And if your collection is 1 based, then you are getting index out of bound when you randomaly generate a 0.

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

2 Comments

On randomization, to achieve even distribution, you should use Math.Floor(Rnd() * TotalRoom). If you use Math.Round(Rnd() * (TotalRoom - 1)) (or simply convert the result to an integer), the highest and lowest values only have half-a-chance of being selected compared to the values inbetween.
@Hand-E-Food: Thanks, it's work :) . I've tried using the first one you gave (Math.Floor)
0

For compact genetic algorithms, you can read the pioneer research paper by Harik,Lobo and Goldberg:

Harik, Georges R., Fernando G. Lobo, and David E. Goldberg. "The compact genetic algorithm." Evolutionary Computation, IEEE Transactions on 3.4 (1999): 287-297.

Since it is not directly related to .NET, you can have a look at the small piece of code in R package eive. The C++ file ccga.cpp in src directory is 60 lines. Cran Package - eive

There is a blog entry here which includes samples for optimizing basic functions using cga in R language.

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.