Ramon Béjar 3 months ago
parent
commit
9b1e1bda48

+ 65
- 0
data/2024-Algorithms/src/bestbippol.zpl View File

1
+#param vfile := "ins1-v.txt";
2
+#param efile := "ins1-e.txt";
3
+
4
+# set V := { "a1", "a2", "a3", "a4" };
5
+set V := { read vfile as "<1s>"  };
6
+param vsize := card(V);
7
+do print "Num vertices: ",vsize;
8
+
9
+do print V;
10
+set Edges := { read efile as "<1s,2s>"  };
11
+do print Edges;
12
+param esize := card(Edges);
13
+do print "Num non-zero edges: ",esize;
14
+
15
+
16
+# param vcost[V] := <"a1"> 2, <"a2"> 4, <"a3"> -5 , <"a4"> -3;
17
+param vcost[V] := read vfile as "<1s> 2n";
18
+do forall <v> in V do print v, " -> ", vcost[v];
19
+
20
+#param ecost[Edges] := <"a1","a1"> 0, <"a1","a2"> 0, <"a1","a3"> 2, <"a1","a4"> 0,
21
+#                      <"a2","a1"> 0, <"a2","a2"> 0, <"a2","a3"> 2, <"a2","a4"> 0,
22
+#                      <"a3","a1"> 2, <"a3","a2"> 0, <"a3","a3"> 0, <"a3","a4"> 0,
23
+#                      <"a4","a1"> 0, <"a4","a2"> 0, <"a4","a3"> 0, <"a4","a4"> 0;
24
+param ecost[Edges] := read efile as "<1s,2s> 3n";
25
+do forall <v1,v2> in Edges do print v1,v2," -> ",ecost[v1,v2];
26
+
27
+set Vpos := {  <v> in V with vcost[v] > 0 };
28
+set Vneg := {  <v> in V with vcost[v] <= 0 };
29
+
30
+#do print "Vpos: ";
31
+#do print Vpos;
32
+#do print "Vneg: ";
33
+#do print Vneg;
34
+#do forall <v> in V do print "Cost for: ", v, " is: ",vcost[v];
35
+#do print Vneg*Vpos;
36
+#do print ((-1)*vcost["a3"]*vcost["a1"])*(1.0-(-1))*(1.0+1)/4.0;
37
+#do print sum<i,j> in Vneg*Vpos : ((-1)*vcost[i]*vcost[j]);
38
+
39
+var x[V] integer >= -1 <= 1;
40
+var dummy integer >=0  <= 1;
41
+
42
+subto polarized:  forall <v> in V: x[v]^2 == 1;
43
+subto fixed: dummy == 1;
44
+
45
+# double quadratic model 
46
+maximize cost: 
47
+(sum <i,j> in Vneg*Vpos :  (((-1.0)*vcost[i]*vcost[j])*(dummy-x[i])*(dummy+x[j])/4.0)) / (1.0*vsize*vsize) *
48
+  ((sum <i,j> in Edges : ecost[i,j]*(dummy-(x[i]*x[j]))/2.0)/(esize) + (dummy*2.0) );
49
+
50
+
51
+
52
+# maximize cost:  (dummy-(x["a3"]*x["a1"]));
53
+
54
+# optimal:
55
+# -(-5)*2 + -(-5)*4 + -(-3)*2 + -(-3)*4 = 10 + 20 + 6 + 12 = 36+12 = 48
56
+# 48 * (2+2+2) = 288
57
+
58
+
59
+# Normalized values
60
+#
61
+#  -(-1)*1 * (1-(-1))*(1+1)/4 + -(-1)*1 * (1-(-1))*(1+1)/4 + -(-1)*1 * (1-(-1))*(1+1)/4 + -(-1)*1 * (1-(-1))*(1+1)/4  = 1+1+1+1 = 4     y 4/(4*4) = 1/4  
62
+
63
+# edges:   -(-2)*3 = 6   y  6 / 3 = 2    2 + 2 = 4 
64
+
65
+

+ 4
- 0
data/2024-Algorithms/src/solve_bippol.sh View File

1
+#!/bin/bash
2
+zimpl bestbippol.zpl -t pip -D vfile=$1 -D efile=$2
3
+scip -c "set limits time 600  read bestbippol.pip   optimize display solution quit"
4
+

Powered by TurnKey Linux.