simplex是线性规划中十分经典的算法,以下为实现代码
头文件
#ifndef SIMPLEX_H_INCLUDED
#define SIMPLEX_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
#define MAX_VALUE 1.0e308
int m ,n;
typedef double Elemtype;
typedef struct calcu_node
{
Elemtype **A;
Elemtype *B;
Elemtype *C;
int *B1;/*基向量对应的位置*/
Elemtype z;
} calcu_node,*pcalcu_node;
typedef struct result
{
Elemtype *x;
Elemtype z;
} result,*presult;
pcalcu_node init_simplex(pcalcu_node calcu_node);
presult simplex(Elemtype **input_A,Elemtype *input_B,Elemtype *input_C);
Elemtype *calculate_x(pcalcu_node calcu_node);
void pivot(pcalcu_node calcu_node,int l,int e);
#endif // SIMPLEX_H_INCLUDED
具体实现
#include "simplex.h"
Elemtype *calculate_x(pcalcu_node calcu_node)
{
Elemtype *x = (Elemtype*)malloc(n * sizeof(Elemtype));
int j;
for(j = 0; j < n; j++)
{
if(calcu_node->B1[j] == 0)
x[j] = 0;
else
{
int i = 0;
for(i; i < m; i++)
{
if(calcu_node->A[i][j] == 1)
x[j] = calcu_node->B[i];
}
}
}
return x;
}
presult simplex(Elemtype **input_A,Elemtype *input_B,Elemtype *input_C)
{
pcalcu_node calcu_node = (pcalcu_node)malloc(sizeof(calcu_node));
calcu_node->A = (Elemtype**)malloc(m * (sizeof(Elemtype)));
calcu_node->B = (Elemtype*)malloc(m * sizeof(Elemtype));
int i = 0,j = 0;
for(; i < m; i++)
{
calcu_node->B[i] =