first commit

This commit is contained in:
2025-12-18 16:00:22 +08:00
commit 785f306726
69 changed files with 33171 additions and 0 deletions

View File

@ -0,0 +1,16 @@
CC=g++
all: data_gen main analyser
data_gen:
$(CC) data_gen.cpp -o data_gen
main:
$(CC) main.cpp algorithm.cpp test.cpp -o main
analyser:
$(CC) analyser.cpp algorithm.cpp test.cpp -o analyser
.PHONY(clean):
clean:
rm -rf *.o *.data data_gen main analyser

View File

@ -0,0 +1,29 @@
DEMO
===========================
##环境依赖
gcc
##目录结构描述
├── README.md // help \
├── main.cpp // 主文件 \
├── algorithm.h // 算法声明头文件 \
├── algorithm.cpp // 算法实现 \
├── data_gen.cpp // 测试集数据生成 \
├── analyser.cpp // 插桩代码测试 \
├── test.h // 测试函数声明头文件 \
├── test.cpp // 测试函数实现 \
├── test.sh // 辅助测试的bash脚本 \
├── Makefile // 编译规则
##测试步骤
1. 编译
2. 生成测试集
3. 测试、插桩测试
(可通过简单的bash脚本进行批量生成数据集和批量测试)
##V1.0 版本内容更新
1. 创建文件与基础实现

View File

@ -0,0 +1,144 @@
/***************************************************************************** *
* Copyright (C) 2022 Mingtian Shao shaomt@nudt.edu.cn *
* *
* This file is part of homework of Algorithm Design and Analysis. *
* *
* @file algorithm.cpp *
* @brief Implement different algorithms *
* *
* @author Mingtian Shao *
* @email shaomt@nudt.edu.cn *
* @version 1.0 *
* @date 2022-11-12 *
* @license GNU General Public License (GPL) *
* *
*----------------------------------------------------------------------------*
* Change History : *
* <Date> | <Version> | <Author> | <Description> *
*----------------------------------------------------------------------------*
* 2022/11/12 | 1.0 | Mingtian Shao | Create file *
*----------------------------------------------------------------------------*
* *
*****************************************************************************/
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <stack>
#include <string>
#include <cstring>
#include "algorithm.h"
using namespace std;
/**
* @func algorithm_1
* @brief 解决背包问题思路一
* @param n: 物品种类
* @param w: 背包容量限制
* @param weights: 物品重量数组
* @param values: 物品价值数组
* @param solve: 保存解的数组
*
* @return 返回背包容纳的最大价值
*/
int algorithm_1(int n, int w, int* weights, int* values, int* solve)
{
// memo index start from 0, which means type-1 or weight-1
vector< vector<int> > memo(n);
vector< vector<int> > nums(n);
for (int i = 0; i < n; i++)
{
memo[i].resize(w);
nums[i].resize(w);
}
// avoid the inefficient branch for i = 0 in the following ciculation
for (int j = 0; j < w; j++) // avaliable weight capacity - 1
{
int k = (j + 1) / weights[0];
memo[0][j] = k * values[0]; // take as many as possible
nums[0][j] = k;
}
for (int i = 1; i < n; i++) // index of chosen object
{
for (int j = 0; j < w; j++) // avaliable weight capacity - 1
{
int maxvalue = 0, maxk = 0;
for (int k = 0; k <= (j + 1) / weights[i]; k++)
{
int tmp = k * values[i];
if (j - k * weights[i] >= 0)
{
tmp += memo[i - 1][j - k * weights[i]];
}
if (tmp > maxvalue)
{
maxvalue = tmp;
maxk = k;
}
}
memo[i][j] = maxvalue;
nums[i][j] = maxk;
}
}
int tmpw = w - 1;
for (int i = n - 1; i >= 0; i--)
{
solve[i] = nums[i][tmpw];
tmpw -= solve[i] * weights[i];
}
return memo[n - 1][w - 1];
}
/**
* @func 函数名
* @brief 函数简要说明
* @param 参数1
* @param 参数2
*
* @return 返回说明
*/
int algorithm_2(int n, int w, int* weights, int* values, int* solve)
{
/*
* coding
*/
}
/**
* @func algorithm_1_analy
* @brief 在函数algorithm_1的基础上进行插桩记录关键操作次数
* @param n: 物品种类
* @param w: 背包容量限制
* @param weights: 物品重量数组
* @param values: 物品价值数组
* @param solve: 保存解的数组
* @param calc_count: 记录计算次数
* @param search_count: 记录查找次数
*
* @return 返回背白容纳的最大价值
*/
int algorithm_1_analy(int n, int w, int* weights, int* values, int* solve, int& calc_count, int& search_count)
{
/*
* coding
*/
}
/**
* @func 函数名
* @brief 函数简要说明
* @param 参数1
* @param 参数2
*
* @return 返回说明
*/
int algorithm_2_analy(int n, int w, int* weights, int* values, int* solve, int& calc_count, int& search_count)
{
/*
* coding
*/
}

