Untitled

 avatar
unknown
plain_text
3 years ago
2.7 kB
5
Indexable
  using LinearAlgebra
function rijesi_simplex(a,b,c)
  d=[]
  if(size(c)[2]==size(a)[2]) 
    baza=zeros(1,size(b)[2])
    for i in 1:(size(b)[2])
      baza[i]=i+size(c)[2]
    end
    for i in 1:size(b)[2]
        t=b[i]
        for j in 1:size(a)[2]
        t=[t a[i,j]]
        end
        if(isempty(d))
          d=t;
        else
        d=[d;t]
        end
    end
    d=[d;0 c]
    r=Matrix(1.0I,size(b)[2],size(b)[2])
    t1=zeros(1,size(b)[2])
    t=[r; t1]
    d=[d t]
      while true
        display(d)
          w=d[size(d)[1],2];
          er=2;
          for p in 2:size(d)[2]
            if(w<d[size(d)[1],p])
              w=d[size(d)[1],p]
              er=p;
            end
          end
          print("er=");
          println(er)
          #w je najveci element
          #er je kolona tog elementa
          if(w<=0)
            break;
          end
          #pivotiranje
        #provjera parametara
        qw=1
          for p in 1:size(d)[1]
            if(d[p,er]>0)
              qw=0
            end
          end
          if(qw==1)
            return "rjesenje je neograniceno"
          end
          #djenljenje za mini
          op=1
          op1=d[1,1]/d[1,er]
          print("op1=")
          println(op1)
          for p in 2:size(d)[1]-1
            if(d[p,er]>0)
              if(op1>d[p,1]/d[p,er])
                print("pp=")
                println(p)
                op1=d[1,p]/d[p,er]
                op=p
                print("op2=")
                println(op1)
              end
            end
          end  
          print("P=")
          println(op)
          #imamo pivot elemtn na er,op
          baza[op]=er-1
          #izmjena reda sa pivotom
          d1=copy(d)
          for i in 1:size(d)[2]
            d[op,i]=d[op,i]/d1[op,er]
          end
          for i in 1:size(d)[1]
            if(i!=op&&d[i,er]!=0)
              for j in 1:size(d)[2]
                  d[i,j]=d[i,j]-d[op,j]*d1[i,er]
              end
            end
          end   
        end
        #ispis z
        z=zeros(1,size(c)[2])
        z=[z -d[size(d)[1],1]]
        for i in 1:size(c)[2]
          if(findfirst(x->x==i,baza)!=nothing)
            z[i]=d[Int(baza[1,findfirst(x->x==i,baza)[2]]),1]
          end
        end
        display(baza)
        return z
  else
    return "neispravni parametri";
  end
end

a=[30 16; 14 19; 11 26; 0 1]
size(a)[2]
b=[22800 14100 15959 550]
c=[800 1000]
rijesi_simplex(a,b,c) 
size(c)[2]
size(a)[2]
a=[0.5 0.3; 0.1 0.2]
b=[150 60]
c=[3 1]
rijesi_simplex(a,b,c)

Editor is loading...