Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
Branch-and-Bound
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
OUATTARA Sie
Branch-and-Bound
Commits
5400343c
Commit
5400343c
authored
5 months ago
by
OUATTARA Sie
Browse files
Options
Downloads
Patches
Plain Diff
correction de la methode solutionFractionnelle et explore_from
parent
2b35d353
Branches
Branches containing commit
No related tags found
1 merge request
!4
Dev1
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
out/production/aroTP2/App.class
+0
-0
0 additions, 0 deletions
out/production/aroTP2/App.class
src/App.java
+1
-1
1 addition, 1 deletion
src/App.java
src/Backpack.java
+22
-26
22 additions, 26 deletions
src/Backpack.java
with
23 additions
and
27 deletions
out/production/aroTP2/App.class
+
0
−
0
View file @
5400343c
No preview for this file type
This diff is collapsed.
Click to expand it.
src/App.java
+
1
−
1
View file @
5400343c
...
@@ -2,7 +2,7 @@ import java.util.List;
...
@@ -2,7 +2,7 @@ import java.util.List;
public
class
App
{
public
class
App
{
public
static
void
main
(
String
[]
args
)
throws
Exception
{
public
static
void
main
(
String
[]
args
)
throws
Exception
{
Backpack
backpack
=
new
InstanceReader
().
read
(
"src/sac
2
"
);
Backpack
backpack
=
new
InstanceReader
().
read
(
"src/sac
4
"
);
backpack
.
solve
();
backpack
.
solve
();
System
.
out
.
println
(
"meilleur solution :"
+
backpack
.
getBestValue
());
System
.
out
.
println
(
"meilleur solution :"
+
backpack
.
getBestValue
());
}
}
...
...
This diff is collapsed.
Click to expand it.
src/Backpack.java
+
22
−
26
View file @
5400343c
...
@@ -2,13 +2,9 @@ import java.util.Comparator;
...
@@ -2,13 +2,9 @@ import java.util.Comparator;
import
java.util.List
;
import
java.util.List
;
public
class
Backpack
{
public
class
Backpack
{
private
int
capacity
;
private
final
int
capacity
;
private
List
<
Loot
>
loots
;
private
final
List
<
Loot
>
loots
;
private
int
sfValuee
=
0
;
private
int
sfWeight
=
0
;
private
int
bestValue
=
0
;
private
int
bestValue
=
0
;
private
boolean
[]
bestSolution
;
private
boolean
[]
currentSolution
;
private
int
currentWeight
=
0
;
private
int
currentWeight
=
0
;
private
int
currentValue
=
0
;
private
int
currentValue
=
0
;
...
@@ -18,10 +14,9 @@ public class Backpack {
...
@@ -18,10 +14,9 @@ public class Backpack {
}
}
public
void
solve
()
{
public
void
solve
()
{
bestSolution
=
new
boolean
[
loots
.
size
()];
this
.
sortByRatio
();
currentSolution
=
new
boolean
[
loots
.
size
()];
solutionFractionnelle
();
explore_from
(
0
);
explore_from
(
0
);
System
.
out
.
println
(
"La valeur optimale est : "
+
bestValue
);
}
}
public
String
toString
(){
public
String
toString
(){
...
@@ -35,21 +30,26 @@ public class Backpack {
...
@@ -35,21 +30,26 @@ public class Backpack {
loots
.
sort
(
Comparator
.
comparing
(
Loot:
:
getRatio
).
reversed
());
loots
.
sort
(
Comparator
.
comparing
(
Loot:
:
getRatio
).
reversed
());
}
}
public
void
solutionFractionnelle
(){
public
int
solutionFractionnelle
(
int
startIndex
,
int
remainingCapacity
)
{
this
.
sortByRatio
();
int
sfValuee
=
0
;
for
(
Loot
loot
:
loots
){
int
sfWeight
=
0
;
if
(
sfWeight
+
loot
.
getWeight
()
<=
this
.
capacity
){
// On commence à partir de startIndex
for
(
int
i
=
startIndex
;
i
<
loots
.
size
();
i
++)
{
Loot
loot
=
loots
.
get
(
i
);
if
(
sfWeight
+
loot
.
getWeight
()
<=
remainingCapacity
){
// Si l'objet entier peut entrer dans le sac
// Si l'objet entier peut entrer dans le sac
sfValuee
+=
loot
.
getValue
();
sfValuee
+=
loot
.
getValue
();
sfWeight
+=
loot
.
getWeight
();
sfWeight
+=
loot
.
getWeight
();
}
}
else
{
else
{
// Si l'objet ne peut pas entrer entièrement dans le sac
// Si l'objet ne peut pas entrer entièrement dans le sac
int
remainingWeight
=
this
.
c
apacity
-
sfWeight
;
int
remainingWeight
=
remainingC
apacity
-
sfWeight
;
sfValuee
+=
(
int
)
(
loot
.
getRatio
()
*
remainingWeight
);
sfValuee
+=
(
int
)
(
loot
.
getRatio
()
*
remainingWeight
);
break
;
break
;
}
}
}
}
return
sfValuee
;
}
}
private
void
explore_from
(
int
index
)
{
private
void
explore_from
(
int
index
)
{
...
@@ -57,37 +57,33 @@ public class Backpack {
...
@@ -57,37 +57,33 @@ public class Backpack {
if
(
index
>=
loots
.
size
())
{
if
(
index
>=
loots
.
size
())
{
if
(
currentValue
>
bestValue
)
{
if
(
currentValue
>
bestValue
)
{
bestValue
=
currentValue
;
bestValue
=
currentValue
;
System
.
arraycopy
(
currentSolution
,
0
,
bestSolution
,
0
,
loots
.
size
());
}
}
return
;
return
;
}
}
// Si la valeur actuelle + la valeur restante possible (sfValuee - valeur déjà utilisée)
// On calcule la borne supérieure pour ce nœud
// est inférieure à la meilleure solution, on coupe la branche
int
remainingCapacity
=
capacity
-
currentWeight
;
if
(
currentValue
+
(
sfValuee
-
currentValue
)
<=
bestValue
)
{
int
borneSuperieure
=
currentValue
+
solutionFractionnelle
(
index
,
remainingCapacity
);
// Si la borne supérieure est inférieure à la meilleure solution
if
(
borneSuperieure
<=
bestValue
)
{
return
;
return
;
}
}
//
Branche gauche -
avec l'objet
//
on essaye d'explorer
avec l'objet
Loot
currentLoot
=
loots
.
get
(
index
);
Loot
currentLoot
=
loots
.
get
(
index
);
if
(
currentWeight
+
currentLoot
.
getWeight
()
<=
capacity
)
{
if
(
currentWeight
+
currentLoot
.
getWeight
()
<=
capacity
)
{
currentSolution
[
index
]
=
true
;
currentWeight
+=
currentLoot
.
getWeight
();
currentWeight
+=
currentLoot
.
getWeight
();
currentValue
+=
currentLoot
.
getValue
();
currentValue
+=
currentLoot
.
getValue
();
explore_from
(
index
+
1
);
explore_from
(
index
+
1
);
currentWeight
-=
currentLoot
.
getWeight
();
currentWeight
-=
currentLoot
.
getWeight
();
currentValue
-=
currentLoot
.
getValue
();
currentValue
-=
currentLoot
.
getValue
();
currentSolution
[
index
]
=
false
;
}
}
//
Branche droite -
sans l'objet
//
on explore
sans l'objet
explore_from
(
index
+
1
);
explore_from
(
index
+
1
);
}
}
public
int
getSfValuee
()
{
return
sfValuee
;
}
public
int
getBestValue
()
{
public
int
getBestValue
()
{
return
bestValue
;
return
bestValue
;
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment