我使用遗传算法 (nest.js) 编写了类调度。我的程序有 Geneticalgorithm.ts、data.ts、population.ts、scheduleservice.ts 和 Schedulemodule.ts。我在 Geneticalgorithmclass 中使用的方法中调用了 Population。作为构造函数。我需要将 Population.getSchedules().length 作为数字传递给构造函数。但我收到了这个错误。Geneticalgorithm.ts Population.ts 和 Schedulemodule.ts 如下。
错误定义;
[Nest] 63732 - 11/11/2024, 6:51:23 PM LOG [NestFactory] Starting Nest application...
[Nest] 63732 - 11/11/2024, 6:51:23 PM LOG [InstanceLoader] AppModule dependencies initialized +54ms
[Nest] 63732 - 11/11/2024, 6:51:23 PM LOG [Instance Loader] Type Orm Module dependencies initialized +0ms
[Nest] 63732 - 11/11/2024, 6:51:23 PM LOG [Instance Loader] Number dependencies initialized +1ms
[Nest] 63732 - 11/11/2024, 6:51:23 PM ERROR [Exception Handler] Nest can't resolve dependencies of the Population (?, Data). Please make sure that the argument Number at index [0] is available in the ScheduleModule context.
Potential solutions:
- Is ScheduleModule a valid NestJS module?
- If Number is a provider, is it part of the current ScheduleModule?
- If Number is exported from a separate @Module, is that module imported within ScheduleModule?
@Module({
imports: [ /* the Module containing Number */ ]
})
import { Injectable } from '@nestjs/common';
import { Data } from './data';
import { Population } from './population';
import { Schedule } from './schedule';
import { Driver } from './driver';
@Injectable()
export class GeneticAlgorithm {
private data: Data;
constructor(data: Data) {
this.data = data;
}
public evolve(population: Population): Population {
return this.mutatePopulation(this.crossoverPopulation(population));
}
private crossoverPopulation(population: Population): Population {
const crossoverPopulation = new Population(population.getSchedules().length, this.data);
for (let i = 0; i < Driver.NUMB_OF_ELITE_SCHEDULES; i++) {
crossoverPopulation.getSchedules()[i] = population.getSchedules()[i];
}
for (let i = Driver.NUMB_OF_ELITE_SCHEDULES; i < population.getSchedules().length; i++) {
if (Driver.CROSSOVER_RATE > Math.random()) {
const schedule1 = this.selectTournamentPopulation(population).sortByFitness().getSchedules()[0];
const schedule2 = this.selectTournamentPopulation(population).sortByFitness().getSchedules()[0];
crossoverPopulation.getSchedules()[i] = this.crossoverSchedule(schedule1, schedule2);
} else {
crossoverPopulation.getSchedules()[i] = population.getSchedules()[i];
}
}
return crossoverPopulation;
}
private crossoverSchedule(schedule1: Schedule, schedule2: Schedule): Schedule {
const crossoverSchedule = new Schedule(this.data).initialize();
for (let i = 0; i < crossoverSchedule.getClasses().length; i++) {
if (Math.random() > 0.5) {
crossoverSchedule.getClasses()[i] = schedule1.getClasses()[i];
} else {
crossoverSchedule.getClasses()[i] = schedule2.getClasses()[i];
}
}
return crossoverSchedule;
}
private mutatePopulation(population: Population): Population {
const mutatePopulation = new Population(population.getSchedules().length, this.data);
const schedules = mutatePopulation.getSchedules();
for (let i = 0; i < Driver.NUMB_OF_ELITE_SCHEDULES; i++) {
schedules[i] = population.getSchedules()[i];
}
for (let i = Driver.NUMB_OF_ELITE_SCHEDULES; i < population.getSchedules().length; i++) {
schedules[i] = this.mutateSchedule(population.getSchedules()[i]);
}
return mutatePopulation;
}
private mutateSchedule(schedule: Schedule): Schedule {
const mutateSchedule = new Schedule(this.data).initialize();
for (let i = 0; i < schedule.getClasses().length; i++) {
if (Driver.MUTATION_RATE > Math.random()) {
schedule.getClasses()[i] = mutateSchedule.getClasses()[i];
}
}
return schedule;
}
private selectTournamentPopulation(population: Population): Population {
const tournamentPopulation = new Population(Driver.TOURNAMENT_SELECTION_SIZE, this.data);
for (let i = 0; i < Driver.TOURNAMENT_SELECTION_SIZE; i++) {
tournamentPopulation.getSchedules()[i] = population.getSchedules()[Math.floor(Math.random() * population.getSchedules().length)];
}
return tournamentPopulation;
}
}
import { Injectable } from '@nestjs/common';
import { Data } from './data';
import { Schedule } from './schedule';
@Injectable()
export class Population {
private schedules: Schedule[];
constructor(size: number, data: Data) {
this.schedules = new Array<Schedule>(size);
for (let i = 0; i < size; i++) {
this.schedules[i] = new Schedule(data).initialize();
}
}
public getSchedules(): Schedule[] {
return this.schedules;
}
public sortByFitness(): Population {
this.schedules.sort((schedule1, schedule2) => {
let returnValue = 0;
if (schedule1.getFitness() > schedule2.getFitness()) returnValue = -1;
else if (schedule1.getFitness() < schedule2.getFitness()) returnValue = 1;
return returnValue;
});
return this;
}
}
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Schedule } from './schedule.entity';
import { ScheduleService } from './schedule.service';
import { ScheduleController } from './schedule.controller';
import { ScheduleRepository } from './schedule.repository';
import { Data } from './data';
import { GeneticAlgorithm } from './genetic-algorithm';
import { Population } from './population';
import { Class } from './class.entity';
import { CourseModule } from '../course/course.module';
import { DepartmentModule } from '../department/department.module';
import { InstructorModule } from '../instructor/instructor.module';
import { MeetingTimeModule } from '../meeting-time/meeting-time.module';
import { RoomModule } from '../room/room.module';
@Module({
imports: [
TypeOrmModule.forFeature([Schedule, Class]),
CourseModule,
DepartmentModule,
InstructorModule,
MeetingTimeModule,
RoomModule,
],
providers: [ScheduleService, ScheduleRepository, Data, GeneticAlgorithm, Population],
controllers: [ScheduleController],
})
export class ScheduleModule {}