Hi everyone. Once again, i'm here.
What I have is two structures:

Public Structure poblacion
    Public ind() As individuo
End Structure

Public Structure individuo
    Public pos as List(Of Point)  
    Public fx As Double 
End Structure

There's one point where I need to randomize the order of the numbers of the "pos" list. When I do that, it changes ALL ind() including the one I sent to the subroutine. Here's the sub of the problem:

Public Sub generarPoblacion(ByRef pob As poblacion, ByVal tpob As Integer)
        For i = 0 To tpob
            generarIndividuo(pob.ind(i).pos, nvars)
    End Sub

'I found the next code on internet to reorder the content of an array.
    Sub generarIndividuo(ByRef pos As List(Of Point), ByVal nvars As Integer)
         Dim rnd As New Random, j As Integer
        For i = 0 To nvars
            j = rnd.Next(i, nvars)
            swapPuntos(pos(i), pos(j))
    End Sub

    Sub swapPuntos(ByRef punto1 As Point, ByRef punto2 As Point)
        Dim temp As Point = punto1
        punto1 = punto2
        punto2 = temp
    End Sub

And here's an screenshot of it affecting all ind:

6 Years
Discussion Span
Last Post by Pundia

It sounds like you are reusing your same Point object in multiple places. How are you getting the Points?


It sounds like you are reusing your same Point object in multiple places. How are you getting the Points?

First I get the points by de mousedown event in the picturebox:

Private Sub imgMapa_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles imgMapa.MouseDown
        Dim myGraphics As Graphics = imgMapa.CreateGraphics
        guardar_puntos(e.X, e.Y)
        Dim myDotPen As New Pen(Color.Black, 1) '// set DOT pen color and width.
        myGraphics.DrawEllipse(myDotPen, e.X - 2, e.Y - 2, 4, 4) '// (preset pen, location.X, location.Y, width, height)
        '// draw Line.
        Dim myLinePen As New Pen(Color.Red, 1)
        'myGraphics.DrawLine(myLinePen, 35, 35, 199, 298) '// (preset pen, startLocation.X, startLocation.Y, endLocation.X, endLocation.Y)
        myGraphics.Dispose() : myDotPen.Dispose() : myLinePen.Dispose()
    End Sub

    Sub guardar_puntos(ByVal x As Integer, ByVal y As Integer)
        contador += 1 'This just counts how many points there are. I know I could have use another way to this, I'll fix it later.
        txt_contador.Text = contador  
        punto = New Point(x, y)
        lista.Add(punto)  'This saves the point
    End Sub

By the way, I changed the way I was doing it in the first post (I just kept trying) and now instead of re-arranging the points y just randomize a list of integers which are an index of the "Lista" list.

So, after I get the points from the map, I create the arrays:

Public Sub crearPoblacion(ByRef pob As poblacion, ByVal tpob As Integer)
        ReDim pob.ind(0 To tpob)
        For i = 0 To tpob
    End Sub

    Public Sub crearIndividuo(ByRef ind As individuo)
        For x = 0 To nvars   'nvars is number of points
            ReDim ind.pos(0 To nvars)
    End Sub

Edited by Pundia: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.