View File

@ -0,0 +1,17 @@
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cstdlib>
#include <stack>
#include <string>
#include <cstring>
using namespace std;
int algorithm_1(int, int, int*, int*, int*);
int algorithm_2(int, int, int*, int*, int*);
int algorithm_1_analy(int, int, int*, int*, int*, int&, int&);
int algorithm_2_analy(int, int, int*, int*, int*, int&, int&);

View File

@ -0,0 +1,98 @@
/*****************************************************************************
* Copyright (C) 2022 Mingtian Shao shaomt@nudt.edu.cn *
* *
* This file is part of homework of Algorithm Design and Analysis. *
* *
* @file analyser.cpp *
* @brief Analyze the number of critical operations *
* for different algorithms *
* *
* @author Mingtian Shao *
* @email shaomt@nudt.edu.cn *
* @version 1.0 *
* @date 2022-11-12 *
* @license GNU General Public License (GPL) *
* *
*----------------------------------------------------------------------------*
* Change History : *
* <Date> | <Version> | <Author> | <Description> *
*----------------------------------------------------------------------------*
* 2022/11/12 | 1.0 | Mingtian Shao | Create file *
*----------------------------------------------------------------------------*
* *
*****************************************************************************/
#include <iostream>
#include <fstream>
#include <random>
#include <cmath>
#include "algorithm.h"
#include "test.h"
using namespace std;
/**
* @func analy_fun
* @brief 插桩算法测试
* @param data_file: 测试集文件
* @param cntcalc: 计算计数器
* @param cntsearch: 查找计数器
* @param fun: 测试的算法函数
*
* @return 重复测试的次数(测试数据的组数)
*/
int analy_fun(char *data_file, int &cntcalc, int &cntsearch, int (fun)(int, int, int*, int*, int*, int&, int&))
{
ifstream fin(data_file);
int result; //保存每个测试用例的最大价值
int repeat; //记录测试数据组数
// k: 生成数据的组数
// n: 物品的种类(问题的规模)
// w: 背包重量限制
int k, n, w;
fin >> k >> n >> w;
repeat = k;
cout << "数据集组数:" << k << " , 物品种类:" << n << " , 背包容量限制: " << w << endl;
int* weights = new int[n], * values = new int[n];
int* solve = new int[n];
while (k--) {
for (int i = 0; i < n; i++) {
fin >> weights[i];
}
for (int i = 0; i < n; i++) {
fin >> values[i];
solve[i] = 0;
}
result = fun(n, w, weights, values, solve, cntcalc, cntsearch);
}
delete[] weights;
delete[] values;
delete[] solve;
return repeat;
}
int main(int argc, char *argv[])
{
//input: data_file
if (argc != 2)
{
cout<< "参数错误参数数据集文件如test_x_x_x.data" << endl;
exit(1);
}
int cntcalc = 0;
int cntsearch = 0;
int repeat;
repeat = analy_fun(argv[1], cntcalc, cntsearch, algorithm_1_analy);
int average_calc = cntcalc / repeat;
int average_search = cntsearch / repeat;
cout << "average_calc: " << average_calc << endl;
cout << "average_search: " << average_search << endl;
return 0;
}

