Nest 无法解决(?)Population 的依赖关系

问题描述 投票:0回答:1

我用遗传算法(nest.js)编码了班级调度。我的程序有 Geneticalgorithm.ts、data.ts、population.ts、scheduleservice.ts、schedulemodule.ts。人口是在我在遗传算法类中使用的方法中调用的。作为构造函数。我需要将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 {}

javascript node.js typescript nestjs nest
1个回答
0
投票

NestJS 使用基于构造函数的注入,因此当您说

Population
是一个 NestJS 提供者时,您必须告诉如何解析
size
data
提供商也是如此。

这是非常基本的内容,您应该能够通过阅读文档网站来掌握:https://docs.nestjs.com

您可以通过创建自定义提供程序并使用这些

data
size
参数或在它们上添加
@Optional()
参数装饰器来告诉它们是可选提供程序来解决这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.