Click here to Skip to main content
15,903,012 members
Home / Discussions / Visual Basic
   

Visual Basic

 
Questionvb 2017 : AI : Genetic Programming Pin
bluatigro4-Sep-17 1:33
bluatigro4-Sep-17 1:33 
AnswerRe: vb 2017 : AI : Genetic Programming Pin
Arthur V. Ratz4-Sep-17 1:59
professionalArthur V. Ratz4-Sep-17 1:59 
GeneralRe: vb 2017 : AI : Genetic Programming Pin
bluatigro5-Sep-17 22:10
bluatigro5-Sep-17 22:10 
GeneralRe: vb 2017 : AI : Genetic Programming Pin
Arthur V. Ratz5-Sep-17 22:40
professionalArthur V. Ratz5-Sep-17 22:40 
GeneralRe: vb 2017 : AI : Genetic Programming Pin
Arthur V. Ratz6-Sep-17 4:49
professionalArthur V. Ratz6-Sep-17 4:49 
GeneralRe: vb 2017 : AI : Genetic Programming Pin
Arthur V. Ratz6-Sep-17 4:56
professionalArthur V. Ratz6-Sep-17 4:56 
GeneralRe: vb 2017 : AI : Genetic Programming Pin
bluatigro6-Sep-17 21:56
bluatigro6-Sep-17 21:56 
GeneralRe: vb 2017 : AI : Genetic Programming Pin
bluatigro7-Sep-17 1:21
bluatigro7-Sep-17 1:21 
i have used my liberty/just code
to expand the code

WARNING :
i dont think i got it al right
please look at it

how do i do :
wrd.count() [ see code ]
'' bluatigro 7 sept 2017
'' genetic programming module

