예제 #1
0
        protected virtual void ExecutionUnitExchangeCrossover(
            SchedulingGenome genome1, SchedulingGenome genome2)
        {
            int executionUnits = genome1.MachineDescription.ExecutionUnits;

            int nCross = (int) (GA.Random.NextGaussian()
                                * (executionUnits / 6)
                                + (executionUnits / 2));

            nCross = Math.Min(nCross, executionUnits);
            nCross = Math.Max(nCross, 0);

            for (int i = 0; i < nCross; i++) {

                int exUnit = GA.Random.Next(
                    genome1.MachineDescription.ExecutionUnits);

                for (int j = 0; j < genome1.Schedule.GetLength(0); j++) {

                    InstructionNode i1 = genome1.Schedule[j, i];
                    InstructionNode i2 = genome2.Schedule[j, i];

                    genome1.Schedule[j, i] = i2;
                    genome2.Schedule[j, i] = i1;
                }
            }
        }
예제 #2
0
        protected virtual int GetViolatedRegisterConstraints(
            SchedulingGenome genome)
        {
            IList[] activeValues = GetActiveValues(genome);

            int violatedConstraints = 0;

            for (int step = 0; step < activeValues.Length; step++) {
                for (int i = 0; i < activeValues[step].Count - 1; i++) {

                    ValueNode vi = (ValueNode) activeValues[step][i];
                    Register  ri = genome.GetValueInfo(vi).Register;

                    for (int j = i + 1; j < activeValues[step].Count; j++) {

                        ValueNode vj = (ValueNode) activeValues[step][j];
                        Register rj = genome.GetValueInfo(vj).Register;

                        if (ri == rj)
                            violatedConstraints += 1;
                    }
                }
            }

            return violatedConstraints;
        }
예제 #3
0
        public virtual void RegisterMutation(SchedulingGenome genome)
        {
            int nMut = (int) (Math.Abs(GA.Random.NextGaussian())
                              * (genome.RegisterValues.Count- 1) / 3 + 1);

            nMut = Math.Min(nMut, genome.RegisterValues.Count);

            for (int i = 0; i < nMut; i++) {

                int valIndex =
                    GA.Random.Next(genome.RegisterValues.Count);

                ValueNode val =
                    (ValueNode) genome.RegisterValues[valIndex];

                RegisterSet assignableRegs =
                    (RegisterSet) genome.InstructionGraph.AssignableRegisters[val];

                int regIndex = GA.Random.Next(assignableRegs.Count);

                genome.GetValueInfo(val).Register =
                    (Register) assignableRegs[regIndex];

                ValueNode depVal = (ValueNode) genome.CyclicDependencies[val];

                if (depVal != null) {
                    genome.GetValueInfo(depVal).Register =
                        (Register) assignableRegs[regIndex];
                }
            }
        }
예제 #4
0
 public SchedulingGenomePopulation(
     int size, GAScheduler scheduler, SchedulingGenome initGenome,
     int nInit)
     : base(size)
 {
     this.scheduler = scheduler;
     this.initGenome = initGenome;
     this.nInit = nInit;
 }
예제 #5
0
        protected virtual void PositionExchangeCrossover(
            SchedulingGenome genome1, SchedulingGenome genome2)
        {
            int nCross = (int) (GA.Random.NextGaussian()
                                * (genome1.InstructionNodes.Count / 6)
                                + (genome1.InstructionNodes.Count / 2));

            nCross = Math.Min(nCross, genome1.InstructionNodes.Count);
            nCross = Math.Max(nCross, 0);

            for (int i = 0; i < nCross; i++) {

                int iNodeIndex =
                    GA.Random.Next(genome1.InstructionNodes.Count);

                InstructionNode iNode =
                    (InstructionNode) genome1.InstructionNodes[iNodeIndex];

                int eu = iNode.Instruction.ExecutionUnit;
                int t1 = genome1.GetInstructionInfo(iNode).SchedulingStep;
                int t2 = genome2.GetInstructionInfo(iNode).SchedulingStep;

                genome1.SwapInstructions(t1, t2, eu);
                genome2.SwapInstructions(t1, t2, eu);

                if (iNode.ResultValue is RegisterValueNode) {

                    Register r1 = genome1.GetValueInfo(iNode.ResultValue).Register;
                    Register r2 = genome2.GetValueInfo(iNode.ResultValue).Register;

                    genome1.GetValueInfo(iNode.ResultValue).Register = r2;
                    genome2.GetValueInfo(iNode.ResultValue).Register = r1;

                    ValueNode depVal =
                        (ValueNode) genome1.CyclicDependencies[iNode.ResultValue];

                    if (depVal != null) {

                        Register dr1 = genome1.GetValueInfo(depVal).Register;
                        Register dr2 = genome2.GetValueInfo(depVal).Register;

                        genome1.GetValueInfo(depVal).Register = dr2;
                        genome2.GetValueInfo(depVal).Register = dr1;
                    }
                }
            }
        }
