Ejemplo n.º 1
0
    def GenerateTotalTimes(self):
        '''
        Generate activity durations based on the quantification of the risk table

        Args:

        Returns: dictionary

        Raises:

        In the example below a risk table R has allready been created

        ex::

           R.GenerateTotalTimes() #generate durations based on the table

        '''
        times = {}
        for q in self.table:
            totaltime = []
            for w in self.table[q]:
                base_duration = self.net.GetNetworkIDDict()[q].GetDuration() #extract all durations
                params = self.table[q][w]
                T = triang(min=params[0], ml=params[1], max=params[2]).generate(1)[0]
                totaltime.append(base_duration + T)
                times[q] = sum(totaltime)

        return times
Ejemplo n.º 2
0
    def Simulate(self, n=10, WriteToDB=True, DbName="Simulation_variates.db", RiskTable = None):

        '''Simulates the network. n = 10 itterations is default (which off course is not enough!)
         Currently, the only avilable distribution is the triangular
         distribution (http://en.wikipedia.org/wiki/Triangular_distribution)
         The method allso gives you the opportunity to write the resulting endates, that is the duration (days) from
         project start to the relevant activity's enddate to a database file called Simulation_variates.db using SQLite
         This file is by default located in /db. You may allso simulate through a Risk table, for that see the documentation of the Driver module


            Args: n (int) number of itterations
                  WriteToDB (boealen) if True writes the endates to database
                  DbName (str) Name of database to create
                  end (boelan) write all activity's enddates to database (not yet working)

            Returns:
                Writes simulation variates to SQLite DB
            Raises:'''

        #updating the project
        #Removing old database file
        DbName = self.path_to_db
        try:
            os.remove(DbName)
        except OSError:
            pass

        
        current_durations = [activity.GetDuration() for activity in self]
        if WriteToDB == True:
            #Constructing appropriate database using SQLite
            db_string = ""
            for q in self.activities:
                db_string = db_string + "ID" + str(q.GetID()) + " TEXT" +  ", "
            db_string_dates =  "CREATE TABLE SimulationResults(" +  db_string[:-2] + ")"
            db_string_critical =  "CREATE TABLE SimulationResults_critical(" +  db_string[:-2] + ")"

            #Creating database
            con = lite.connect(DbName)
            cur = con.cursor()
            try:
                cur.execute(db_string_dates)
                cur.execute(db_string_critical)
            except:
                print "Database allready exists"
            con.commit() # Save (commit) the changes

        self.networkends = []
        """
        This loops assigns a random duration for each activity in the
        network, and calculates the endtime. This is done n times
        """
        #Generates n variates

        if RiskTable == None:

            for i in range(n):
                self.CalculateTotalFloats()
                #each activity is assigned a new duration here:
                for q in self.activities:
                    try:
                        Time = triang(min = q.minduration,
                                      ml = q.mlduration,
                                      max = q.maxduration)
                        T = Time.generate(1)[0]
                        q.AssignDuration(T)
                    except AttributeError:
                        continue
                self.__ForwardPass()
                #Only initiate if you want to write resukts to db
                if WriteToDB == True:
                    #Every endate for each activity is here:
                    enddates = []
                    criticality = []
                    for q in self.activities:
                        try:
                            duration = q.GetEnd(asobject=True)-self.GetNetworkStart(asobject=True)
                            enddates.append(duration.days)
                            critical = q.GetSlack(free=False)
                            criticality.append(critical)

                        except AttributeError:
                            continue
                    enddates = str(tuple(enddates))
                    criticality= str(tuple(criticality))
                    db_string_dates = "INSERT INTO SimulationResults VALUES" + enddates
                    db_string_criticality = "INSERT INTO SimulationResults_critical VALUES" + criticality
                    cur.execute(db_string_dates)
                    cur.execute(db_string_criticality)


                    enddate = (self.GetNetworkEnd() - self.GetNetworkStart()).days
                    self.networkends.append(enddate)


        else:

            for i in range(n):
                self.CalculateTotalFloats()
                #each activity is assigned a new duration here:
                T = RiskTable.GenerateTotalTimes() #this is a dict on the form {'ID':totaltime}
                for ID in T:
                    self.GetNetworkIDDict()[ID].AssignDuration(T[ID]) #activityinstance
                self.__ForwardPass()

                #Only initiate if you want to write resukts to db
                if WriteToDB == True:
                    #Every endate for each activity is here:
                    enddates = []
                    criticality = []
                    for q in self.activities:
                        try:
                            duration = q.GetEnd(asobject=True)-self.GetNetworkStart(asobject=True)
                            enddates.append(duration.days)
                            critical = q.GetSlack(free=False)
                            criticality.append(critical)


                        except AttributeError:
                            continue
                    enddates = str(tuple(enddates))
                    criticality= str(tuple(criticality))
                    db_string_dates = "INSERT INTO SimulationResults VALUES" + enddates
                    db_string_criticality = "INSERT INTO SimulationResults_critical VALUES" + criticality
                    cur.execute(db_string_dates)
                    cur.execute(db_string_criticality)

                    enddate = (self.GetNetworkEnd() - self.GetNetworkStart()).days
                    self.networkends.append(enddate)
        con.commit()
        con.close()
        
        for duration, activity in zip(current_durations, self):
            activity.AssignDuration(duration)