0

I have implemented branch and bound algorithm , the algorithm is as follows:

  1. find the optimal solution to the linear programming model with the integer restrictions relaxed.
  2. at node 1 let the relaxed solution be the upper bound and the rounded down integer solution be the lower bound
  3. select the variable with the greatest fractional part for branching create two new constraints for this variable reflecting the partitioned integer values.the result will be a new <= constraint and a new >= constraint.
  4. create two new nodes ,one for the >= constraint and one for <= constraint .
  5. solve the relaxed linear programming with the new constraint added at each of these nodes.
  6. the relaxed solution is upper bound at each node and the existing maximum integer solution (at any node) is the lower bound.
  7. If the greatest upper bound value of any ending node
  8. return to step 3.

The implementation is as follows:

#include <stdio.h>
#include <math.h>
#include "time.h"
#include<sys/timeb.h>


#define CMAX  100  //max. number of variables in economic function
#define VMAX  100  //max. number of constraints


  int NC, NV, NOPTIMAL,P1,P2,XERR,NC1,NC2;
  double TS[CMAX][VMAX];


  void Data() 
  {
    double R1,R2;
    char R;
    int I,J;
    printf("\n LINEAR PROGRAMMING\n\n");
    printf(" MAXIMIZE (Y/N) ? "); 
    scanf_s("%c", &R);
     printf("\n");
  printf(" Number of variables in E.F.: "); scanf_s("%d", &NV);
  printf(" Number of <= inequalities..: "); scanf_s("%d", &NC);
  printf(" Number of >= inequalities..: "); scanf_s("%d", &NC1);
  printf(" Number of = equalities.....: "); scanf_s("%d", &NC2);
  /*  printf("\n NUMBER OF VARIABLES OF ECONOMIC FUNCTION ? "); 
    scanf_s("%d", &NV);
    printf("\n NUMBER OF CONSTRAINTS ? "); 
    scanf_s("%d", &NC);*/
    if (R == 'Y' || R=='y')
      R1 = 1;
    else
      R1 = -1;

    printf("\n INPUT COEFFICIENTS OF ECONOMIC FUNCTION:\n");
    for (J = 1; J<=NV; J++) 
    {
      printf("       x%d ?: ", J); 
      scanf_s("%lf", &R2);
      TS[1][J+1] = R2 * R1;
    }
    printf("       Right hand side ? "); 
    scanf_s("%lf", &R2);
    TS[1][1] = R2 * R1;
    for (I = 1; I<=NC; I++) 
    {
        printf("\n CONSTRAINT #%d:\n", I);
      for (J = 1; J<=NV; J++) 
      {
        printf("       x%d ?: ", J); 
        scanf_s("%lf", &R2);
        TS[I + 1][J + 1] = -R2;
      }
      printf("       Right hand side ?: "); 
      scanf_s("%lf", &TS[I+1][1]);
      }
    for (I = 1; I<=NC1; I++) 
    {
        printf("\n CONSTRAINT #%d:\n", I);
      for (J = 1; J<=NV; J++) 
      {
        printf("       x%d ?: ", J); 
        scanf_s("%lf", &R2);
        TS[I + 1][J + 1] = -R2;
      }
      printf("       Right hand side ?: "); 
      scanf_s("%lf", &TS[I+1][1]);
      }
    for (I = 1; I<=NC2; I++) 
    {
        printf("\n CONSTRAINT #%d:\n", I);
      for (J = 1; J<=NV; J++) 
      {
        printf("       x%d ?: ", J); 
        scanf_s("%lf", &R2);
        TS[I + 1][J + 1] = -R2;
      }
      printf("       Right hand side ?: "); 
      scanf_s("%lf", &TS[I+1][1]);
    }
    printf("\n\n RESULTS:\n\n");
    for(J=1; J<=NV; J++)
        TS[0][J+1] = J;
    for(I=NV+1; I<=NV+NC; I++)  
        TS[I-NV+1][0] = I;
  }

  void Pivot();
  void Formula();
  void Optimize();
  //void Branch_and_Bound();

  void Simplex() 
  {
e10: Pivot();
     Formula();
     Optimize();
     //Branch_and_Bound();
     if (NOPTIMAL == 1) goto e10;
  }

  void Pivot() 
  {

    double RAP,V,XMAX;
    int I,J;

    XMAX = 0;
    for(J=2; J<=NV+1; J++) 
    {
    if (TS[1][J] > 0 && TS[1][J] > XMAX) 
      {
        XMAX = TS[1][J];
        P2 = J;
      }
    }
    RAP = 999999;
    for (I=2; I<=NC+1; I++) 
    {
      if (TS[I][P2] >= 0) 
          goto e10;
      V = fabs(TS[I][1] / TS[I][P2]);
      if (V < RAP) 
      {
        RAP = V;
        P1 = I;
      }
e10:;
    }
    V = TS[0][P2]; 
    TS[0][P2] = TS[P1][0]; 
    TS[P1][0] = V;
  }

  void Formula() 
  {;
    //Labels: e60,e70,e100,e110;
    int I,J;

    for (I=1; I<=NC+1; I++) 
    {
      if (I == P1) goto e70;
      for (J=1; J<=NV+1; J++) 
      {
        if (J == P2) goto e60;
        TS[I][J] -= TS[P1][J] * TS[I][P2] / TS[P1][P2];
e60:;
      }
e70:;
    }
    TS[P1][P2] = 1 / TS[P1][P2];
    for (J=1; J<=NV+1; J++)
    {
      if (J == P2) goto e100;
      TS[P1][J] *=fabs(TS[P1][P2]);
e100:;
    }
    for (I=1; I<=NC+1; I++) 
    {
      if (I == P1) goto e110;
      TS[I][P2] *= TS[P1][P2];
e110:;
    }
  }   

  void Optimize() 
  {
    int I,J;
    for (I=2; I<=NC+1; I++)

      if (TS[I][1] < 0)  XERR = 1;
    NOPTIMAL = 0;
    if (XERR == 1)  return;
    for (J=2; J<=NV+1; J++)
      if (TS[1][J] > 0)  NOPTIMAL = 1;
  }
  /*void Branch_and_Bound()
  {
      int I,J;
 for (I=1; I<=NC+1; I++) 
    {
      if (I == P1) goto e70;
      for (J=1; J<=NV+1; J++) 
      {
        if (J == P2) goto e60;
        TS[I][J] -= TS[P1][J] * TS[I][P2] / TS[P1][P2];
e60:;
      }
e70:;
    }
    TS[P1][P2] = 1 / TS[P1][P2];
    for (J=1; J<=NV+1; J++)
    {
      if (J == P2) goto e100;
      TS[P1][J] *=fabs(TS[P1][P2]);
e100:;
    }
    for (I=1; I<=NC+1; I++) 
    {
      if (I == P1) goto e110;
      TS[I][P2] *= TS[P1][P2];
e110:;
    }
  } */ 

  void Results() 
  {
    //Labels: e30,e70,e100;
    int I,J;

    if (XERR == 0) goto e30;
    printf(" NO SOLUTION.\n"); goto e100;
e30:for (I=1; I<=NV; I++)
    for (J=2; J<=NC+1; J++) 
    {
      if (TS[J][0] != 1*I) goto e70;
      printf("       VARIABLE x%d: %f\n", I, TS[J][1]);
e70:  ;
    }
    printf("\n       ECONOMIC FUNCTION: %f\n", TS[1][1]);
e100:;printf("\n");
  }


void main()  
{

  Data();
  Simplex();
  //Branch_and_Bound();
  Results(); 
}

Now I have to make some change in implementation. In step 3, if the only other change in the normal branch and bound method is at step 3, once the variable with xj with the greatest fractional part has been determined, the two new constraints developed from this variable are xj=0 and xj=1. These two new constraint will form the two branches at each node.

SebastianK
  • 3,582
  • 3
  • 30
  • 48

0 Answers0