예제 #6
0
        protected virtual IList[] GetActiveValues(SchedulingGenome genome)
        {
            IList[] activeValues = new IList[genome.Schedule.GetLength(0)];

            for (int i = 0; i < genome.Schedule.GetLength(0); i++) {

                activeValues[i] = new ArrayList();

                foreach (ValueNode rValue in genome.RegisterValues) {

                    int production = genome.GetValueInfo(rValue).Production;
                    int lastUsage  = genome.GetValueInfo(rValue).LastUsage;

                    if (production <= i && i <= lastUsage)
                        activeValues[i].Add(rValue);
                }
            }

            return activeValues;
        }
예제 #7
0
        public virtual void ScheduleCrossSwap(SchedulingGenome genome)
        {
            int nMut = (int) (Math.Abs(GA.Random.NextGaussian())
                              * (genome.InstructionNodes.Count - 1) / 3 + 1);

            nMut = Math.Min(nMut, genome.InstructionNodes.Count);

            for (int i = 0; i < nMut; i++) {

                int instrIndex1 =
                    GA.Random.Next(genome.InstructionNodes.Count);

                InstructionNode instr1 = (InstructionNode)
                    genome.InstructionNodes[instrIndex1];

                int instrIndex2 =
                    GA.Random.Next(genome.InstructionNodes.Count);

                InstructionNode instr2 = (InstructionNode)
                    genome.InstructionNodes[instrIndex2];

                IList instrDependencies1 =
                    (IList) genome.Dependencies[instr1];

                IList instrDependencies2 =
                    (IList) genome.Dependencies[instr2];

                int t1 = genome.GetInstructionInfo(instr1).SchedulingStep;
                int t2 = genome.GetInstructionInfo(instr2).SchedulingStep;

                if ((instrDependencies1.Contains(instr2) && t1 <= t2) ||
                    (instrDependencies2.Contains(instr1) && t2 <= t1)) {

                    int eu1 = instr1.Instruction.ExecutionUnit;
                    int eu2 = instr2.Instruction.ExecutionUnit;

                    if (eu1 != eu2) {
                        genome.SwapInstructions(t1, t2, eu1);
                        genome.SwapInstructions(t1, t2, eu2);
                    } else {
                        genome.SwapInstructions(t1, t2, eu1);
                    }
                }
            }
        }
예제 #8
0
        public virtual void ScheduleCompaction(SchedulingGenome genome)
        {
            int nMut = (int) (Math.Abs(GA.Random.NextGaussian())
                              * (genome.InstructionNodes.Count - 1) / 3 + 1);

            nMut = Math.Min(nMut, genome.InstructionNodes.Count);

            for (int i = 0; i < nMut; i++) {

                int iNodeIndex =
                    GA.Random.Next(genome.InstructionNodes.Count);

                InstructionNode iNode =
                    (InstructionNode) genome.InstructionNodes[iNodeIndex];

                int t = genome.GetInstructionInfo(iNode).SchedulingStep;

                if (t > 0) {

                    int exUnit = iNode.Instruction.ExecutionUnit;

                    if (genome.Schedule[t - 1, exUnit] == null)
                        genome.SwapInstructions(t, t - 1, exUnit);
                }
            }
        }
