基于Springboot+Mybatis+Mysql的网盘文件管理系统

简介: 基于Springboot+Mybatis+Mysql的网盘文件管理系统

基于Springboot+Mybatis+Mysql的网盘文件管理系统


一、算法介绍


具体百度


1.1 三个重要式子


(1)每条路选中概率

其中alpha 和 beta 参数自定义

alpha为信息因子占的权重,权重过高,蚂蚁选中已走的路概率越高,开始走新路概率低

beta为两点距离的权重(能见度),权重过高,蚂蚁优先走两点最短距离的路

{% asset_img rate.png %}


(2)信息素更新

信息素更新包括:信息素挥发 以及 信息素新增

蚂蚁走完一遍所有城市后,就开始先计算挥发,再计算新增

{% asset_img sub.png %}

{% asset_img add.png %}


二、Java实现代码(TSP问题)


2.1 TSP问题


  • 简单来说,就是寻找一条访问所有城市,最短的一条路线
  • 前提:所有城市之间是互通的,任意两个城市可以来往


2.2 Java实现代码


//创建一个蚂蚁类 Ant.java
public class Ant {
    private List<Integer> route = new ArrayList<Integer>(); //路线:记录蚂蚁行走的路线
    private Integer currentCity; //当前城市:记录蚂蚁当前所在城市的ID
    private List<Integer> citys = new ArrayList<Integer>(); //城市:记录需要行走的城市ID
    private double[][] pheromoneMatrix; //信息素矩阵
    private int[][] distanceMatrix; //距离矩阵(整型方便查看)
    //构造器
    public Ant(List<Integer> citys, double[][] pheromoneMatrix, int[][] distanceMatrix) {
        this.citys = citys;
        this.pheromoneMatrix = pheromoneMatrix;
        this.distanceMatrix = distanceMatrix;
    }
    /**
     * 添加路线
     * @param cityId
     */
    private void addRoute(Integer cityId) {
        route.add(cityId); //路线中添加城市ID
        currentCity = cityId; //当前城市修改为城市ID
        citys.remove(cityId); //需要行走的城市移除当前城市ID
    }
    /**
     * 随机选择初始城市
     */
    private void randomSelectCity() {
        Random random = new Random();
        Integer cityId = random.nextInt(citys.size())+1;
        addRoute(cityId);
    }
    /**
     * 选择下一个城市
     */
    private void selectNextCity() {
        if(citys.size() == 1) {
            addRoute(citys.get(0));
            addRoute(route.get(0)); //路线添加最开始的城市
            return;
        }
        Double alpha = 1.0; //信息素因子权重
        Double beta  = 2.0; //路线距离权重
        Map<Integer, Double> molecules = new HashMap<Integer, Double>();
        //计算选路概率公式中的分子
        for (Integer city : citys) {
            //城市从1开始数,数组从0开始数,所以涉及数组都要‘-1’
            Double molecule = Math.pow(pheromoneMatrix[currentCity-1][city-1], alpha) * Math.pow(1.0 / distanceMatrix[currentCity-1][city-1], beta);
            molecules.put(city, molecule);
        }
        //计算选路概率公式中的分母
        Double totalMolecule = 0.0;
        for(Integer city : molecules.keySet()) {
            totalMolecule += molecules.get(city);
        }
        //轮盘赌选择下一个城市
        double random = Math.random();
        Double temp = 0.0;
        for(Integer city : molecules.keySet()) {
            temp += molecules.get(city) / totalMolecule;
            if(temp >= random) {
                addRoute(city);
                break;
            }
        }
    }
    /**
     * 蚂蚁开始旅行所有城市
     */
    public void tour() {
        int cityQuantity = citys.size();
        randomSelectCity();
        for(int i=0; i<cityQuantity; i++) {
            selectNextCity();
        }
    }
    /**
     * 获取路线
     * @return
     */
    public List<Integer> getRoute() {
        return route;
    }
    /**
     * 计算路线总距离
     * @return
     */
    public Integer getRouteLength() {
        Integer length = 0;
        for(int i=0; i<route.size()-1; i++) {
            length += distanceMatrix[route.get(i)-1][route.get(i+1)-1];
        }
        return length;
    }
}
//创建一个AOC类:是蚁群优化算法主要实现类
public class ACO {
    List<Integer> citys = new ArrayList<Integer>(); //城市集合
    private double[][] pheromoneMatrix; //信息素矩阵
    private int[][] distanceMatrix; //距离矩阵
    private int times; //运行次数
    private int antQuantity; // 蚂蚁数量
    private List<Integer> bestRoute; //最佳路线
    private Integer bestLength = -1; //最佳距离
    //构造器
    public ACO(List<Integer> citys, double[][] pheromoneMatrix, int[][] distanceMatrix, int times, int antQuantity) {
        this.citys = citys;
        this.pheromoneMatrix = pheromoneMatrix;
        this.distanceMatrix = distanceMatrix;
        this.times = times;
        this.antQuantity = antQuantity;
    }
    /**
     * 更新信息素
     * @param ants 蚂蚁群
     */
    private void update(List<Ant> ants) {
        //信息素挥发
        double volatilizationRate = 0.5; //挥发率
        for(int i=0; i<citys.size(); i++) {
            for(int j=0; j<citys.size(); j++) {
                pheromoneMatrix[i][j] = pheromoneMatrix[i][j] * (1.0-volatilizationRate);
            }
        }
        //信息素新增
        for(Ant ant : ants) {
            List<Integer> route = ant.getRoute();
            for(int i=0; i<route.size()-1; i++){
                pheromoneMatrix[route.get(i)-1][route.get(i+1)-1] += 1.0 / ant.getRouteLength();
            }
        }
    }
    /**
     * 记录最佳路径和最佳距离
     */
    private void recordBest(List<Ant> ants) {
        //给bestLength赋予初始值
        if(bestLength == -1.0) {
            bestLength = ants.get(0).getRouteLength();
            bestRoute = ants.get(0).getRoute();
        }
        //遍历比较最佳
        for(Ant ant : ants) {
            if(bestLength > ant.getRouteLength()) {
                bestLength = ant.getRouteLength();
                bestRoute = ant.getRoute();
            }
        }
    }
    /**
     * 运行蚁群优化算法
     */
    private void runAlgorithm(){
        //创建蚂蚁集合存储蚂蚁
        List<Ant> ants = new ArrayList<Ant>();
        for(int i=0; i<antQuantity; i++){
            //复制城市集合(集合为地址引用,为了不影响原参数,复制一个新集合)
            List<Integer> cityCopy = new ArrayList<Integer>();
            for (Integer city : citys) {
                cityCopy.add(city);
            }
            //创建蚂蚁,并开始旅行所有城市
            Ant ant = new Ant(cityCopy, pheromoneMatrix, distanceMatrix);
            ant.tour();
            ants.add(ant);
        }
        update(ants); //更新信息素
        recordBest(ants); //记录最佳路线与距离
    }
    /**
     * 多次运行蚁群优化算法(蚂蚁算法的运行入口)
     */
    public void run() {
        for(int i=0; i<times; i++){
            runAlgorithm();
        }
    }
    /**
     * 获取最佳路线
     */
    public List<Integer> getBestRoute() {
        return bestRoute;
    }
    /**
     * 获取最佳距离
     */
    public Integer getBestLength() {
        return bestLength;
    }
}

