473,407 Members | 2,315 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

error of indent

4
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.
Expand|Select|Wrap|Line Numbers
  1.     # -*- coding: cp1252 -*-
  2.     import random, sys, os, time
  3.     class individu:
  4.     def __init__ (self,nombre_genes,random=-1):
  5.     self.genes = []
  6.     self.nombre_genes = nombre_genes
  7.     if random == -1 : self.random ()
  8.     """
  9.     random : initialise aleatoirement les genes
  10.     """
  11.     def random (self):
  12.     self.genes = []
  13.     f = range(0,self.nombre_genes)
  14.     for v in range(self.nombre_genes):
  15.     val = random.choice (f)
  16.     f.remove (val)
  17.     self.genes.append (val)
  18.     """
  19.     evaluation : compare l'individu avec un tableau d'autres individus
  20.     retourne le nombre d'individus auquel l'individu self est superieur et de meilleure
  21.     qualité
  22.     """
  23.     def evalutation (self,autres_individus,distances):
  24.     valeur_individu = 0
  25.     for v in autres_individus:
  26.     if self.compare(v,distances) > 0 : valeur_individu = valeur_individu + 1
  27.     self.valeur_individu = valeur_individu
  28.     return valeur_individu
  29.     """
  30.     compare : compare l'individu self avec un autre individu
  31.     retourne un nombre positif s'il est superieur
  32.     un nombre negatif s'il est inferieur
  33.     le nombre 0 s'il est égal
  34.     """
  35.     def calcul_distance (self,distance):
  36.     a = self.genes[0]
  37.     dist = 0
  38.     for b in self.genes[1:]:
  39.     min=0
  40.     max=0
  41.     if a > b :
  42.     min = b
  43.     max = a
  44.     else :
  45.     min = a
  46.     max = b
  47.     dist = dist + (distance[min])[max-min-1]
  48.     a = b
  49.     self.distance = dist
  50.     return dist
  51.     def compare (self,individu,distance):
  52.     individu.calcul_distance (distance)
  53.     self.calcul_distance (distance)
  54.     return self.distance - individu.distance
  55.     class population:
  56.     def __init__ (self,nombre_genes,nombre_initial_population=50):
  57.     self.nombre_genes = nombre_genes
  58.     self.distances = [[]] * nombre_genes
  59.     self.individus = []
  60.     self.nombre_initial_population = nombre_initial_population
  61.     for v in range(self.nombre_initial_population):
  62.     self.individus.append (individu(nombre_genes))
  63.     """
  64.     selection : methode de selection des meilleurs N individus
  65.     """
  66.     def selection (self , N=-1):
  67.     if N == -1 : N = len(self.individus)
  68.     for v in self.individus:
  69.     v.evalutation (self.individus,self.distances)
  70.     self.individus.sort (lambda a,b : a.valeur_individu - b.valeur_individu)
  71.     if N <= len(self.individus) : self.individus = self.individus[0:N]
  72.     """
  73.     croisement : selectionne deux individus et croise leur gênes pour en creer de nouveaux
  74.     """
  75.     def croisement_deux (self , i1 , i2):
  76.     i = individu (self.nombre_genes)
  77.     l = len(i1.genes)
  78.     a = l / 2
  79.     p2 = i2.genes[a:l]
  80.     p1 = i1.genes[0:a]
  81.     choices = range(0,l)
  82.     for v in p1:
  83.     try:choices.remove (v)
  84.     except : pass
  85.     for v in p2:
  86.     try : choices.remove (v)
  87.     except : pass
  88.     idc = 0
  89.     for id in range(len(p2)):
  90.     if p2[id] in p1:
  91.     p2[id] = choices[idc]
  92.     idc = idc + 1
  93.     i.genes = p1 + p2
  94.     return i
  95.     """ croise tous les individus entre eux"""
  96.     def croisement_all (self):
  97.     new = []
  98.     for v1 in range(len(self.individus)):
  99.     for v2 in range(v1,len(self.individus)):
  100.     new.append ( self.croisement_deux (self.individus[v1] , self.individus[v2]))
  101.     self.individus = self.individus + new
  102.     """ croise nombre_croisement individus aleatoires entre eux """
  103.     def croisement_nombre (self , nombre_croisement):
  104.     new = []
  105.     for r in range(nombre_croisement):
  106.     v1 = random.randint (0 , len(self.individus)-1)
  107.     v2 = random.randint (0 , len(self.individus)-1)
  108.     new.append (self.croisement_deux (self.individus[v1] , self.individus[v2]) )
  109.     self.individus = self.individus + new
  110.     if __name__ == "__main__" :
  111.     """ population de 50 individus"""
  112.     p = population (10,50)
  113.     """ initialise les distances """
  114.     p.distances[0] = [2,3,4,5,6,7,8,9,10 ] # distance de la premiere ville avec la 2eme , 3eme , ... , 10eme
  115.     p.distances[1] = [11,12,13,14,15,16,17,18 ] # distance de la deuxieme ville avec la 3eme , 4eme , ... ,10eme
  116.     p.distances[2] = [19,20,21,22,23,24,25] #etc...
  117.     p.distances[3] = [26,27,28,29,30,31]
  118.     p.distances[4] = [32,33,34,35,36]
  119.     p.distances[5] = [37,38,39 ,40]
  120.     p.distances[6] = [41,42,43]
  121.     p.distances[7] = [44,45]
  122.     p.distances[8] = [46] # distance de la 9eme ville avec la 10eme ville
  123.     """
  124.     10 generations
  125.     a chaque fois fait 100 croisements
  126.     et selectionne les 10 premiers
  127.     """
  128.     for i in range(10):
  129.     p.croisement_nombre (100)
  130.     p.selection (10)
  131.     for v in p.individus:
  132.     print v.genes,v.calcul_distance (p.distances)
  133.  
  134.  
  135.  