예제 #9
0
파일: GAScheduler.cs 프로젝트: harnold/cobe
        public virtual void Initialize(
            MachineDescription machineDescription,
            InstructionGraph instructionGraph,
            SchedulingGenome initGenome)
        {
            InitializeBase(machineDescription, instructionGraph);
            InitializeDependencies();

            population = new SchedulingGenomePopulation(
                PopulationSize, this, initGenome, 1);

            InitializePopulation();
            InitializeGA();

            bestObjectiveMonitor = new double[MonitoredGenerations];

            if (Initialized != null)
                Initialized(this);
        }
예제 #10
0
 public virtual SchedulingGenome Clone(
     IDictionary instructionMap, IDictionary valueMap)
 {
     SchedulingGenome clone = new SchedulingGenome();
     clone.Copy(this, instructionMap, valueMap);
     return clone;
 }
예제 #11
0
        protected virtual void RegisterCrossover(
            SchedulingGenome genome1, SchedulingGenome genome2)
        {
            int nCross = (int) (GA.Random.NextGaussian()
                                * (genome1.RegisterValues.Count / 6)
                                + (genome1.RegisterValues.Count / 2));

            nCross = Math.Min(nCross, genome1.RegisterValues.Count);
            nCross = Math.Max(nCross, 0);

            for (int i = 0; i < nCross; i++) {

                ValueNode val = (ValueNode) genome1.RegisterValues[
                    GA.Random.Next(genome1.RegisterValues.Count)];

                Register r1 = genome1.GetValueInfo(val).Register;
                Register r2 = genome2.GetValueInfo(val).Register;

                genome1.GetValueInfo(val).Register = r2;
                genome2.GetValueInfo(val).Register = r1;

                ValueNode depVal = (ValueNode) genome1.CyclicDependencies[val];

                if (depVal != null) {
                    genome1.GetValueInfo(depVal).Register = r2;
                    genome2.GetValueInfo(depVal).Register = r1;
                }
            }
        }
예제 #12
0
        protected virtual void RelativeOrderCrossoverForExUnit(
            SchedulingGenome genome1, SchedulingGenome genome2,
            int exUnit, IList instructionsOnExUnit)
        {
            int nCross = (int) (GA.Random.NextGaussian()
                                * (instructionsOnExUnit.Count / 6)
                                + (instructionsOnExUnit.Count / 2));

            nCross = Math.Min(nCross, instructionsOnExUnit.Count);
            nCross = Math.Max(nCross, 0);

            InstructionNode[] selectedInstr1 = new InstructionNode[nCross];
            InstructionNode[] selectedInstr2 = new InstructionNode[nCross];

            int[] instrSteps1 = new int[nCross];
            int[] instrSteps2 = new int[nCross];

            for (int i = 0; i < nCross; i++) {

                int selectedInstrIndex =
                    GA.Random.Next(instructionsOnExUnit.Count);

                InstructionNode selectedInstr =
                    (InstructionNode) instructionsOnExUnit[selectedInstrIndex];

                selectedInstr1[i] = selectedInstr;
                selectedInstr2[i] = selectedInstr;

                instrSteps1[i] = genome1.GetInstructionInfo(
                    selectedInstr).SchedulingStep;

                instrSteps2[i] = genome2.GetInstructionInfo(
                    selectedInstr).SchedulingStep;
            }

            Array.Sort(instrSteps1, selectedInstr2);
            Array.Sort(instrSteps2, selectedInstr1);

            for (int i = 0; i < nCross; i++) {
                genome1.ScheduleInstructionForStep(
                    selectedInstr1[i], instrSteps1[i]);
                genome2.ScheduleInstructionForStep(
                    selectedInstr2[i], instrSteps2[i]);
            }
        }
예제 #13
0
        protected virtual void RelativeOrderCrossover(
            SchedulingGenome genome1, SchedulingGenome genome2)
        {
            for (int i = 0; i < genome1.MachineDescription.ExecutionUnits; i++) {

                IList instructionsOnExUnit = genome1.InstructionsOnExUnit[i];

                if (instructionsOnExUnit.Count > 0) {
                    RelativeOrderCrossoverForExUnit(
                        genome1, genome2, i, instructionsOnExUnit);
                }
            }
        }
