问题遇到的现象和发生背景
reproduce一篇DEA模型的论文(线性规划),之前代码用Python写好了,但导师让用C再重新写一遍,用的GLPK库。因为这个库我之前没用过,很多操作都是借助AI完成。数据文件重新另存成csv格式,跑不通后,又拿前两行数据重新存了个csv文件调试,还是相同的报错。debug了两天,没找着问题在哪,不知道是库配置或者环境的问题还是环境或者代码的问题。
遇到的现象和发生背景,请写出第一个错误信息
用代码块功能插入代码,请勿粘贴截图。 不用代码块回答率下降 50%
根据回答修改了数组传递和索引,还是报错
修改后的完整代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glpk.h>
#define MAX_LINE_LEN 1024
typedef struct
{
int year;
char securities_code[20];
double financial_expense;
double dividend;
double operating_cost;
double total_asset;
double employee;
double total_profit;
double operating_income;
double non_current_liabilities;
double paid_in_capital;
} DMU;
DMU *read_csv(const char *filename, int *count)
{
FILE *fp = fopen(filename, "r");
if (!fp)
{
perror("Error opening file");
return NULL;
}
DMU *data = NULL;
char line[MAX_LINE_LEN];
*count = 0;
// 跳过 UTF-8 BOM
if (fgetc(fp) == 0xEF && fgetc(fp) == 0xBB && fgetc(fp) == 0xBF)
{
// BOM 已检测
}
else
{
rewind(fp);
}
// 跳过标题行
fgets(line, sizeof(line), fp);
while (fgets(line, sizeof(line), fp))
{
DMU *temp = realloc(data, (*count + 1) * sizeof(DMU));
if (!temp)
{
free(data);
fclose(fp);
return NULL;
}
data = temp;
DMU *d = &data[*count];
memset(d, 0, sizeof(DMU));
char *token;
int field = 0; // 定义一次
int max_fields = 11; // 总字段数
token = strtok(line, ",");
while (token != NULL && field < max_fields)
{
switch (field)
{
case 0:
d->year = atoi(token);
printf("Parsing field %d: year = %d\n", field, d->year);
break;
case 1:
strncpy(d->securities_code, token, sizeof(d->securities_code) - 1);
d->securities_code[sizeof(d->securities_code) - 1] = '\0'; // 确保终止
break;
case 2:
d->financial_expense = atof(token);
printf("Parsing field %d: financial_expense = %f\n", field, d->financial_expense);
break;
case 3:
d->dividend = atof(token);
break;
case 4:
d->operating_cost = atof(token);
break;
case 5:
d->total_asset = atof(token);
break;
case 6:
d->employee = atof(token);
break;
case 7:
d->total_profit = atof(token);
if (d->total_profit <= 0)
{
fprintf(stderr, "Error: Line %d has invalid total_profit.\n", *count + 1);
free(data);
fclose(fp);
return NULL;
}
break;
case 8:
d->operating_income = atof(token);
if (d->operating_income <= 0)
{
fprintf(stderr, "Error: Line %d has invalid operating_income.\n", *count + 1);
free(data);
fclose(fp);
return NULL;
}
break;
case 9:
d->non_current_liabilities = atof(token);
break;
case 10:
d->paid_in_capital = atof(token);
if (d->paid_in_capital < 0)
{
fprintf(stderr, "Error: Line %d has invalid paid_in_capital.\n", *count + 1);
free(data);
fclose(fp);
return NULL;
}
break;
default:
break;
}
token = strtok(NULL, ",");
field++;
}
// 检查字段数量
if (field != max_fields)
{
fprintf(stderr, "Error: Line %d has %d fields (expected 11).\n", *count + 1, field);
free(data);
fclose(fp);
return NULL;
}
(*count)++;
}
fclose(fp);
return data;
}
double calculate_efficiency(DMU *dmus, int num_dmus, int target_idx)
{
DMU *target = &dmus[target_idx];
glp_prob *lp = glp_create_prob();
glp_set_obj_dir(lp, GLP_MAX);
// ================== Add Variables ==================
glp_add_cols(lp, 7); // 列索引1-7
for (int i = 1; i <= 7; i++)
{
glp_set_col_bnds(lp, i, GLP_LO, 0.0, 0.0);
}
// ================== Objective Function ==================
glp_set_obj_coef(lp, 6, target->operating_income); // u0 (第6列)
glp_set_obj_coef(lp, 7, target->total_profit); // u1 (第7列)
// ================== Constraints ==================
int row_count = 0;
// 约束1: 标准化输入
row_count++;
glp_add_rows(lp, 1);
glp_set_row_bnds(lp, row_count, GLP_FX, 1.0, 1.0);
{
int ind[] = {1, 2, 3, 4, 5}; // 前5列变量
double val[] = {target->financial_expense,
target->operating_cost,
target->total_asset,
target->employee,
target->non_current_liabilities};
printf("Setting row %d, ind[] = ", row_count);
for (int k = 0; k < 5; k++)
{ // 正确循环次数
printf("%d ", ind[k]);
}
printf("\n");
glp_set_mat_row(lp, row_count, 5, ind, val);
}
// 约束2: 所有DMU的约束
for (int j = 0; j < num_dmus; j++)
{
printf("Setting DMU %d constraint (row %d):\n", j, row_count);
DMU *dmu = &dmus[j];
printf(" financial_expense = %f\n", dmu->financial_expense);
printf(" operating_cost = %f\n", dmu->operating_cost);
printf(" total_asset = %f\n", dmu->total_asset);
printf(" employee = %f\n", dmu->employee);
printf(" non_current_liabilities = %f\n", dmu->non_current_liabilities);
printf(" operating_income = %f\n", dmu->operating_income);
printf(" total_profit = %f\n", dmu->total_profit);
row_count++;
glp_add_rows(lp, 1);
glp_set_row_bnds(lp, row_count, GLP_LO, 0.0, 0.0);
int ind[] = {1, 2, 3, 4, 5, 6, 7}; // 必须包含7个合法索引
double val[] = {dmu->financial_expense,
dmu->financial_expense,
dmu->operating_cost,
dmu->total_asset,
dmu->employee,
dmu->non_current_liabilities,
-dmu->operating_income,
-dmu->total_profit};
// 打印调试信息
printf("Setting DMU %d constraint (row %d): ind[] = ", j, row_count);
// 检查索引合法性
for (int k = 0; k < 7; k++)
{
if (ind[k] < 1 || ind[k] > 7)
{
fprintf(stderr, "Invalid column index %d at row %d\n", ind[k], row_count);
printf(" ind[%d] = %d, val[%d] = %f\n", k, ind[k], k, val[k]);
exit(1);
}
}
glp_set_mat_row(lp, row_count, 7, ind, val);
}
// ================== 求解 ==================
glp_simplex(lp, NULL);
double eff = glp_get_obj_val(lp);
glp_delete_prob(lp);
return eff;
}
int main()
{
int num_dmus;
DMU *dmus = read_csv("./test.csv", &num_dmus);
if (!dmus)
return 1;
for (int i = 0; i < num_dmus; i++)
{
double eff = calculate_efficiency(dmus, num_dmus, i);
printf("DMU %s (%d): Efficiency = %.4f\n",
dmus[i].securities_code, dmus[i].year, eff);
}
free(dmus);
return 0;
}
VScode终端运行结果及详细报错内容
PS D:\DEA_code> .\model2
Parsing field 0: year = 2023
Parsing field 2: financial_expense = 1000.000000
Parsing field 0: year = 2023
Parsing field 2: financial_expense = 2000.000000
Setting row 1, ind[] = 1 2 3 4 5
glp_set_mat_row: i = 1; ind[5] = -804953840; column index out of range
Error detected in file ../../glpk-5.0/src/api/prob1.c at line 771
PS D:\DEA_code>
目录结构如图

我的解答思路和尝试过的方法,不写自己思路的,回答率下降 60%
尝试过添加各种调试信息,并截取前两行数据单独放csv测试,以及重新安装GLPK库,都没有解决
根据回答修改了数组传递和索引,还是报错
新的报错信息和运行结果:
PS D:\DEA_code> .\model2
Parsing field 0: year = 2023
Parsing field 2: financial_expense = 1000.000000
Parsing field 0: year = 2023
Parsing field 2: financial_expense = 2000.000000
Setting row 1, ind[] = 1 2 3 4 5
glp_set_mat_row: i = 1; ind[5] = 0; column index out of range
Error detected in file ../../glpk-5.0/src/api/prob1.c at line 771
PS D:\DEA_code>
我想要达到的结果,如果你需要快速回答,请尝试 “付费悬赏”
我想找到什么地方出了问题,怎么解决