针对TSP问题的蚂蚁优化算法就写好了,只要创建AOC,传入对应的参数,运行run()方法就会跑起来,通过getXxxx获取路线以及路线长度

//主程序代码演示
public class Main {
    public static void main(String[] args) {
        //自定义距离矩阵
        int[][] distanceMatrix = new int[][]{{0, 1, 3, 1}, {1, 0, 3, 2}, {3, 3, 0, 2}, {1, 2, 2, 0}};
        //创建信息素矩阵并赋初值
        double[][] pheromoneMatrix = new double[4][4];
        for(int i=0; i<distanceMatrix.length; i++) {
            for(int j=0; j<distanceMatrix.length; j++) {
                pheromoneMatrix[i][j] = 0.1;
            }
        }
        //创建城市集合
        List<Integer> citys = new ArrayList<Integer>();
        for(int i=0; i<4; i++) {
            citys.add(i+1);
        }
        //运行蚁群优化算法
        ACO aco = new ACO(citys, pheromoneMatrix, distanceMatrix, 50, 6);
        aco.run();
        System.out.println(aco.getBestRoute());
        System.out.println(aco.getBestLength());
    }
}


2.3 自定义工具类


  • 由于自己定义距离矩阵,信息素矩阵觉得麻烦,就创建一个工具类实现创建
  • (1)将城市信息存储在一个txt文件中
  • 城市ID X轴 Y轴


源码+数据库:https://download.csdn.net/download/wyn_365/85430779


ae15d214fc1a4f2a8503b662498682e7.png

cff2138f38d64b059dfcf330d7d3080c.png

c3129ca4efa94833a2406b804a5ae77f.png

959e987a8299465e9b3b4fe50e8dc7f0.png

2df6b9d336cf429ba9b90e42ea2fe90a.png

38ca36085f144606857c8c5db3bd8418.png

7aa6779e96b24cb8b736087c2b86ade8.png

57013a7986ab4514a1c98c22e591dfe4.png

8570f75350164f32b5e74a50c839a71d.png

