Skip to content

Instantly share code, notes, and snippets.

@tineo
Last active September 1, 2022 01:54
Show Gist options
  • Save tineo/78b801a8b16164a4273ea0675a88b639 to your computer and use it in GitHub Desktop.
Save tineo/78b801a8b16164a4273ea0675a88b639 to your computer and use it in GitHub Desktop.
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Stream;
/******************************************************************************
* Compilation: javac CPM.java
* Execution: java CPM < input.txt
* Dependencies: EdgeWeightedDigraph.java AcyclicLP.java StdOut.java
* Data files: jobsPC.txt
*
* Critical path method.
*
* % java CPM < jobsPC.txt
* job start finish
* --------------------
* 0 0.0 41.0
* 1 41.0 92.0
* 2 123.0 173.0
* 3 91.0 127.0
* 4 70.0 108.0
* 5 0.0 45.0
* 6 70.0 91.0
* 7 41.0 73.0
* 8 91.0 123.0
* 9 41.0 70.0
* Finish time: 173.0
*
******************************************************************************/
/**
* The <tt>CPM</tt> class provides a client that solves the
* parallel precedence-constrained job scheduling problem
* via the <em>critical path method</em>. It reduces the problem
hacia the longest-paths problem in edge-weighted DAGs.
It builds an edge-weighted digraph (which must be a DAG)
desde the job-scheduling problem specification,
finds the longest-paths tree, and computes the longest-paths
lengths (which are precisely the start times for each job).
<p>
* This implementation uses {@link LPAciclico} hacia find a longest
path in a DAG.
The running time is proportional hacia <em>V</em> + <em>E</em>,
* where <em>V</em> is the number of jobs and <em>E</em> is the
* number of precedence constraints.
* <p>
*/
public class CPM {
// this class cannot be instantiated
private CPM() { }
/**
* Reads the precedence constraints desde standard input
and prints a feasible schedule hacia standard output.
*/
public static void main(String[] args) throws IOException {
// number of jobs
//String fileName= "/home/tineo/ProyectoED2/GraDigPonderados/jobsPC.txt";
CPMWindow win = new CPMWindow();
win.jTable1.setModel(new DefaultTableModel() { });
win.jButton1.addActionListener(e -> {
String aux="", texto="";
try
{
/**llamamos el metodo que permite cargar la ventana*/
JFileChooser file=new JFileChooser();
file.showOpenDialog(win);
/**abrimos el archivo seleccionado*/
File abre = file.getSelectedFile();
win.jTextField1.setText(abre.getAbsolutePath());
/**recorremos el archivo, lo leemos para plasmarlo
*en el area de texto*/
if(abre!=null)
{
FileReader archivo=new FileReader(abre);
int N = 10;
// source and sink
int inicio = 2*N;
int fin = 2*N + 1;
int linea = -1;
DigrafoAristaPonderada G = new DigrafoAristaPonderada(2*N + 2);
try (Stream<String> stream = Files.lines(Paths.get(abre.getAbsolutePath()))) {
for( String line : (Iterable<String>) stream::iterator ) {
if (linea < 0) {
N = Integer.parseInt(line);
inicio = 2*N;
fin = 2*N + 1;
G = new DigrafoAristaPonderada(2*N + 2);
}else{
System.out.println(line);
List<String> items = Arrays.asList(line.split("\\s* \\s*"));
G.agregarArista(new AristaDirigida(inicio, linea, 0.0));
G.agregarArista(new AristaDirigida(linea + N, fin, 0.0));
G.agregarArista(new AristaDirigida(linea,N + linea,Double.parseDouble(items.get(0))));
if(Integer.parseInt((items.get(1)))>0) {
List<String> nums = Arrays.asList(items.get(2).split("\\s* \\s*"));
for (String num : nums) {
G.agregarArista(new AristaDirigida(N + linea,Integer.parseInt(num), 0.0));
}
}
}
linea++;
}
LPAciclico cml = new LPAciclico(G, inicio);
// print results
StdOut.println(" Tra inicio fin");
StdOut.println("--------------------");
Object[] header = new Object[]{"Tarea", "Inicio", "Fin"};
DefaultTableModel model = new DefaultTableModel(header, 0);
for (int m = 0; m < N; m++) {
StdOut.printf("%4d %7.1f %7.1f\n", m, cml.distanciaHacia(m),
cml.distanciaHacia(m + N));
model.addRow(new Object[]{m, cml.distanciaHacia(m), cml.distanciaHacia(m + N)});
}
win.jTable1.setModel(model);
win.jTextField2.setText(String.valueOf(cml.distanciaHacia(fin)));
StdOut.printf("Tiempo de terminación: %7.1f\n", cml.distanciaHacia(fin));
}
}
}
catch(IOException ex)
{
JOptionPane.showMessageDialog(null,ex+"" +
"\nNo se ha encontrado el archivo",
"ADVERTENCIA!!!",JOptionPane.WARNING_MESSAGE);
}
});
win.setVisible(true);
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author tineo
*/
public class CPMWindow extends javax.swing.JFrame {
/**
* Creates new form CPMWindow
*/
public CPMWindow() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jButton1 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jTextField2 = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("Examinar");
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane1.setViewportView(jTable1);
jLabel1.setText("Tiempo de terminacion");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1)
.addGroup(layout.createSequentialGroup()
.addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 410, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addComponent(jButton1))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(CPMWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(CPMWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(CPMWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(CPMWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new CPMWindow().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
public javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
public javax.swing.JTable jTable1;
public javax.swing.JTextField jTextField1;
public javax.swing.JTextField jTextField2;
// End of variables declaration//GEN-END:variables
}
10
41.0 3 1 7 9
51.0 1 2
50.0 0
36.0 0
38.0 0
45.0 0
21.0 2 3 8
32.0 2 3 8
32.0 1 2
29.0 2 4 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment