-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEssaisSuccessifs.java
More file actions
260 lines (233 loc) · 4.37 KB
/
Copy pathEssaisSuccessifs.java
File metadata and controls
260 lines (233 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package project;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* @author katiènin
* @author brahim
*
*/
public class EssaisSuccessifs {
private int n;
private double c=1.5;
//solution optimale
private int solOpt[];
//vecteur solutioon
private int X[];
private int Imp[];
double somcour=0;
int dernierpris=1;
int nbpris=1;
static List<int []> solutions=new ArrayList<int []>();
//private int Xcour[];
double coutOpt=0;
private Set <Point> points = new HashSet<Point>();
private int nbAppel=0;
public EssaisSuccessifs (int _n) {
this.n=_n;
nbAppel=1;
points= Parser.recuperePoints();
initSolOpt();
initX();
}
/**
* initialise la solution optimale
*/
public void initSolOpt(){
Imp=new int[n];
for(int i=0;i<n;i++){
if(i%2==0){
Imp[i]=1;
}
}
solOpt=new int[n];
solOpt=Imp;
coutOpt=cout(Imp);
//coutOpt=distance(1,n)+c;
solOpt[0]=1;
solOpt[n-1]=1;
}
public void initX(){
X=new int[n];
for(int i=1;i<n;i++)
{
X[i]=0;
}
X[0]=1;
}
public double cout(int[] x){
double cout=0;
int drp=1;
int i=2;
while(i<=n){
if(x[i-1]==1){
cout=cout+distance(drp,i)+c;
drp=i;
}
i++;
}
return cout;
}
/**
* @param l'abscisse des points d'extrémités du segment
* @return la somme des distance des point dont les abscisse sont entre i,j de la ligne
* c=1.5 ici
*/
public double distance(double i ,double j){
Point p1=null,p2=null;
Ligne l;
double a,b,c,distance=0;
if(points.size()>1){
for(Point p : points){
if(p.getx()==i){
p1=p;
}
if(p.getx()==j){
p2=p;
}
}
if(p1!=null&&p2!=null){
a=p2.gety()-p1.gety();
b=j-i;
c=p2.gety()*b+i*a;
l=new Ligne(p1,p2);
for(Point p : points){
if(!l.appartient(p)){
if(l.horsLigne(p)){
distance=Math.abs(a*p.getx()+b*p.gety()+c)/Math.sqrt(a*a+b*b)+distance;
}
}
}
}
}
return distance;
}
void enregistrer(int i,int xi){
this.X[i-1]=xi;
if(xi==1){
somcour=somcour+distance(dernierpris,i);
dernierpris=i;
nbpris++;
}
}
public boolean solTrouvee(){
return((X[0]==1)&&(X[n-1]==1));
}
public void optimal(int[] x){
if(cout(x)<coutOpt){
coutOpt=cout(x);
solOpt=x;
}
}
//&&(c*(nbpris-1)+somcour<coutOpt)
public boolean optEncorePosssible(int i){
if(cout(X)<coutOpt){
return true;
}else{
return false;
}
}
boolean satisfaisant(int xi){
return X[0]==1;
}
public void defaire(int xi,int i){
if(xi==1){
int j=i-1;
while(j>1 && X[j-1]!=1){
j--;
}
somcour=somcour-distance(dernierpris,i);
dernierpris=j;
nbpris--;
X[i-1]=0;
}
}
public void appligbri(int i){
for(int xi=0;xi<2;xi++){
enregistrer(i,xi);
if (solTrouvee()){
solutions.add(X.clone());
optimal(X.clone());
}else{
if(i<n){
if(optEncorePosssible(i)){
nbAppel++;
appligbri(i+1);
}
}
}
defaire(xi,i);
}
}
/**
* @return the solutions
*/
public List<int[]> getSolutions() {
return solutions;
}
public void touteSol(int i){
for(int xi=0;xi<2;xi++){
enregistrer(i,xi);
afficheSols(X);
if (solTrouvee()){
solutions.add(X.clone());
optimal(X.clone());
}else{
if(i<n){
touteSol(i+1);
}
}
defaire(xi,i);
}
}
void afficheSols(int[] x){
for(int i=0;i<n;i++){
System.out.print(x[i]+" ");
}
System.out.println("");
}
public int[] getSolOpt() {
return solOpt;
}
public void setSolOpt(int[] solOpt) {
this.solOpt = solOpt;
}
public double getCoutOpt() {
return coutOpt;
}
public void setCoutOpt(double coutOpt) {
this.coutOpt = coutOpt;
}
/**
* construit la solution optimale
* @param sol : tableau contenant la solution optimale
*/
public void buildSolOpt(int[] sol){
System.out.print("La solution optimale est : ");
System.out.print("(");
for(int i=1;i<this.n;i++){
if (sol[i-1]==1){
System.out.print(i+", ");
}
}
System.out.print(n+")");
}
/**
* @return the nbAppel
*/
public int getNbAppel() {
return nbAppel;
}
/**
* @return the x : vecteur solution
*/
public int[] getX() {
return X;
}
public static void main(String[] args) {
EssaisSuccessifs test=new EssaisSuccessifs(Parser.recuperePoints().size());
test.appligbri(1);
test.buildSolOpt(test.getSolOpt());
}
}