Jan 17 '13 #1
3 1946
Rabbit
12,516 Expert Mod 8TB
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.
Jan 17 '13 #2
dooja
4
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:
Expand|Select|Wrap|Line Numbers
  1. # *-* coding: utf8 *-*
  2.  
  3. import random, sys, os, time
  4. class individu:
  5.  def __init__ (self,nombre_genes,random=-1):
  6.      self.genes = []
  7.      self.nombre_genes = nombre_genes
  8.      if random == -1 : self.random ()
  9. """
  10. random : initialise aleatoirement les genes
  11. """
  12. def random (self):
  13.     self.genes = []
  14.     f = range(0,self.nombre_genes)
  15.     for v in range(self.nombre_genes):
  16.         val = random.choice (f)
  17.         f.remove (val)
  18.         self.genes.append (val)
  19. """
  20. evaluation : compare l'individu avec un tableau d'autres individus
  21. retourne le nombre d'individus auquel l'individu self est superieur et de meilleure
  22. qualité
  23. """
  24. def evalutation (self,autres_individus,distances):
  25.     valeur_individu = 0
  26.     for v in autres_individus:
  27.         if self.compare(v,distances) > 0 : valeur_individu = valeur_individu + 1
  28.         self.valeur_individu = valeur_individu
  29.     return valeur_individu
  30. """
  31. compare : compare l'individu self avec un autre individu
  32. retourne un nombre positif s'il est superieur
  33. un nombre negatif s'il est inferieur
  34. le nombre 0 s'il est égal
  35. """
  36. def calcul_distance (self,distance):
  37.     a = self.genes[0]
  38.     dist = 0
  39.     for b in self.genes[1:]:
  40.         min=0
  41.         max=0
  42.         if a > b :
  43.             min = b
  44.             max = a
  45.         else :
  46.             min = a
  47.             max = b
  48.         dist = dist + (distance[min])[max-min-1]
  49.         a = b
  50.         self.distance = dist
  51.     return dist
  52. def compare (self,individu,distance):
  53.     individu.calcul_distance (distance)
  54.     self.calcul_distance (distance)
  55.     return self.distance - individu.distance
  56. class population:
  57.     def __init__ (self,nombre_genes,nombre_initial_population=50):
  58.         self.nombre_genes = nombre_genes
  59.         self.distances = [[]] * nombre_genes
  60.         self.individus = []
  61.         self.nombre_initial_population = nombre_initial_population
  62.     for v in range(self.nombre_initial_population):
  63.         self.individus.append (individu(nombre_genes))
  64. """
  65. selection : methode de selection des meilleurs N individus
  66. """
  67. def selection (self , N=-1):
  68.         if N == -1 : N = len(self.individus)
  69.         for v in self.individus:
  70.             v.evalutation (self.individus,self.distances)
  71.             self.individus.sort (lambda a,b : a.valeur_individu - b.valeur_individu)
  72.         if N <= len(self.individus) : self.individus = self.individus[0:N]
  73. """
  74. croisement : selectionne deux individus et croise leur gênes pour en creer de nouveaux
  75. """
  76. def croisement_deux (self , i1 , i2):
  77.         i = individu (self.nombre_genes)
  78.         l = len(i1.genes)
  79.         a = l / 2
  80.         p2 = i2.genes[a:l]
  81.         p1 = i1.genes[0:a]
  82.         choices = range(0,l)
  83.         for v in p1:
  84.             try:choices.remove (v)
  85.             except : pass
  86.         for v in p2:
  87.             try : choices.remove (v)
  88.             except : pass
  89.         idc = 0
  90.         for id in range(len(p2)):
  91.             if p2[id] in p1:
  92.                 p2[id] = choices[idc]
  93.                 idc = idc + 1
  94.                 i.genes = p1 + p2
  95.         return i
  96. """ croise tous les individus entre eux"""
  97. def croisement_all (self):
  98.     new = []
  99.     for v1 in range(len(self.individus)):
  100.         for v2 in range(v1,len(self.individus)):
  101.             new.append ( self.croisement_deux (self.individus[v1] , self.individus[v2]))
  102.             self.individus = self.individus + new
  103. """ croise nombre_croisement individus aleatoires entre eux """
  104. def croisement_nombre (self , nombre_croisement):
  105.     new = []
  106.     for r in range(nombre_croisement):
  107.         v1 = random.randint (0 , len(self.individus)-1)
  108.         v2 = random.randint (0 , len(self.individus)-1)
  109.         new.append (self.croisement_deux (self.individus[v1] , self.individus[v2]) )
  110.         self.individus = self.individus + new
  111. if __name__ == "__main__" :
  112.    p = population (10,50) # population de 50 individus puis on initialise les distances
  113.    p.distances[0] = [2,3,4,5,6,7,8,9,10 ] # distance de la premiere ville avec la 2eme , 3eme , ... , 10eme
  114.    p.distances[1] = [11,12,13,14,15,16,17,18 ] # distance de la deuxieme ville avec la 3eme , 4eme , ... ,10eme
  115.    p.distances[2] = [19,20,21,22,23,24,25] #etc...
  116.    p.distances[3] = [26,27,28,29,30,31]
  117.    p.distances[4] = [32,33,34,35,36]
  118.    p.distances[5] = [37,38,39 ,40]
  119.    p.distances[6] = [41,42,43]
  120.    p.distances[7] = [44,45]
  121.    p.distances[8] = [46] # distance de la 9eme ville avec la 10eme ville
  122. """
  123. 10 generations
  124. a chaque fois fait 100 croisements
  125. et selectionne les 10 premiers
  126. """
  127. for i in range(10):
  128.     p.croisement_nombre (100)
  129.     p.selection (10)
  130.     for v in p.individus:
  131.         print v.genes,v.calcul_distance (p.distances)
  132.  
Jan 18 '13 #3
Rabbit
12,516 Expert Mod 8TB
We only allow one question per thread in this forum. Since this is a new question, please create a new thread for it.
Jan 18 '13 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
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...
2
Frinavale
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...
4
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 ...
6
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...
7
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...
3
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...
6
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...
9
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...
1
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...
0
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
1
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...
0
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...
0
marktang
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,...
0
jinu1996
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...
0
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...
0
tracyyun
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...
0
agi2029
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,...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.