hay for all;
a try to run the following code in python 2.5 in order to resolve the STP, but i find an error of indent (error: unexpected indent), please; can you help me. -
# -*- coding: cp1252 -*-
-
import random, sys, os, time
-
class individu:
-
def __init__ (self,nombre_genes,random=-1):
-
self.genes = []
-
self.nombre_genes = nombre_genes
-
if random == -1 : self.random ()
-
"""
-
random : initialise aleatoirement les genes
-
"""
-
def random (self):
-
self.genes = []
-
f = range(0,self.nombre_genes)
-
for v in range(self.nombre_genes):
-
val = random.choice (f)
-
f.remove (val)
-
self.genes.append (val)
-
"""
-
evaluation : compare l'individu avec un tableau d'autres individus
-
retourne le nombre d'individus auquel l'individu self est superieur et de meilleure
-
qualité
-
"""
-
def evalutation (self,autres_individus,distances):
-
valeur_individu = 0
-
for v in autres_individus:
-
if self.compare(v,distances) > 0 : valeur_individu = valeur_individu + 1
-
self.valeur_individu = valeur_individu
-
return valeur_individu
-
"""
-
compare : compare l'individu self avec un autre individu
-
retourne un nombre positif s'il est superieur
-
un nombre negatif s'il est inferieur
-
le nombre 0 s'il est égal
-
"""
-
def calcul_distance (self,distance):
-
a = self.genes[0]
-
dist = 0
-
for b in self.genes[1:]:
-
min=0
-
max=0
-
if a > b :
-
min = b
-
max = a
-
else :
-
min = a
-
max = b
-
dist = dist + (distance[min])[max-min-1]
-
a = b
-
self.distance = dist
-
return dist
-
def compare (self,individu,distance):
-
individu.calcul_distance (distance)
-
self.calcul_distance (distance)
-
return self.distance - individu.distance
-
class population:
-
def __init__ (self,nombre_genes,nombre_initial_population=50):
-
self.nombre_genes = nombre_genes
-
self.distances = [[]] * nombre_genes
-
self.individus = []
-
self.nombre_initial_population = nombre_initial_population
-
for v in range(self.nombre_initial_population):
-
self.individus.append (individu(nombre_genes))
-
"""
-
selection : methode de selection des meilleurs N individus
-
"""
-
def selection (self , N=-1):
-
if N == -1 : N = len(self.individus)
-
for v in self.individus:
-
v.evalutation (self.individus,self.distances)
-
self.individus.sort (lambda a,b : a.valeur_individu - b.valeur_individu)
-
if N <= len(self.individus) : self.individus = self.individus[0:N]
-
"""
-
croisement : selectionne deux individus et croise leur gênes pour en creer de nouveaux
-
"""
-
def croisement_deux (self , i1 , i2):
-
i = individu (self.nombre_genes)
-
l = len(i1.genes)
-
a = l / 2
-
p2 = i2.genes[a:l]
-
p1 = i1.genes[0:a]
-
choices = range(0,l)
-
for v in p1:
-
try:choices.remove (v)
-
except : pass
-
for v in p2:
-
try : choices.remove (v)
-
except : pass
-
idc = 0
-
for id in range(len(p2)):
-
if p2[id] in p1:
-
p2[id] = choices[idc]
-
idc = idc + 1
-
i.genes = p1 + p2
-
return i
-
""" croise tous les individus entre eux"""
-
def croisement_all (self):
-
new = []
-
for v1 in range(len(self.individus)):
-
for v2 in range(v1,len(self.individus)):
-
new.append ( self.croisement_deux (self.individus[v1] , self.individus[v2]))
-
self.individus = self.individus + new
-
""" croise nombre_croisement individus aleatoires entre eux """
-
def croisement_nombre (self , nombre_croisement):
-
new = []
-
for r in range(nombre_croisement):
-
v1 = random.randint (0 , len(self.individus)-1)
-
v2 = random.randint (0 , len(self.individus)-1)
-
new.append (self.croisement_deux (self.individus[v1] , self.individus[v2]) )
-
self.individus = self.individus + new
-
if __name__ == "__main__" :
-
""" population de 50 individus"""
-
p = population (10,50)
-
""" initialise les distances """
-
p.distances[0] = [2,3,4,5,6,7,8,9,10 ] # distance de la premiere ville avec la 2eme , 3eme , ... , 10eme
-
p.distances[1] = [11,12,13,14,15,16,17,18 ] # distance de la deuxieme ville avec la 3eme , 4eme , ... ,10eme
-
p.distances[2] = [19,20,21,22,23,24,25] #etc...
-
p.distances[3] = [26,27,28,29,30,31]
-
p.distances[4] = [32,33,34,35,36]
-
p.distances[5] = [37,38,39 ,40]
-
p.distances[6] = [41,42,43]
-
p.distances[7] = [44,45]
-
p.distances[8] = [46] # distance de la 9eme ville avec la 10eme ville
-
"""
-
10 generations
-
a chaque fois fait 100 croisements
-
et selectionne les 10 premiers
-
"""
-
for i in range(10):
-
p.croisement_nombre (100)
-
p.selection (10)
-
for v in p.individus:
-
print v.genes,v.calcul_distance (p.distances)
-
-
-
3 1946
For one thing, none of your code is indented beyond the initial whitespace at the beginning of every line. You should learn the rules of indentation required in python and then go through and indent all your code correctly.
thank you for your help,
I indent all the code correctly, But;I find an other error is:
Traceback (most recent call last):
File "C:\Python25\gen.py", line 57, in <module>
class population:
File "C:\Python25\gen.py", line 63, in population
for v in range(self.nombre_initial_population):
NameError: name 'self' is not defined
I need your help again: -
# *-* coding: utf8 *-*
-
-
import random, sys, os, time
-
class individu:
-
def __init__ (self,nombre_genes,random=-1):
-
self.genes = []
-
self.nombre_genes = nombre_genes
-
if random == -1 : self.random ()
-
"""
-
random : initialise aleatoirement les genes
-
"""
-
def random (self):
-
self.genes = []
-
f = range(0,self.nombre_genes)
-
for v in range(self.nombre_genes):
-
val = random.choice (f)
-
f.remove (val)
-
self.genes.append (val)
-
"""
-
evaluation : compare l'individu avec un tableau d'autres individus
-
retourne le nombre d'individus auquel l'individu self est superieur et de meilleure
-
qualité
-
"""
-
def evalutation (self,autres_individus,distances):
-
valeur_individu = 0
-
for v in autres_individus:
-
if self.compare(v,distances) > 0 : valeur_individu = valeur_individu + 1
-
self.valeur_individu = valeur_individu
-
return valeur_individu
-
"""
-
compare : compare l'individu self avec un autre individu
-
retourne un nombre positif s'il est superieur
-
un nombre negatif s'il est inferieur
-
le nombre 0 s'il est égal
-
"""
-
def calcul_distance (self,distance):
-
a = self.genes[0]
-
dist = 0
-
for b in self.genes[1:]:
-
min=0
-
max=0
-
if a > b :
-
min = b
-
max = a
-
else :
-
min = a
-
max = b
-
dist = dist + (distance[min])[max-min-1]
-
a = b
-
self.distance = dist
-
return dist
-
def compare (self,individu,distance):
-
individu.calcul_distance (distance)
-
self.calcul_distance (distance)
-
return self.distance - individu.distance
-
class population:
-
def __init__ (self,nombre_genes,nombre_initial_population=50):
-
self.nombre_genes = nombre_genes
-
self.distances = [[]] * nombre_genes
-
self.individus = []
-
self.nombre_initial_population = nombre_initial_population
-
for v in range(self.nombre_initial_population):
-
self.individus.append (individu(nombre_genes))
-
"""
-
selection : methode de selection des meilleurs N individus
-
"""
-
def selection (self , N=-1):
-
if N == -1 : N = len(self.individus)
-
for v in self.individus:
-
v.evalutation (self.individus,self.distances)
-
self.individus.sort (lambda a,b : a.valeur_individu - b.valeur_individu)
-
if N <= len(self.individus) : self.individus = self.individus[0:N]
-
"""
-
croisement : selectionne deux individus et croise leur gênes pour en creer de nouveaux
-
"""
-
def croisement_deux (self , i1 , i2):
-
i = individu (self.nombre_genes)
-
l = len(i1.genes)
-
a = l / 2
-
p2 = i2.genes[a:l]
-
p1 = i1.genes[0:a]
-
choices = range(0,l)
-
for v in p1:
-
try:choices.remove (v)
-
except : pass
-
for v in p2:
-
try : choices.remove (v)
-
except : pass
-
idc = 0
-
for id in range(len(p2)):
-
if p2[id] in p1:
-
p2[id] = choices[idc]
-
idc = idc + 1
-
i.genes = p1 + p2
-
return i
-
""" croise tous les individus entre eux"""
-
def croisement_all (self):
-
new = []
-
for v1 in range(len(self.individus)):
-
for v2 in range(v1,len(self.individus)):
-
new.append ( self.croisement_deux (self.individus[v1] , self.individus[v2]))
-
self.individus = self.individus + new
-
""" croise nombre_croisement individus aleatoires entre eux """
-
def croisement_nombre (self , nombre_croisement):
-
new = []
-
for r in range(nombre_croisement):
-
v1 = random.randint (0 , len(self.individus)-1)
-
v2 = random.randint (0 , len(self.individus)-1)
-
new.append (self.croisement_deux (self.individus[v1] , self.individus[v2]) )
-
self.individus = self.individus + new
-
if __name__ == "__main__" :
-
p = population (10,50) # population de 50 individus puis on initialise les distances
-
p.distances[0] = [2,3,4,5,6,7,8,9,10 ] # distance de la premiere ville avec la 2eme , 3eme , ... , 10eme
-
p.distances[1] = [11,12,13,14,15,16,17,18 ] # distance de la deuxieme ville avec la 3eme , 4eme , ... ,10eme
-
p.distances[2] = [19,20,21,22,23,24,25] #etc...
-
p.distances[3] = [26,27,28,29,30,31]
-
p.distances[4] = [32,33,34,35,36]
-
p.distances[5] = [37,38,39 ,40]
-
p.distances[6] = [41,42,43]
-
p.distances[7] = [44,45]
-
p.distances[8] = [46] # distance de la 9eme ville avec la 10eme ville
-
"""
-
10 generations
-
a chaque fois fait 100 croisements
-
et selectionne les 10 premiers
-
"""
-
for i in range(10):
-
p.croisement_nombre (100)
-
p.selection (10)
-
for v in p.individus:
-
print v.genes,v.calcul_distance (p.distances)
-
We only allow one question per thread in this forum. Since this is a new question, please create a new thread for it.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Lara1 |
last post by:
I'm trying to get certain cells to show a hovering alert message when I click on them. (I don't want an error-message style box to pop up, because I'll eventually want it to show for lots of cells...
|
by: Frinavale |
last post by:
I thought it would be nice to share the solution to my IIS headache with the rest of the world.
I have developed a web application that has been distributed. Upon installing my application, some...
|
by: nazimsal |
last post by:
Imports System.Data.SqlClient
Namespace Accounts
Public Class NewAccount
Dim cmd As New SqlCommand
Dim c As New DatabaseConnection.DataBaseConnaction.ConnectionClass
...
|
by: lily86 |
last post by:
i'm using vs.net 2003.... i want to let the user upload images and i want to save the image to my web server file to convenient i display the images on other pages.... but i'm having a problem when i...
|
by: Yesurbius |
last post by:
I am receiving the following error when attempting to run my query. In my mind - this error should not be happening - its a straight-forward query with a subquery. I am using Access 2003 with all...
|
by: brkseven |
last post by:
Looking for help with this Contact Form.
The error is on line 1, but that' doesn't mean a lot, I think. In fact, a php syntax check passed it, but I was hoping for an easy syntax error, it looks...
|
by: Soorali |
last post by:
Hi
I am a newbie to VC++ and this is my first independent project so please pardon my ignorance!!
My project compiles and runs perfectly fine in Debug mode.
However, when I try to compile it...
|
by: SusanK4305 |
last post by:
When I click the button I get this Error
"Run-time error '53' file not found"
What am I doing wrong?
Private Sub Command8_Click()
'Note: This is to open S Drive file for notes Managment...
|
by: Eran Hefer |
last post by:
Problem:
User got an error massage when trying to load an InfoPath form:
"This session has exceeded the amount of allowable resources"
More Details:
Check out the Event Viewer on the...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |