Here's something that I did that is similar to that, it's a genetic algorithm to find out what sequence of number>operator>number... would calculate to make a given number. I wrote it in DBP, but I don't see why it wouldn't work in DBC. I'm attaching a .exe anyways, I hope that it's useful.
Rem ***** Main Source File *****
Rem Project: GA5
Rem Created: 4/10/2010 8:19:03 PM
global total_fitness#
global avg_fitness#
global GOAL as float
global loop_counter
global GENE_LENGTH
input "Number to be found: ",GOAL
cls:cls
gl=((len(str$(GOAL))+(len(str$(GOAL))-1))*2)-1
GENE_LENGTH=gl
#constant POP_SIZE 100.0
#constant CROSSOVER 0.7
#constant MUTATION 0.1
set text opaque
generate_population(POP_SIZE,GENE_LENGTH)
calculate(0,POP_SIZE)
fitness()
do
crossover()
calculate(0,POP_SIZE)
fitness()
display()
total_fitness#=0
loop
function fitness()
total_fitness#=0.0
avg_fitness#=0.0
for x=1 to POP_SIZE
total_fitness#=total_fitness#+pop_fitness(x)
next x
avg_fitness#=total_fitness#/POP_SIZE
endfunction
function display()
inc loop_counter
num=loop_counter
set cursor 0,0
print str$(avg_fitness#)
print GOAL
remstart
if loop_counter mod 250=0 and loop_counter>0
filenum=loop_counter/250
file$=str$(filenum)
open to write filenum,file$
write string filenum,str$(GOAL)
write string filenum,str$(avg_fitness#)
debug=1
endif
remend
for x=1 to POP_SIZE
if pop_fitness(x)>=oldpop#
oldpop#=pop_fitness(x)
highest=x
endif
if pop_fitness(x)=1.0
sol$=population$(x)
sol=x
endif
if debug=1
write string filenum,population$(x)
write string filenum,str$(pop_calculated(x))
write string filenum,str$(pop_fitness(x))
write string filenum," "
endif
next x
if debug=1 then close file filenum
print "Best solution: "+population$(highest)+"="+str$(pop_calculated(highest))+" "
print "Highest fitness: "+str$(pop_fitness(highest))+" "
print "Generation Number: "+str$(num)+" "
if sol$<>""
print "Solution found: "+population$(sol)+"="+str$(pop_calculated(sol))
print "Solution saved to file."
print "Click to end."
repeat
inc increment
file$="Solution"+str$(increment)+".txt"
if file exist(file$)
free_file=0
else
free_file=1
endif
until free_file=1
open to write 1,file$
write string 1,"GOAL: "+str$(GOAL)
write string 1,"GENERATION: "+str$(loop_counter)
write string 1,"GENE: "+population$(sol)
write string 1,"GENE POOL SIZE: "+str$(POP_SIZE)
write string 1,"GENE LENGTH: "+str$(GENE_LENGTH)
write string 1,"TOT FIT: "+str$(total_fitness#)
write string 1,"AVG FIT: "+str$(avg_fitness#)
wait mouse
end
endif
endfunction
function crossover()
for total=0 to POP_SIZE
cross=rnd(10)
if cross<=(CROSSOVER*100)
for elitism=1 to POP_SIZE
if highest#<=pop_fitness(elitism)
highest#=pop_fitness(elitism)
num=elitism
population$(0)=population$(num)`should allow best gene to proliferate
endif
next elitism
redo:
for get_two=1 to 2
for x=0 to POP_SIZE
cross_chance#=(pop_fitness(x)/total_fitness#)*100 `1 to 100
cross_limit#=rnd(100)
if cross_limit#<=cross_chance#
selections(get_two)=x
endif
next x
next get_two
if selections(1)=selections(2) then goto redo
if selections(1)=0 or selections(2)=0 then goto redo
if selections(1)=num or selections(2)=num then goto redo
place=rnd(GENE_LENGTH-2)+2
place2=GENE_LENGTH-place
temp1$=population$(selections(1))
temp2$=population$(selections(2))
population$(selections(1))=left$(temp1$,place)+right$(temp2$,place2)
population$(selections(2))=left$(temp2$,place)+right$(temp1$,place2)
calculate(selections(1),selections(1))
calculate(selections(2),selections(2))
endif
mutate_limit#=rnd(1000)
if mutate_limit#<=(MUTATION*1000) and total<>num
selected=total
place2=rnd(GENE_LENGTH-1)+1
place1=place2-1
place3=GENE_LENGTH-place2
if val(mid$(population$(selected),place2))>0 or mid$(population$(selected),place2)="0"
replace$=str$(rnd(9))
else
replace=rnd(3)+10
if replace=10 then replace$="+"
if replace=11 then replace$="-"
if replace=12 then replace$="*"
if replace=13 then replace$="/"
endif
temp$=population$(selected)
population$(selected)=left$(temp$,place1)+replace$+right$(temp$,place3)
calculate(selected,selected)
endif
next total
endfunction
function calculate(first,last)
temp=(GENE_LENGTH-1)/2
for x=first to last
first$=mid$(population$(x),1)
for y=1 to GENE_LENGTH
operator$=mid$(population$(x),y+1)
second$=mid$(population$(x),y+2)
if operator$="+" then first$=str$(val(first$)+val(second$))
if operator$="-" then first$=str$(val(first$)-val(second$))
if operator$="*" then first$=str$(val(first$)*val(second$))
if operator$="/" then first$=str$(val(first$)/val(second$))
next y
pop_calculated(x)=val(first$)
pop_fitness(x)=1.0/(1.0+abs(GOAL-pop_calculated(x)))
next x
endfunction
function generate_population(pop,length)
dim population$(pop)
dim pop_calculated(pop) as float
dim pop_fitness(pop) as float
dim selections(2)
for amount=1 to pop
for bits=1 to length
`if placement is odd, bit is a number
if bits mod 2 <> 0
bit=rnd(9)
result$=result$+str$(bit)
else
`if placement is even, bit is an operator
bit=rnd(3)+10
if bit=10 then result$=result$+"+"
if bit=11 then result$=result$+"-"
if bit=12 then result$=result$+"*"
if bit=13 then result$=result$+"/"
endif
next bits
population$(amount)=result$
result$=""
next amount
endfunction