예제 #14
0
        public virtual void Copy(
            SchedulingGenome other,
            IDictionary instructionMap,
            IDictionary valueMap)
        {
            population = other.population;
            scheduler = other.scheduler;

            schedule = new InstructionNode[
                other.schedule.GetLength(0),
                other.schedule.GetLength(1)];

            for (int i = 0; i < schedule.GetLength(0); i++) {
                for (int j = 0; j < schedule.GetLength(1); j++) {

                    InstructionNode iNode = other.schedule[i, j];

                    if (iNode != null) {
                        schedule[i, j] =
                            (InstructionNode) instructionMap[other.schedule[i, j]];
                    } else {
                        schedule[i, j] = null;
                    }
                }
            }

            valueInfos = new Hashtable();

            foreach (ValueNode vNode in other.ValueInfos.Keys) {

                ValueInfo info = new ValueInfo();
                ValueInfo otherInfo = other.GetValueInfo(vNode);

                info.Register = otherInfo.Register;
                info.Production = otherInfo.Production;
                info.LastUsage = otherInfo.LastUsage;
                valueInfos[valueMap[vNode]] = info;
            }

            instructionInfos = new Hashtable();

            foreach (InstructionNode iNode in other.InstructionInfos.Keys) {

                InstructionInfo info = new InstructionInfo();
                InstructionInfo otherInfo = other.GetInstructionInfo(iNode);

                info.SchedulingStep = otherInfo.SchedulingStep;
                instructionInfos[instructionMap[iNode]] = info;
            }

            isValid = other.isValid;
            scheduleLength = other.scheduleLength;
            violatedSchedulingConstraints = other.violatedSchedulingConstraints;
            violatedRegisterConstraints = other.violatedRegisterConstraints;
            generationOfBirth = other.generationOfBirth;
        }
예제 #15
0
        public virtual void ScheduleMutation(SchedulingGenome genome)
        {
            int nMut = (int) (Math.Abs(GA.Random.NextGaussian())
                              * (genome.InstructionNodes.Count - 1) / 3 + 1);

            nMut = Math.Min(nMut, genome.InstructionNodes.Count);

            for (int i = 0; i < nMut; i++) {

                int iNodeIndex =
                    GA.Random.Next(genome.InstructionNodes.Count);

                InstructionNode iNode =
                    (InstructionNode) genome.InstructionNodes[iNodeIndex];

                int exUnit = iNode.Instruction.ExecutionUnit;
                int t1 = genome.GetInstructionInfo(iNode).SchedulingStep;
                int t2 = GA.Random.Next(genome.Schedule.GetLength(0));

                genome.SwapInstructions(t1, t2, exUnit);
            }
        }
예제 #16
0
        protected virtual int GetViolatedSchedulingConstraints(
            SchedulingGenome genome)
        {
            int violatedConstraints = 0;

            foreach (InstructionNode instr in genome.InstructionNodes) {

                int instrStep = genome.GetInstructionInfo(instr).SchedulingStep;
                IList instrDependencies = (IList) genome.Dependencies[instr];

                foreach (InstructionNode depInstr in instrDependencies) {

                    int depInstrStep =
                        genome.GetInstructionInfo(depInstr).SchedulingStep;

                    if (instrStep <= depInstrStep)
                        violatedConstraints += 1;
                }

                /*
                foreach (ValueNode depVal in instr.OperandValues) {

                    if (!depVal.IsInputValue()) {

                        InstructionNode depInstr =
                            depVal.ProducingInstruction;

                        int instrStep =
                            genome.GetInstructionInfo(instr).SchedulingStep;
                        int depInstrStep =
                            genome.GetInstructionInfo(depInstr).SchedulingStep;

                        if (depInstrStep >= instrStep)
                            violatedConstraints += depInstrStep - instrStep + 1;
                    }
                }
                */
            }

            return violatedConstraints;
        }
예제 #17
0
 public virtual object Clone()
 {
     SchedulingGenome clone = new SchedulingGenome();
     clone.Copy(this);
     return clone;
 }