单纯形算法的c语言实现

该博客介绍了如何用C语言实现单纯形算法,专注于解决最大值标准问题。输入数据存储于input.txt,输出数据写入output.txt。算法设计考虑了约束方程组,包括变量、松驰变量和目标函数系数。初始基变量的指定决定了基矩阵单位化的顺序,并通过约束方程的系数和常数项来构建问题。
摘要由CSDN通过智能技术生成

单纯形算法的c语言实现


利用五一的假期,把运筹学中的单纯形算法写了一下,有些粗略,但基本可用,只对几个确定有解的简单样本进行了试算,没有对无解的情况进行测试。
本程序把迭代时入基变量,出基变量,出基变量所在的行都作了打印输出,实施行变换后的矩阵也进了输出,适合学生拿来练手,或教师做教案时用。
文章的最后,有输入文件的说明。
闲话少说,直接上代码。
samplex.c

#include <stdio.h>
#include <stdlib.h>

#define BUFFSIZE 8192

FILE * file,*fileOut;
int m,n;
double *A; //m*(n+1)matrix,Constraint Equation Matrix
int* baseVariableOrder ;//m个
double *objectiveFunctionCoefficient; //n个
double *augmentFeasibleSolution;
char buff[BUFFSIZE];
double *checkData;
int inVariable,outRow,outVariable;
double *checkRatio;
double maxCheckData;

void printObjctiveFunction();
void adjust();

int main(){
   
	file = fopen("input.txt","r");
	if (file==NULL){
   
		printf("Can't find input file!");
		exit(1);
	}
	fileOut = fopen("output.txt","w");
	if (file==NULL){
   
		printf("Can't Create output file!");
		exit(1);
	}	
	for ( int i = 0; i < BUFFSIZE; i++ ){
   
        buff[i] = 0;
	}

	fgets( buff , BUFFSIZE , file );
	sscanf( buff , "%d %d", &m, &n );

	printf( "\nconstraint equation rows are %d, the variables are %d\n", m , n );
	fprintf(fileOut,"\nconstraint equation rows are %d, the variables are %d\n", m , n );


	A = (double *)calloc(sizeof(double),m*(n+1));
    objectiveFunctionCoefficient = (double *)calloc(sizeof(double),n);
	baseVariableOrder = (int *)calloc(sizeof(int),m);
	augmentFeasibleSolution	= (double *)calloc(sizeof(double),n);
	checkData = (double *)calloc(sizeof(double),n);
    checkRatio = (double *)calloc(sizeof(double),m);

    //objectiveFunctionCoefficient
	fgets( buff , BUFFSIZE , file );
    char *start,*endptr ;
    start = buff;
    for ( int i = 0; i < n; i ++ ){
   
        objectiveFunctionCoefficient[i] = strtod( start,&endptr);
        start = endptr;
    }
    printObjctiveFunction();

    //baseVariableOrder
    //asign intial base variabbles and give the sequence
    //the base matrix must be identity matrix!
	fgets( buff , BUFFSIZE , file );
    start = buff;
    endptr = NULL;
    for ( int i = 0; i < m; i ++ ){
   
        baseVariableOrder[i] = strtod( start,&endptr);
        start = endptr;
    }
    //A-constraint Matix
     for</
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值