Module Module1
    '' function gene's
    Public Const gp_add As String = "[ + # # ]"
    Public Const gp_sub As String = "[ - # # ]"
    Public Const gp_mul As String = "[ * # # ]"
    Public Const gp_div As String = "[ / # # ]"
    Public Const gp_sqrt As String = "[ sqrt # # ]"
    public const vars as string = "xyzdefgh"
    Public Class GeneProg
        Private genes As Collection
        Private Enum numMode As Integer
            OnlyInputs = 0
            AsDouble = 1
            AsInteger = 2
        End Enum
        Private gpstate As numMode
        private varMax as int16
        private var(8) as double
        private growthrate as double
        private progLenMax as int16

        Public Sub New()
            gpstate = numMode.OnlyInputs
            varmax = 0
            growthrate = 0.2
            progLenMax = 200
        End Sub

        Public Sub use(gen As String)
        '' for activation of a functiongen or number 
            genes.Add(gen)
        End Sub

        public sub setVarMax( m as int16 )
        '' how many variables seting
            if m < 1 or m > len(vars) then exit sub
            dim i as int16
            for i = 1 to m
                use(mid(vars, i, 1))
            next i
        end sub
        
        public sub setVar(no as int16, q as double)
        '' set variable 
        '' only as set var max is set this is useful
            if no < 1 or no > len(vars) then exit sub
            var(no) = q
        end sub
        
        public sub useIntegers()
        '' create a set of integer gene's
        '' and set writing to integer's
            dim i as int16
            for i = 0 to 31
                 use(str(2 ^ i))
                 use(str(-(2 ^ i)))
            next i
            use("0")
            gpstate = numMode.asinteger
        end sub

        public sub useDoubles()
        '' create a set of double gene's
        '' and set writing to double's
            dim i as int16
            for i = -31 to 31
                use(str(2 ^ i))
                use(str(-(2 ^ i)))
            next i
            use("0")
            gpstate = numMode.asdouble
        end sub

        private function isVar(x as string)as bool
            return len(x) = 1 and instr(vars, x)
        end function
        private function isFunctionGen(x as string) as bool
            return left(x, 1) = "["
        end function
        private function isNumber(x as string) as bool
             return val(x)<>0 or x = "0" or x = "0.0"
        end function

        Public Function run(prog As String) As String
        '' parse formula 
        '' return a double in a string if succes
        '' return "error" if iligal calculation
            While InStr(prog, "]") <> 0
                Dim eind As Int16 = InStr(prog, "]")
                Dim bgin As Int16 = eind
                While Mid(prog, bgin, 1) <> "["
                    bgin -= 1
                End While
                Dim part As String = Mid(prog _
                    , bgin, eind - bgin + 1)
                Dim q() As String = Split(part)
                Dim a As Double = Val(q(2))
                Dim b As Double = Val(q(3))
                Dim ab As Double
                Try
                    Select Case q(1)
                        Case "+"
                            ab = a + b
                        Case "-"
                            ab = a - b
                        Case "*"
                            ab = a * b
                        Case "/"
                            If b = 0 Then
                                Return "error"
                            Else
                                ab = a / b
                            End If
                        Case "sqrt"
                            ab = Math.Sqrt(a)
                        Case Else
                            Return "error"
                    End Select
                Catch ex As Exception
                    Return "error"
                End Try
                Dim l As String = Left(prog, bgin - 1)
                Dim r As String = Right(prog _
                , Len(prog) - eind)
                prog = l + Str(ab) + r
            End While
            Return prog
        End Function

        Public Function mix(pa As String, pb As String) As String
        '' crosover
        '' put a random part of pa in the place
        '' of a random part of pb 
        '' or visa versa
            Dim begina As Int16
            Dim einda As Int16
            Dim beginb As Int16
            Dim eindb As Int16
            Dim cola As New Collection
            Dim colb As New Collection
            If Rnd() < 0.5 Then
                Dim q As String = pa
                pa = pb
                pb = q
            End If
            Dim i As Integer
            For i = 1 To Len(pa)
                If Mid(pa, i, 1) = "[" Then
                    cola.Add(i)
                End If
            Next
            For i = 1 To Len(pb)
                If Mid(pb, i, 1) = "[" Then
                    colb.Add(i)
                End If
            Next
            begina = cola.Item(random)
            einda = begina
            Dim fl As Int16 = 0
            While fl > 0
                einda += 1
                If Mid(pa, einda, 1) = "]" Then fl -= 1
                If Mid(pa, einda, 1) = "[" Then fl += 1
            End While
            beginb = colb.Item(random)
            fl = 0
            While fl > 0
                eindb += 1
                If Mid(pb, eindb, 1) = "]" Then fl -= 1
                If Mid(pb, eindb, 1) = "[" Then fl += 1
            End While
            dim l as string = left(pa, begina - 1)
            dim m as string = mid(pb ,beginb ,eindb - beginb)
            dim r as string = right(pa ,len(pa) - einda + 1 )
            Return l + m + r
        End Function

        public function write( hookmax as int16 ) as string
        '' write a program whit the activated genes
             '' get a function gene for seed
             dim dice as int16 = int( rnd( 0 ) * genes.count() )
             while not( isfunctionGene( genes.item( dice ) ) )
                 dice = int( rnd( 0 ) * genes.count() )
             wend
             dim uit as string = genes.item( dice )
             dim hook as int16
             dim p as int16
             while instr( uit, "#" ) <> 0 _
             and hook < hookmax
                  p = instr( uit , "#" )
                  dice = int( rnd( 0 ) * genes.count() )
                  dim l as string = left( uit , p - 1 )
                  dim r as string = right( uit , len( uit ) - p )
                  uit = l + " " + gene.item( dice ) + r
                  if isFunctioGene( gene.item( dice ) ) then
                      hook = hook + 1
                  end if
             end while
             uit = lastsharp( uit )
             if rnd() < growthrate _
             and len( uit ) < progLenMax then
                 uit = growth( uit )
             end if
             return uit
        end function

        private function lastsharp( uit as string ) as string
        '' replace al # whit number's or var's
            while instr( uit$, "#" ) <> 0
                dim p as int16 = instr( uit , "#" )
                dim dice as int16 = int( rnd( 0 ) * genes.count() )
                while isFunctionGene( genes.item( dice ) )
                    dice = int( rnd( 0 ) * genes.count() )
                end while
                dim l as string = left( uit , p - 1 )
                dim r as string = right( uit , len( uit ) - p )
                uit = l + " " + genes.item( dice ) + r
            end while
            return uit
        end function

        private function growth( a as string )as string
        '' make the formula a longer
            dim wrd() as string = split(a)
            dim dice as int16 = int( rnd() * wrd.count() + 1 )
            while not( isVar( wrd( dice ) ) ) _
            and not( isNumber( wrd( dice ) ) )
                dice = int( rnd() * tel + 1 )
            end while
            dim atom as string = wrd( dice )
            dim dice2 as int16 = int( rnd(0) * genes.count() )
            while not( isFunctionGene( gene( dice2 ) ) )
                dice2 = int( rnd(0) * gene.count() )
            end while
            dim gen as string = genes.item( dice2 )
            uit = ""
            dim i as int16
            for i = 1 to wrd.count()
            if i = dice then
                uit = uit + gen + " "
            else
                uit = uit + wrd( i ) + " "
            end if
            next i
            return lastsharp( uit )
        end function

        public function mutate( a  as string )as string
        '' mutate formula a
            dim wrd() as string = split(a)
            ''take a atom that isnt a hook or empty
            dim dice as int16 = int( rnd() * wrd.count() )
            while wrd( dice ) = "[" _
            or    wrd( dice ) = "]" _
            or    wrd( dice ) = ""
                dice = int( rnd( 0 ) * wrd.count() )
            end while
            dim atom as string = wrd( dice )
            if isVar( atom ) then
                if rnd() < .6 then
                    atom = mid( vars _
                    , int( rnd() * varMax ) , 1 )
                else
                    select case gpstate
                        case numMode.Asintegers
                             atom = str( 2 _
                             ^ ( int( rnd() * 32 ) ) )
                        case nummode.asdoubles
                             atom = str( 2 _
                             ^ ( int( rnd() * 64 - 32 ) ) )
                        case else
                            atom = mid( vars _
                            , int( rnd() * ( varMax - 1 ) + 1 ) _
                            , 1 )
                    end select
                end if
            else
                if isNumber( atom ) then
                    select case gpstate
                        case nummode.asintegers
                            dim x as double = val( atom )
                            atom = str( x _
                            xor 2 ^ int( rnd() * 32 ) )
                        case else ''doubles
                            dim x as double = val( atom )
                            dim q as double = 2 ^ int( rnd(0) * 64 - 32 )
                            if rnd() < .5 then
                                atom = str( x - q )
                            else
                                atom = str( x + q )
                            end if
                    end select
                    if varMax > 0 then
                        if rnd(0) < .4 then
                           atom = mid( vars _
                           , int( rnd() * ( varMax - 1 ) + 1 ) _
                           , 1 )
                        end if
                    end if
                else
                '' atom is a function
                    dim q as int16 = 0
                    while not( isFunctionGene( gene.item( q ) ) )
                        q = int( rnd( 0 ) * genes.count() )
                    wend
                    dim w() as string = split( genes.item(q) )
                    atom = w( 1 )
                end if
            end if    
            dim uit as string 
            wrd(dice)=atom
            for i = 1 to tel + 2
                uit = uit + wrd( i ) + " "
            next i
            if rnd() < .2 _
            and len( uit ) < proglenmax then
                uit = growth( uit )
            end if
            return uit 
        end function 

    End Class

    Sub Main()
        Dim proga As String = "[ + 7 [ - 2 3 ] ]"
        Dim progb As String = "[ * 4 [ / 5 6 ] ]"
        Dim GP As New GeneProg()
        Console.WriteLine("[ test run ]")
        Console.WriteLine("prog a = " & proga)
        Console.WriteLine("prog b = " & progb)
        Console.WriteLine("run a = " & GP.run(proga))
        Console.WriteLine("check a = " _
        & 7.0 + (2.0 - 3.0))
        Console.WriteLine("run b = " & GP.run(progb))
        Console.WriteLine("check b =" _
        & 4.0 * (5.0 / 6.0))
        Console.WriteLine("[ push return to continue ]")
        Console.ReadKey()
        Console.WriteLine("[ test mix ]")
        Console.WriteLine("prog a = " & proga)
        Console.WriteLine("prog b = " & progb)
        Dim i As Int16
        dim c as string
        For i = 0 To 5
            c = GP.mix(proga, progb)
            Console.WriteLine("mix a b = c = " & c)
            Console.WriteLine("run c = " & c)
        Next
        Console.WriteLine("[ push return to continue ]")
        Console.ReadKey()
        console.writeline("[ test mutate ]"
        ''gp.mutate needs this :
        gp.use(gp_add)
        gp.use(gp_sub)
        gp.use(gp_mul)
        gp.use(gp_div)
        gp.use(gp_sqrt)
        gp.useintegers()
        Console.WriteLine("prog a = " & proga)
        dim progc as string
        for i = 0 to 5
            progc = gp.mutate(proga)
            console.writeline("mutate a = c = "&progc)
            consolo.writeline("run c = "&gp.run(progc))      
        next i
        console.writeline("[ push return to continue ]")
        console.readkey()
        console.writeline("[ test write ]"
        for i = 0 to 5
            progc = gp.write(6)
            console.writeline("write c = "&progc)
            consolo.writeline("run c = "&gp.run(progc))
        next i
        console.writeline("[ push return to end programma ]")
        console.readkey()      
    End Sub

End Module

GeneralRe: vb 2017 : AI : Genetic Programming Pin
Arthur V. Ratz7-Sep-17 3:15
professionalArthur V. Ratz7-Sep-17 3:15 
GeneralRe: vb 2017 : AI : Genetic Programming Pin
Arthur V. Ratz7-Sep-17 7:44
professionalArthur V. Ratz7-Sep-17 7:44 
GeneralRe: vb 2017 : AI : Genetic Programming Pin
bluatigro7-Sep-17 23:26
bluatigro7-Sep-17 23:26 
GeneralRe: vb 2017 : AI : Genetic Programming Pin
Arthur V. Ratz7-Sep-17 23:37
professionalArthur V. Ratz7-Sep-17 23:37 
QuestionImport Outlook calendar entries from Excel with VBscript? Pin
xs13x31-Aug-17 11:03
xs13x31-Aug-17 11:03 
SuggestionRe: Import Outlook calendar entries from Excel with VBscript? Pin
Ralf Meier31-Aug-17 20:47
mveRalf Meier31-Aug-17 20:47 
AnswerRe: Import Outlook calendar entries from Excel with VBscript? Pin
Chris Quinn31-Aug-17 22:20
Chris Quinn31-Aug-17 22:20 
AnswerRe: Import Outlook calendar entries from Excel with VBscript? Pin
Ralf Meier1-Sep-17 0:32
mveRalf Meier1-Sep-17 0:32 
AnswerRe: Import Outlook calendar entries from Excel with VBscript? Pin
Richard Deeming1-Sep-17 2:47
mveRichard Deeming1-Sep-17 2:47 
GeneralRe: Import Outlook calendar entries from Excel with VBscript? Pin
xs13x1-Sep-17 3:13
xs13x1-Sep-17 3:13 
GeneralRe: Import Outlook calendar entries from Excel with VBscript? Pin
Richard Deeming1-Sep-17 4:00
mveRichard Deeming1-Sep-17 4:00 
QuestionI need to load a JP2 into an ImageList. All info I've found is out of date. Pin
Member 1338764431-Aug-17 5:19
Member 1338764431-Aug-17 5:19 
QuestionMessage Removed Pin
30-Aug-17 17:49
compcanada201730-Aug-17 17:49 
QuestionOpenFileDialog - Specific Path Pin
purushotham.k929-Aug-17 6:00
purushotham.k929-Aug-17 6:00 
AnswerRe: OpenFileDialog - Specific Path Pin
A_Griffin29-Aug-17 8:07
A_Griffin29-Aug-17 8:07 
QuestionUsing Webclient.uploadfile files is never copied to website Pin
Member 1098357222-Aug-17 12:47
Member 1098357222-Aug-17 12:47 
Questionvb.net desktop app zoom feature Pin
dcof22-Aug-17 6:34
dcof22-Aug-17 6:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.