相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
目录
相关文章
|
人工智能 运维 Java
SpringBoot+MySQL实现动态定时任务
这是一个基于Spring Boot的动态定时任务Demo,利用spring-context模块实现任务调度功能。服务启动时会扫描数据库中的任务表,将任务添加到调度器中,并通过固定频率运行的ScheduleUpdater任务动态更新任务状态和Cron表达式。核心功能包括任务的新增、删除与Cron调整,支持通过ScheduledFuture对象控制任务执行。项目依赖Spring Boot 2.2.10.RELEASE,使用MySQL存储任务信息,包含任务基类ITask及具体实现(如FooTask),便于用户扩展运维界面以增强灵活性。
567 10
|
XML Java 数据库连接
微服务——SpringBoot使用归纳——Spring Boot集成MyBatis——基于 xml 的整合
本教程介绍了基于XML的MyBatis整合方式。首先在`application.yml`中配置XML路径,如`classpath:mapper/*.xml`,然后创建`UserMapper.xml`文件定义SQL映射,包括`resultMap`和查询语句。通过设置`namespace`关联Mapper接口,实现如`getUserByName`的方法。Controller层调用Service完成测试,访问`/getUserByName/{name}`即可返回用户信息。为简化Mapper扫描,推荐在Spring Boot启动类用`@MapperScan`注解指定包路径避免逐个添加`@Mapper`
1404 0
|
Java 数据库连接 数据库
Spring boot 使用mybatis generator 自动生成代码插件
本文介绍了在Spring Boot项目中使用MyBatis Generator插件自动生成代码的详细步骤。首先创建一个新的Spring Boot项目,接着引入MyBatis Generator插件并配置`pom.xml`文件。然后删除默认的`application.properties`文件,创建`application.yml`进行相关配置,如设置Mapper路径和实体类包名。重点在于配置`generatorConfig.xml`文件,包括数据库驱动、连接信息、生成模型、映射文件及DAO的包名和位置。最后通过IDE配置运行插件生成代码,并在主类添加`@MapperScan`注解完成整合
1890 1
Spring boot 使用mybatis generator 自动生成代码插件
|
Java 关系型数据库 MySQL
SpringBoot 通过集成 Flink CDC 来实时追踪 MySql 数据变动
通过详细的步骤和示例代码,您可以在 SpringBoot 项目中成功集成 Flink CDC,并实时追踪 MySQL 数据库的变动。
3930 45
|
SQL Java 数据库
解决Java Spring Boot应用中MyBatis-Plus查询问题的策略。
保持技能更新是侦探的重要素质。定期回顾最佳实践和新技术。比如,定期查看MyBatis-Plus的更新和社区的最佳做法,这样才能不断提升查询效率和性能。
1037 1
|
监控 Java 关系型数据库
Spring Boot整合MySQL主从集群同步延迟解决方案
本文针对电商系统在Spring Boot+MyBatis架构下的典型问题(如大促时订单状态延迟、库存超卖误判及用户信息更新延迟)提出解决方案。核心内容包括动态数据源路由(强制读主库)、大事务拆分优化以及延迟感知补偿机制,配合MySQL参数调优和监控集成,有效将主从延迟控制在1秒内。实际测试表明,在10万QPS场景下,订单查询延迟显著降低,超卖误判率下降98%。
698 5
|
前端开发 Java 数据库连接
Java后端开发-使用springboot进行Mybatis连接数据库步骤
本文介绍了使用Java和IDEA进行数据库操作的详细步骤,涵盖从数据库准备到测试类编写及运行的全过程。主要内容包括: 1. **数据库准备**:创建数据库和表。 2. **查询数据库**:验证数据库是否可用。 3. **IDEA代码配置**:构建实体类并配置数据库连接。 4. **测试类编写**:编写并运行测试类以确保一切正常。
1076 2
|
XML Java 数据库连接
微服务——SpringBoot使用归纳——Spring Boot集成MyBatis——基于注解的整合
本文介绍了Spring Boot集成MyBatis的两种方式:基于XML和注解的形式。重点讲解了注解方式,包括@Select、@Insert、@Update、@Delete等常用注解的使用方法,以及多参数时@Param注解的应用。同时,针对字段映射不一致的问题,提供了@Results和@ResultMap的解决方案。文章还提到实际项目中常结合XML与注解的优点,灵活使用两者以提高开发效率,并附带课程源码供下载学习。
1347 0
|
Java 数据库连接 数据库
微服务——SpringBoot使用归纳——Spring Boot集成MyBatis——MyBatis 介绍和配置
本文介绍了Spring Boot集成MyBatis的方法,重点讲解基于注解的方式。首先简述MyBatis作为持久层框架的特点,接着说明集成时的依赖导入,包括`mybatis-spring-boot-starter`和MySQL连接器。随后详细展示了`properties.yml`配置文件的内容,涵盖数据库连接、驼峰命名规范及Mapper文件路径等关键设置,帮助开发者快速上手Spring Boot与MyBatis的整合开发。
2176 0
|
Java 关系型数据库 MySQL
如何将Spring Boot + MySQL应用程序部署到Pivotal Cloud Foundry (PCF)
如何将Spring Boot + MySQL应用程序部署到Pivotal Cloud Foundry (PCF)
483 5

推荐镜像

更多