• Jueves 28 de Marzo de 2024, 20:05

Autor Tema:  metodo simplex en java  (Leído 17772 veces)

daviid

  • Nuevo Miembro
  • *
  • Mensajes: 3
  • Nacionalidad: mx
    • Ver Perfil
metodo simplex en java
« en: Martes 18 de Octubre de 2011, 03:49 »
0
 :ayuda:hola soy novato aqui y necesito de su pronta ayuda con un codigo fuente que tengo es sobre el "METODO SIMPLEX"es de suma urgencia....




SE LES AGRADECE SU PRONTA RESPUESTA xD

p.d.:
[el problema es exactamente en el applet no corre como debe de ser]


Código: Java(TM) 2 Platform Standard Edition 5.0
  1. import java.util.*;
  2. import java.awt.*;
  3. import java.applet.*;
  4.  
  5. class RN {
  6.         int     n;
  7.         int     d;
  8.  
  9.         RN() {n=0; d=1;}
  10.         RN(int n) {this.n=n; this.d=1;}
  11.         RN(int n, int d) {this.n=n; this.d=d; reduce();}
  12.         RN(String s) {
  13.                 int k = s.indexOf('/');
  14.                 if (k>0) {
  15.                         d = Integer.valueOf(s.substring(k+1)).intValue();
  16.                         s = s.substring(0,k);
  17.                 } else
  18.                         d = 1;
  19.                 n = Integer.valueOf(s).intValue();
  20.                 reduce();
  21.         }
  22.  
  23.         int euclid(int a, int b) {
  24.                 int     q,r;
  25.  
  26.                 if (a < 0) a = -a;
  27.                 if (b < 0) b = -b;
  28.                 if (b == 0)
  29.                         if (a==0)
  30.                                 return -1;
  31.                         else
  32.                                 return a;
  33.                 for (;;) {
  34.                         q = a / b;
  35.                         r = a % b;
  36.                         if (r==0)
  37.                                 break;
  38.                         a = b;
  39.                         b = r;
  40.                 }
  41.                 return b;
  42.         }
  43.  
  44.         boolean reduce() {
  45.                 int     c;
  46.  
  47.                 if ((c = euclid(n,d))<0)
  48.                         return false;
  49.                 if (d<0)
  50.                         c *= -1;
  51.                 n /= c;
  52.                 d /= c;
  53.                 return true;
  54.         }
  55.  
  56.         void set(int n) {this.n=n; this.d=1;}
  57.         void set(int n, int d) {this.n=n; this.d=d;}
  58.         void set(RN a) {n=a.n; d=a.d;}
  59.  
  60.         void mul(RN a) {
  61.                 a.reduce();
  62.                 RN aa = new RN(n,a.d);
  63.                 RN bb = new RN(a.n,d);
  64.                 aa.reduce();
  65.                 bb.reduce();
  66.                 n = aa.n * bb.n;
  67.                 d = aa.d * bb.d;
  68.         }
  69.  
  70.         void div(RN a) {
  71.                 a.reduce();
  72.                 RN aa = new RN(n, a.n);
  73.                 RN bb = new RN(a.d, d);
  74.                 aa.reduce();
  75.                 bb.reduce();
  76.                 n = aa.n * bb.n;
  77.                 d = aa.d * bb.d;
  78.         }
  79.  
  80.         void inv() {int x; x = n; n = d; d = x; reduce();}
  81.  
  82.         boolean plus(RN a) {
  83.                 int     c,x,y;
  84.  
  85.                 c = euclid(d, a.d);
  86.                 if (c < 0)
  87.                         return false;
  88.                 if ((x = a.d/c*n + d/c*a.n)==0) {
  89.                         x = 0;
  90.                         y = 1;
  91.                 } else
  92.                         y = d/c*a.d;
  93.                 n = x;
  94.                 d = y;
  95.                 this.reduce();
  96.                 return true;
  97.         }
  98.  
  99.         boolean minus(RN a) {
  100.                 int     c,x,y;
  101.  
  102.                 c = euclid(d, a.d);
  103.                 if (c < 0)
  104.                         return false;
  105.                 if ((x = a.d/c*n - d/c*a.n)==0) {
  106.                         x = 0;
  107.                         y = 1;
  108.                 } else
  109.                         y = d/c*a.d;
  110.                 n = x;
  111.                 d = y;
  112.                 this.reduce();
  113.                 return true;
  114.         }
  115.  
  116.         boolean gt(RN a) {RN c=new RN(n,d); c.minus(a); return c.n>0;}
  117.         boolean ge(RN a) {RN c=new RN(n,d); c.minus(a); return c.n>=0;}
  118.         boolean eq(RN a) {RN c=new RN(n,d); c.minus(a); return c.n==0;}
  119.         boolean le(RN a) {RN c=new RN(n,d); c.minus(a); return c.n<=0;}
  120.         boolean lt(RN a) {RN c=new RN(n,d); c.minus(a); return c.n<0;}
  121. }
  122.  
  123. public class Simplex extends Applet {
  124.         int     m,n,r,s;
  125.         int     step,cycle;
  126.         RN[][]  a = new RN[10][20];
  127.         int[]   base = new int[10];
  128.         String  message = "";
  129.  
  130.         void Print(Graphics g, FontMetrics fm, int x, int y, String s) {
  131.                 int     w = fm.stringWidth(s);
  132.                 int     h = fm.getHeight();
  133.                 g.drawString(s,x-w/2,y-h/2+fm.getAscent());
  134.         }
  135.  
  136.         void Print(Graphics g, FontMetrics fm, int x, int y, int n) {
  137.                 Print(g,fm,x,y,""+n);
  138.         }
  139.  
  140.         void Print(Graphics g, FontMetrics fm, int x, int y, RN rn) {
  141.                 int     wn, wd, h = fm.getHeight();
  142.                 int     sign;
  143.  
  144.                 if (rn.n<0)
  145.                         sign = -1;
  146.                 else
  147.                         sign = 1;
  148.                 if (rn.d == 1) {
  149.                         Print(g,fm,x,y,sign*rn.n);
  150.                         wd = fm.stringWidth(""+(sign*rn.n));
  151.                 } else {
  152.                         Print(g,fm,x,y-h/2-2,sign*rn.n);
  153.                         Print(g,fm,x,y+h/2+2,rn.d);
  154.                         wn = fm.stringWidth(""+sign*rn.n);
  155.                         wd = fm.stringWidth(""+rn.d);
  156.                         if (wn>wd)
  157.                                 wd = wn;
  158.                         g.drawLine(x-wd/2,y-1,x+wd/2,y-1);
  159.                 }
  160.                 if (sign<0) {
  161.                         wn = wd/2+fm.stringWidth("-")+2;
  162.                         wd = fm.getHeight();
  163.                         g.drawString("-",x-wn,y-wd/2+fm.getAscent());
  164.                 }
  165.         }
  166.  
  167.         void input_data() {
  168.                 m = Integer.parseInt(getParameter("m"));
  169.                 n = Integer.parseInt(getParameter("n"));
  170.                 String sdat = getParameter("data");
  171.                 StringTokenizer st = new StringTokenizer(sdat,",");
  172.                 for (int i = 0; i<=m; i++) {
  173.                         for (int j=0; j<n; j++)
  174.                                 a[i][j] = new RN(st.nextToken());
  175.                         base[i] = n+i;
  176.                         for (int j=n; j<n+m; j++) {
  177.                                 RN rn = new RN(0);
  178.                                 if (j == i+n)
  179.                                         rn.set(1);
  180.                                 a[i][j] = rn;
  181.                         }
  182.                         a[i][n+m] = new RN(st.nextToken());
  183.                 }
  184.                 n += m;
  185.         }
  186.  
  187.         boolean step1() {               /* búsqueda de pivote s (r, s)*/
  188.                 RN      c = new RN();
  189.  
  190.                 s = 0; r = -1;
  191.                 c.set(a[m][s]);
  192.                 for (int j=1; j<n; j++)
  193.                         if (c.gt(a[m][j])) {
  194.                                 s = j;
  195.                                 c.set(a[m][s]);
  196.                         }
  197.                 if (c.n>=0) {
  198.                         s = -1;
  199.                         return true;
  200.                 } else
  201.                         return false;
  202.         }
  203.  
  204.         boolean step2() {               /* búsqueda de pivote r (r, s)*/
  205.                 RN      t = new RN();
  206.                 RN      c = new RN();
  207.  
  208.                 for (int i=0; i<m; i++) {
  209.                         if (a[i][s].n<=0)
  210.                                 continue;
  211.                         t.set(a[i][n]);
  212.                         t.div(a[i][s]);
  213.                         if (r<0 || t.lt(c)) {
  214.                                 r = i;
  215.                                 c.set(t);
  216.                         }
  217.                 }
  218.                 if (r<0)
  219.                         return true;
  220.                 else
  221.                         return false;
  222.         }
  223.  
  224.         void step3() {          /* pivote operacion 1 */
  225.                 RN      c = new RN();
  226.  
  227.                 base[r] = s;
  228.                 c.set(a[r][s]);
  229.                 for (int j=0; j<=n; j++)
  230.                         a[r][j].div(c);
  231.         }
  232.  
  233.         void step4() {
  234.                 RN      c = new RN();
  235.                 RN      t = new RN();
  236.  
  237.                 for (int i=0; i<=m; i++) {
  238.                         if (i == r)
  239.                                 continue;
  240.                         c.set(a[i][s]);
  241.                         for (int j=0; j<=n; j++) {
  242.                                 t.set(c);
  243.                                 t.mul(a[r][j]);
  244.                                 a[i][j].minus(t);
  245.                         }
  246.                 }
  247.                 r = s = -1;
  248.         }
  249.  
  250.         public void init() {
  251.                 input_data();
  252.                 step = cycle = 0;
  253.                 s = r = -1;
  254.                 message = "click aquí.";
  255.                 setBackground(Color.white);
  256.         }
  257.  
  258.         void select_color(Graphics g, int i, int j) {
  259.                 Color   c;
  260.  
  261.                 if ((step==1 && i==m && j==s)||(step!=1 && i==r && j==s))
  262.                         c = Color.red;
  263.                 else if ((step==1&&i==m&&j<n&&a[i][j].n<0)||
  264.                          (step==2&&(j==s||j==n)&&i<m&&a[i][s].n>0)||
  265.                          (step==3&&(i==r||j==s)))
  266.                         c = Color.blue;
  267.                 else
  268.                         c = Color.black;
  269.                 g.setColor(c);
  270.         }
  271.  
  272.         public void paint(Graphics g) {
  273.                 int     w,h;
  274.                 int     x,y;
  275.  
  276.                 FontMetrics fm = g.getFontMetrics();
  277.                 w = fm.stringWidth("012345")+10;
  278.                 h = fm.getHeight()*2+4;
  279.  
  280.                 x = 25 + w/2;
  281.                 y = 25 + h/2;
  282.                 g.setColor(getBackground());
  283.                 g.fillRect(x-w/2,y-h/2,(n+2)*w+1,h+1);
  284.                 g.setColor(Color.black);
  285.                 Print(g,fm,x,y,"basicas");
  286.                 for (int j=0; j<n; j++) {
  287.                         x += w;
  288.                         Print(g,fm,x,y,"x"+(j+1));
  289.                 }
  290.                 x += w;
  291.                 Print(g,fm,x,y,"constantes");
  292.                 for (int i=0; i<=m; i++) {
  293.                         x = 25 + w/2;
  294.                         y += h;
  295.                         g.setColor(getBackground());
  296.                         g.fillRect(x-w/2,y-h/2,(n+2)*w+1,h+1);
  297.                         g.setColor(Color.black);
  298.                         if (i==m)
  299.                                 Print(g,fm,x,y,"-z");
  300.                         else
  301.                                 Print(g,fm,x,y,"x"+(base[i]+1));
  302.                         for (int j=0; j<=n; j++) {
  303.                                 select_color(g,i,j);
  304.                                 x += w;
  305.                                 Print(g,fm,x,y,a[i][j]);
  306.                         }
  307.                 }
  308.                 g.setColor(getBackground());
  309.                 g.fillRect(25,25+(m+2)*h,300,(h-4)/2+1);
  310.                 g.fillRect(25,25-h/2,120,(h-4)/2+1);
  311.                 g.setColor(Color.black);
  312.                 g.drawString(message,25,25+(m+2)*h+fm.getAscent());
  313.                 g.drawString("Ciclo "+cycle,25,25-h/2+fm.getAscent());
  314.         }
  315.  
  316.         public void update(Graphics g) {
  317.                 paint(g);
  318.         }
  319.  
  320.         public boolean mouseDown(Event ev, int x, int y)
  321.         {
  322.                 boolean isOptimal = false;
  323.                 boolean isUnbounded = false;
  324.  
  325.                 switch (step) {
  326.                         case 0:
  327.                                 if (isOptimal=step1())
  328.                                         message = "Optimo";
  329.                                 else
  330.                                         message = "Seleciona x"+(s+1)+" para entrar en la base.";
  331.                                 break;
  332.                         case 1:
  333.                                 if (isUnbounded=step2())
  334.                                         message = "ilimitado";
  335.                                 else
  336.                                         message = "Seleciona x"+(base[r]+1)+" para salir de la base.";
  337.                                 break;
  338.                         case 2:
  339.                                 step3();
  340.                                 message = "Vuelva a colocar la tabla simplex.";
  341.                                 break;
  342.                         case 3:
  343.                                 step4();
  344.                                 message = "Repite el ciclo.";
  345.                                 cycle++;
  346.                                 break;
  347.                         case 4:
  348.                                 init();
  349.                                 message = "Click aquí";
  350.                                 repaint();
  351.                                 return true;
  352.                         default:
  353.                                 break;
  354.                 }
  355.                 step++;
  356.                 repaint();
  357.                 step %= 4;
  358.                 if (isOptimal||isUnbounded)
  359.                         step = 4;
  360.                 return true;
  361.         }
  362. }
« última modificación: Martes 18 de Octubre de 2011, 03:58 por daviid »
the.ghost

ramirovillagrana

  • Nuevo Miembro
  • *
  • Mensajes: 1
    • Ver Perfil
Re:metodo simplex en java
« Respuesta #1 en: Martes 18 de Octubre de 2011, 14:59 »
0
Isidro solo te queda estudiar para el metodo de la M y para el examen lo siento... :jumpie:


                                                              :kicking:

daviid

  • Nuevo Miembro
  • *
  • Mensajes: 3
  • Nacionalidad: mx
    • Ver Perfil
Re:metodo simplex en java
« Respuesta #2 en: Jueves 27 de Octubre de 2011, 02:10 »
0
 :yes:  hahaha :jumpie: pues si que mas pero aunque no me preocupa mucho  :think: porque si le entiendo bn a los dos temas y creo poder pasar el examen sin ningun problema......xD

 :suerte:

y de igual manera tu estudiale muy bn  :smartass:  ;)
the.ghost