Skip to content

Commit ddb09fb

Browse files
committed
first conection working
1 parent 35aac4b commit ddb09fb

File tree

8 files changed

+238
-36
lines changed

8 files changed

+238
-36
lines changed

back/DesenfrenoDePasiones.mzn

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
% Encabezado
2+
% Restricciones Naive Model
3+
% Jaime Cuartas Granada 1632664
4+
% Emily Esmeralda Carvajal Camelo 1630436
5+
% Desenfreno de Pasiones
6+
include "globals.mzn";
7+
enum ACTORES;
8+
array[ACTORES, int] of int: Escenas;
9+
int: nEscenas=length(row(Escenas,1))-1;
10+
array[1..nEscenas] of int: Duracion;
11+
array[int,1..2] of int: Disponibilidad;
12+
array[int,1..2] of ACTORES: Evitar;
13+
14+
% asserts
15+
16+
constraint assert(length(ACTORES) = length(col(Escenas,1)),
17+
"Cantidad invalida de escenas o actores, no coincide");
18+
constraint assert(length(row(Escenas,1))-1 = length(Duracion),
19+
"Cantidad invalida de escenas o duracion, no coincide");
20+
21+
constraint assert(forall(d in Duracion)(d > 0),
22+
"Valores de duracion invalidos");
23+
24+
constraint assert(forall(i in index_set(Duracion))
25+
(forall (j in col(Escenas,i))(j = 0 \/ j = 1)),
26+
"Algun valor es invalido en la matriz Escenas");
27+
28+
%faltan asserts de disponibilidad y evitar%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
29+
30+
% Dado un actor retorna el costo del actor por las escenas en que participa
31+
array[ACTORES] of int: Costo = [Escenas[x,nEscenas+1] | x in ACTORES];
32+
% Dado un actor retorna las escenas en que esta
33+
array[ACTORES] of set of int: EscenaDe = [{y | y in 1..nEscenas where Escenas[x,y]=1} | x in ACTORES];
34+
35+
% Variable
36+
array[1..nEscenas] of var 1..nEscenas: PosicionDeEscena;
37+
38+
% Suma los costos de cada actor por las escenas en que esta
39+
var int : costo = (sum(j in ACTORES)(
40+
let {
41+
var int: primera=min([PosicionDeEscena[e] | e in EscenaDe[j]]);
42+
var int: ultima=max([PosicionDeEscena[e] | e in EscenaDe[j]]);
43+
}
44+
in
45+
(sum(i in 1..nEscenas)
46+
(if PosicionDeEscena[i] >= primera
47+
/\
48+
PosicionDeEscena[i] <= ultima
49+
then Duracion[i]
50+
else 0
51+
endif)) * Costo[j]));
52+
53+
% Dado un actor suma los costos por las escenas en que esta
54+
array[ACTORES] of var int : TiempoDe = [(
55+
let {
56+
var int: primera = min([PosicionDeEscena[e] | e in EscenaDe[j]]);
57+
var int: ultima = max([PosicionDeEscena[e] | e in EscenaDe[j]]);
58+
}
59+
in
60+
(sum(i in 1..nEscenas)
61+
(if PosicionDeEscena[i] >= primera
62+
/\
63+
PosicionDeEscena[i] <= ultima
64+
then Duracion[i]
65+
else 0
66+
endif))) | j in ACTORES];
67+
68+
%Suma la duracion de las escenas en que dos actores que estan en la lista evitar se encuentran en el mismo momento en el set
69+
var int: evitar = (sum(j in 1..length(col(Evitar,1)))(
70+
let {
71+
set of int: a = EscenaDe[row(Evitar,j)[1]];
72+
set of int: b = EscenaDe[row(Evitar,j)[2]];
73+
var int: primera = (if sum(a intersect b) != 0
74+
then min([PosicionDeEscena[e] | e in a intersect b])
75+
else 0
76+
endif);
77+
var int: ultima = (if primera != 0
78+
then min([max([PosicionDeEscena[e] | e in a]),
79+
max([PosicionDeEscena[e] | e in b])])
80+
else -1
81+
endif);
82+
}
83+
in
84+
(sum(i in 1..nEscenas)
85+
(if PosicionDeEscena[i] >= primera
86+
/\
87+
PosicionDeEscena[i] <= ultima
88+
then Duracion[i]
89+
else 0
90+
endif))));
91+
92+
% Restricciones
93+
94+
constraint all_different(PosicionDeEscena);
95+
96+
constraint forall(i in 1..length(col(Disponibilidad,1)))
97+
(if row(Disponibilidad,i)[2] = 0
98+
then TiempoDe[ACTORES[row(Disponibilidad,i)[1]]] > row(Disponibilidad,i)[2]
99+
else TiempoDe[ACTORES[row(Disponibilidad,i)[1]]] <= row(Disponibilidad,i)[2]
100+
endif);
101+
102+
103+
%solve satisfy;
104+
105+
solve minimize costo;
106+
107+
%salida
108+
109+
output["posicion(\(PosicionDeEscena[p])):\(p) " | p in 1..length(PosicionDeEscena)]++["\n"]
110+
++["Tiempo compartido: \(evitar)"]++["\n"]
111+
++["Costo: \(costo) \n"];
112+
%output["Costo: \(costo) \n"];

back/api.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!flask/bin/python
2+
from flask_cors import CORS, cross_origin
3+
import os
4+
from flask import Flask, flash, request, redirect, url_for, jsonify
5+
from werkzeug.utils import secure_filename
6+
from os import system, getcwd
7+
import subprocess
8+
9+
ALLOWED_EXTENSIONS = {'dzn'}
10+
11+
app = Flask(__name__)
12+
cors = CORS(app)
13+
app.config['CORS_HEADERS'] = 'Content-Type'
14+
15+
def allowed_file(filename):
16+
return '.' in filename and \
17+
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
18+
19+
@app.route('/dzn', methods=['POST'])
20+
@cross_origin()
21+
def upload_file():
22+
result = "nada"
23+
target = os.path.join(getcwd(), 'upload')
24+
dznfile = request.files['dznfile']
25+
filename = secure_filename(dznfile.filename)
26+
dznfile.save(os.path.join(target, 'temp.dzn'))
27+
command = "/home/esmeralda/Desktop/MiniZincIDE-2.3.2-bundle-linux-x86_64/MiniZincIDE-2.3.2-bundle-linux/bin/minizinc --solver Gecode /home/esmeralda/Desktop/DesenfrenoPasiones/minizinc/DesenfrenoDePasiones.mzn " + os.path.join(target, 'temp.dzn') + '>' + os.path.join(target, 'output.txt')
28+
result = subprocess.Popen(command, shell=True)
29+
f = open(os.path.join(target, 'output.txt'))
30+
result = f.read()
31+
return result
32+
33+
if __name__ == '__main__':
34+
app.run(debug=True)

back/upload/output.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
posicion(8):1 posicion(3):2 posicion(6):3 posicion(4):4 posicion(9):5 posicion(7):6 posicion(2):7 posicion(1):8 posicion(5):9
2+
Tiempo compartido: 11
3+
Costo: 520
4+
----------
5+
==========

back/upload/temp.dzn

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
ACTORES = {Actor1, Actor2, Actor3, Actor4, Actor5} ;
2+
3+
Escenas = [| 0,1,0,0,0,0,1,0,0,10
4+
| 0,1,1,1,0,1,1,1,1,20
5+
| 1,1,1,1,0,1,0,0,1,5
6+
| 0,1,1,1,0,0,1,1,1,5
7+
| 1,0,0,0,1,1,0,0,0,15|];
8+
9+
Duracion = [2,1,1,1,3,4,2,3,1];
10+
11+
12+
Disponibilidad =[|Actor1, 13
13+
|Actor2, 14
14+
|Actor3, 15
15+
|Actor4, 14
16+
|Actor5, 10|];
17+
18+
Evitar =[|Actor1, Actor2
19+
|Actor2, Actor3
20+
|Actor4, Actor5|];
21+
22+
%Ejecución con Gecode
23+
24+
% Orden de las Escenas: [5, 1, 6, 9, 4, 3, 2, 7, 8]
25+
% Costo: 520
26+
% Tiempo Compartido: 11
27+
% ----------
28+
% ==========
29+
% Finished in 31s 398msec

front-minizinc/package-lock.json

+31
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

front-minizinc/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"@testing-library/jest-dom": "^4.2.4",
77
"@testing-library/react": "^9.5.0",
88
"@testing-library/user-event": "^7.2.1",
9+
"axios": "^0.19.2",
910
"bootstrap": "^4.4.1",
1011
"react": "^16.13.1",
1112
"react-bootstrap": "^1.0.1",

front-minizinc/public/logo192.png

5.22 KB
Loading

front-minizinc/src/App.js

+26-36
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22
import './App.css';
3-
import { Modal, Button, Form, CardDeck, Card } from 'react-bootstrap';
3+
import { Modal, Button,CardDeck, Card } from 'react-bootstrap';
4+
import axios from 'axios';
45

56
const backColor = {
67
backgroundColor: 'Transparent',
@@ -9,36 +10,28 @@ const backColor = {
910
class App extends React.Component {
1011
constructor(props) {
1112
super(props);
12-
this.state = {
13+
this.state = {
1314
r: '',
14-
dznfile: '',
15+
dznfile: null,
1516
}
1617
this.dznfile = this.dznfile.bind(this);
1718
this.execute = this.execute.bind(this);
1819
}
1920
dznfile = (event) => {
2021
event.preventDefault();
21-
let files = event.target.files;
22-
if (files && files.length > 0) {
23-
this.setState({ dznfile: files });
24-
}
25-
else
26-
console.log("no hay archivo");
22+
let file = event.target.files[0];
23+
this.setState({ 'dznfile' : file})
2724
}
2825
execute = () => {
29-
//../../../MiniZincIDE-2.3.2-bundle-linux-x86_64/MiniZincIDE-2.3.2-bundle-linux/bin/minizinc --solver Gecode /home/esmeralda/Desktop/DesenfrenoPasiones/minizinc/DesenfrenoDePasiones.mzn /home/esmeralda/Desktop/DesenfrenoPasiones/minizinc/Trivial2.dzn
30-
31-
//var sh = require('shelljs');
32-
//var path = require('path');
33-
//sh.config.execPath = path.join('home', 'esmeralda', 'Desktop', 'DesenfrenoPasiones', 'front-minizinc', 'src');
34-
//var output = sh.exec('ls');
35-
//console.log(sh.exec("ls"));
36-
// if (sh.exec('ls').code !== 0) {
37-
// sh.echo('Error: ls failed');
38-
// sh.exit(1);
39-
// }
40-
//var version = sh.exec('ls -a', {silent:true}).stdout;
41-
this.setState({ r: 'funciona...' });
26+
const data = new FormData();
27+
data.append('dznfile', this.state.dznfile);
28+
axios({
29+
url: 'http://localhost:5000/dzn',
30+
method: 'POST',
31+
data: data,
32+
config: { headers: {'Content-Type':'multipart/form-data'}}
33+
}).then( ( response ) => this.setState({r: response.data})
34+
).catch( ( error ) => console.log(error))
4235
}
4336
render() {
4437
return (
@@ -53,22 +46,19 @@ class App extends React.Component {
5346
</Modal.Title>
5447
</Modal.Header>
5548
<Modal.Body>
56-
<CardDeck>
57-
<Card>
58-
Carga tu archivo de dzn
59-
<Form.Group>
60-
<Form.Label>Archivo dzn</Form.Label>
61-
<Form.Control name="dzn[]" type="file" multiple onChange={this.dznfile} />
62-
</Form.Group>
63-
<Button onClick={this.execute} variant="primary">
64-
Enviar
49+
<CardDeck>
50+
<Card>
51+
<h5>Carga tu archivo de dzn</h5>
52+
<input name="dzn" type="file" onChange={this.dznfile} />
53+
<Button onClick={this.execute} variant="primary">
54+
Enviar
6555
</Button>
66-
</Card>
67-
<Card>
68-
Respuesta
56+
</Card>
57+
<Card>
58+
Respuesta
6959
{this.state.r}
70-
</Card>
71-
</CardDeck>
60+
</Card>
61+
</CardDeck>
7262
</Modal.Body>
7363
<Modal.Footer>
7464
<Button href='/' variant='danger'> Limpiar </Button>

0 commit comments

Comments
 (0)