View File

@ -0,0 +1,81 @@
/*****************************************************************************
* Copyright (C) 2022 Mingtian Shao shaomt@nudt.edu.cn *
* *
* This file is part of homework of Algorithm Design and Analysis. *
* *
* @file data_gen.cpp *
* @brief Generate data and save it to a file *
* *
* @author Mingtian Shao *
* @email shaomt@nudt.edu.cn *
* @version 1.0 *
* @date 2022-11-12 *
* @license GNU General Public License (GPL) *
* *
*----------------------------------------------------------------------------*
* Change History : *
* <Date> | <Version> | <Author> | <Description> *
*----------------------------------------------------------------------------*
* 2022/11/12 | 1.0 | Mingtian Shao | Create file *
*----------------------------------------------------------------------------*
* *
*****************************************************************************/
#include <iostream>
#include <fstream>
#include <random>
#include <cmath>
using namespace std;
/**
* @func data_gen
* @brief 生成测试数据集
* @param k: 测试集数量
* @param n: 物品种类
* @param w: 背包重量限制
*
* @return null
*/
void data_gen(int k, int n, int w)
{
char out_file[100];
//输出文件名可参考"test_{k}_{n}_{w}.data"
sprintf(out_file, "test_%d_%d_%d.data", k, n, w);
ofstream fout(out_file);
fout << k << " " << n << " " << w << endl;
default_random_engine e(time(nullptr));
//随机数分布对象
uniform_int_distribution<signed> u(2, w / 1.5);
while (k--)
{
// 生成weight值
for (int i = 0; i < n; i++) //生成2~n的随机数序列
fout << u(e) << " ";
fout << endl;
// 生成value值
for (int i = 0; i < n; i++) //生成2~n的随机数序列
fout << u(e) << " ";
fout << endl;
}
}
int main(int argc, char *argv[])
{
// k: 生成数据的组数
// n: 物品的种类(问题的规模)
// w: 背包重量限制
if(argc != 4 )
{
cout << "参数错误,参数包括: 生成数据的组数k, 物品的种类n, 背包重量限制w" << endl;
exit(1);
}
int k, n, w;
k = atoi(argv[1]);
n = atoi(argv[2]);
w = atoi(argv[3]);
data_gen(k,n,w);
return 0;
}

View File

@ -0,0 +1,57 @@
/*****************************************************************************
* Copyright (C) 2022 Mingtian Shao shaomt@nudt.edu.cn *
* *
* This file is part of homework of Algorithm Design and Analysis. *
* *
* @file main.cpp *
* @brief Test the performance of different algorithms *
* *
* @author Mingtian Shao *
* @email shaomt@nudt.edu.cn *
* @version 1.0 *
* @date 2022-11-12 *
* @license GNU General Public License (GPL) *
* *
*----------------------------------------------------------------------------*
* Change History : *
* <Date> | <Version> | <Author> | <Description> *
*----------------------------------------------------------------------------*
* 2022/11/12 | 1.0 | Mingtian Shao | Create file *
*----------------------------------------------------------------------------*
* *
*****************************************************************************/
#include <ctime>
#include <cstdlib>
#include <stack>
#include <string>
#include <cstring>
#include "algorithm.h"
#include "test.h"
using namespace std;
int main(int argc, char *argv[])
{
//input: data_file
if (argc != 2)
{
cout<< "参数错误参数数据集文件如test_x_x_x.data" << endl;
exit(1);
}
long double average_time_1, average_time_2;
average_time_1 = test_5(argv[1], algorithm_1);
//average_time_2 = test_5(argv[1], algorithm_2);
cout << average_time_1 << endl;
//cout << average_time_2 << endl;
return 0;
/**
* 供参考:
* 可能的输入数据集文件、n物品种类、w背包重量上限
* 1. 读取data_gen生成的测试集文件
* 2. 使用不同算法对于测试集进行求解
* 3. 记录算法性能并保存到文件
* coding
*/
}

View File

@ -0,0 +1,122 @@
/*****************************************************************************
* Copyright (C) 2022 Mingtian Shao shaomt@nudt.edu.cn *
* *
* This file is part of homework of Algorithm Design and Analysis. *
* *
* @file algorithm.cpp *
* @brief test different algorithms *
* *
* @author Mingtian Shao *
* @email shaomt@nudt.edu.cn *
* @version 1.0 *
* @date 2022-11-12 *
* @license GNU General Public License (GPL) *
* *
*----------------------------------------------------------------------------*
* Change History : *
* <Date> | <Version> | <Author> | <Description> *
*----------------------------------------------------------------------------*
* 2022/11/12 | 1.0 | Mingtian Shao | Create file *
*----------------------------------------------------------------------------*
* *
*****************************************************************************/
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <stack>
#include <string>
#include <cstring>
#include <fstream>
#include "algorithm.h"
#include "test.h"
using namespace std;
/**
* @func PrintArray
* @brief 输出指针数组
* @param n: 数组长度
* @param nums: 整型指针数组
*
* @return null
*/
void PrintArray(int n, int* nums)
{
for (int i = 0; i < n; i++)
{
cout << nums[i] << " ";
}
cout << endl;
}
/**
* @func test_5
* @brief 测试算法运行时间并输出每组测试用例的结果
* @param data_file: 测试数据集文件
* @param fun: 测试算法的实现函数该函数有5个参数故该测试函数名为test_5
*
* @return 算法的平均运行时间
*/
long double test_5(char* data_file, int (fun)(int, int, int*, int*, int*))
{
double sum_time = 0;
clock_t start_time, end_time;
ifstream fin(data_file);
int result; //保存每个测试用例的最大价值
int repeat; //记录测试数据组数
// k: 生成数据的组数
// n: 物品的种类(问题的规模)
// w: 背包重量限制
int k, n, w;
fin >> k >> n >> w;
repeat = k;
cout << "数据集组数:" << k << " , 物品种类:" << n << " , 背包容量限制: " << w << endl;
int* weights = new int[n], * values = new int[n];
int* solve = new int[n];
while (k--) {
for (int i = 0; i < n; i++) {
fin >> weights[i];
}
for (int i = 0; i < n; i++) {
fin >> values[i];
solve[i] = 0;
}
cout << "weights:" << endl;
PrintArray(n, weights);
cout << "values:" << endl;
PrintArray(n, values);
// 仅记录算法运行时间
start_time = clock();
result = fun(n, w, weights, values, solve);
end_time = clock();
sum_time += (double)(end_time - start_time) / CLOCKS_PER_SEC;
cout << "max value is " << result << endl;
cout << "the solution is "; PrintArray(n, solve);
}
delete[] weights;
delete[] values;
delete[] solve;
long double average_time = sum_time / repeat;
return average_time;
}
/**
* @func test_7
* @brief 主要用于记录算法fun的关键操作次数
* @param data_file: 测试数据集文件
* @param fun: 测试算法的实现函数该函数有7个参数故该测试函数名为test_7
*
* @return 算法的平均运行时间,该值仅供参考,因为插桩代码影响了算法运行效率
*/
long double test_7(char* data_file, int (fun)(int, int, int*, int*, int*, int&, int&))
{
/*
* coding
*/
}

View File

@ -0,0 +1,14 @@
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cstdlib>
#include <stack>
#include <string>
#include <cstring>
#include <fstream>
using namespace std;
long double test_5(char*, int (fun)(int, int, int*, int*, int*));
long double test_7(char*, int (fun)(int, int, int*, int*, int*, int&, int&));

View File

@ -0,0 +1,22 @@
#!/bin/bash
# 测试脚本,下述内容仅为示例
# 使用前先增加执行权限: sudo chmod +x test.sh
echo "test srart"
./data_gen 1000 100 100
./main test_1000_100_100.data
./analyser test_1000_100_100.data
echo "--------------------------------------------"
./data_gen 1000 500 500
./main test_1000_500_500.data
./analyser test_1000_500_500.data
echo "--------------------------------------------"
echo "test end"