txt au cas où

This commit is contained in:
2025-09-16 21:33:07 +02:00
parent c993e4c316
commit 2befe983c3
2 changed files with 328 additions and 0 deletions

187
src/Node.txt Normal file
View File

@@ -0,0 +1,187 @@
//IMPORTS
import java.util.LinkedList;
import java.util.*;
public class Node{
//ATTRIBUTS
/*----------------------------------------------------------------------------------*/
private String value;
private LinkedList<Node> successors = new LinkedList<Node>();
private int successors_len = 0;
private int depth = 0;
//ASSOCIATIONS DIRECTES
private LinkedList<Node> directedfrom = new LinkedList<Node>();
private LinkedList<Node> directedto = new LinkedList<Node>();
private int directedfrom_len = 0;
private int directedto_len = 0;
//PARAMETRES CUSTOMISES
/*le dictionnaire de paramètres customisés n'est pas ordonné. A prenre en compte
lors de la conception ou de la modification de fonctionnalités.*/
private Map<String, String> custom_parameters = new HashMap<String, String>();
private Set<Map.Entry<String, String>> cstprms_entry = custom_parameters.entrySet();
private Set<String> keys;
/*----------------------------------------------------------------------------------*/
//FIN ATTRIBUTS
//METHODES
/*----------------------------------------------------------------------------------*/
/*methodes params customs*/
private void updateKeysForCustParams(){
this.keys = this.custom_parameters.keySet();
}
public boolean newCustomParameter(String name, String value){
if(this.custom_parameters.containsKey(name)){
System.out.println("FAILED:UNIQUE NAME CONSTRAINT VIOLATED");
return false;
}
this.custom_parameters.put(name, value);
this.updateKeysForCustParams();
return true;
}
public boolean editCustomParameter(String name, String value){
if(this.custom_parameters.containsKey(name)){
this.custom_parameters.remove(name);
this.custom_parameters.put(name, value);
this.updateKeysForCustParams();
return true;
} else {
System.out.println("FAILED:PARAMETER NOT REGISTERED IN MAP");
return false;
}
}
public boolean removeCustomParameter(String name, String value){
if(!this.custom_parameters.containsKey(name)){
System.out.println("FAILED:PARAMETER NOT REGISTERED IN MAP");
return false;
}
this.custom_parameters.remove(name);
this.updateKeysForCustParams();
return true;
}
public String getCustomParam(String name){
if(!this.custom_parameters.containsKey(name)){
System.out.println("FAILED:PARAMETER NOT REGISTERED IN MAP");
return "";
}
return this.custom_parameters.get(name);
}
/*----------------------------------------------------------------------------------*/
/*methodes associations dirigées*/
public void addDirectedAssociationFrom(Node n){
this.directedfrom.add(n);
this.directedfrom_len++;
}
public void addDirectedAssociationTo(Node n){
this.directedto.add(n);
n.addDirectedAssociationFrom(this);
this.directedto_len++;
}
public LinkedList<Node> getDirectedAssociationsTo(){
return this.directedto;
}
public boolean hasDirectedAssociationTo(Node n){
for(int i=0; i<this.directedto_len; i++){
if(n == this.directedto.get(i)){
return true;
}
}
return false;
}
public boolean hasDirectedAssociationFrom(Node n){
for(int i=0; i<this.directedfrom_len; i++){
if(n == this.directedfrom.get(i)){
return true;
}
}
return false;
}
public int getLenDirectedAssociationsTo(){
return this.directedto_len;
}
public int getLenDirectedAssociationsFrom(){
return this.directedfrom_len;
}
/*----------------------------------------------------------------------------------*/
/*methodes generales*/
public String getVal(){
return this.value;
}
public int getDepth(){
return this.depth;
}
public void updateDepth(int val){
this.depth = val;
}
public Node getSuccessorAt(int index){
return this.successors.get(index);
}
public void addSuccessor(Node n){
n.updateDepth(this.depth+1);
this.successors.add(n);
this.successors_len++;
}
public void addSuccessors(Node[] ns){
for(int i=0; i<ns.length; i++){
ns[i].updateDepth(this.depth+1);
this.successors.add(ns[i]);
}
this.successors_len+=ns.length;
}
public void SortSuccessorsByIntVal(){
//tests
if(this.successors_len == 0){
System.out.println("FAILED:no successors");
return;
}
//variables
Node tmp;
//prog
for(int i=1; i<this.successors_len-1; i++){
for(int j=i; j<this.successors_len; j++){
if(Integer.parseInt(this.successors.get(j).getVal())<Integer.parseInt(this.successors.get(i).getVal())){
tmp = this.successors.get(i);
this.successors.set(i, this.successors.get(j));
this.successors.set(j, tmp);
}
}
}
}
/*----------------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------------*/
//FIN METHODES
//CONSTRUCTEUR(s)
/*----------------------------------------------------------------------------------*/
public Node(String value){
this.value = value;
}
/*----------------------------------------------------------------------------------*/
//FIN CONSTRUCTEURS(s)
}

141
src/Simulation.txt Normal file
View File

@@ -0,0 +1,141 @@
import java.util.LinkedList;
import java.util.Random;
public class Simulation {
public static void main(String[] args){
//tests
if(args.length<1){
System.out.println("USAGEERROR:java Simulation <int:nbr de sites>");
return;
}
if (Integer.parseInt(args[0])<2) {
System.out.println("FAILSAFE:<int:nbr de sites> MUST BE GREATER OR EQUAL TO 2");
}
//fin tests
//variables
LinkedList<Node> pool = new LinkedList<Node>();
LinkedList<Integer> popular_kids = new LinkedList<Integer>();
Random randgen = new Random();
int r1, r2;
double pertmax;
int scoremax;
Node x, y;
int targetScore;
int added_links = 0;
int no_link=0;
int last = -1;
boolean exit = false;
//fin variables
//prog
for(int i=0; i<Integer.parseInt(args[0]); i++){
pool.add(new Node("site"+Integer.toString(i+1)));
}
for(int i=0; i<Integer.parseInt(args[0]); i++){
pool.get(i).newCustomParameter("pertinence", Double.toString((double) (randgen.nextInt(100))));
pool.get(i).newCustomParameter("score", "0");
}
/*
for(int i=0; i<Integer.parseInt(args[0]); i++){
System.out.println(pool.get(i).getVal());
System.out.println(pool.get(i).getCustomParam("score"));
System.out.println(pool.get(i).getCustomParam("pertinence"));
System.out.println("-----------------------");
}*/
while((!exit)){
do{
r1 = randgen.nextInt(Integer.parseInt(args[0]));
}while((popular_kids.contains(r1))||(r1==last));
r2 = randgen.nextInt(Integer.parseInt(args[0]));
pertmax = -1;
if(r2!=r1){
x = pool.get(r1);
y = pool.get(r2);
while(x.hasDirectedAssociationTo(y)){
r2 = (r2+1)%Integer.parseInt(args[0]);
if(r2!=r1){
y = pool.get(r2);
}
}
for(int i=0; i<x.getLenDirectedAssociationsTo(); i++){
if(Double.parseDouble(x.getDirectedAssociationsTo().get(i).getCustomParam("pertinence"))>pertmax){
pertmax = Double.parseDouble(x.getDirectedAssociationsTo().get(i).getCustomParam("pertinence"));
}
}
if(Double.parseDouble(y.getCustomParam("pertinence"))>=pertmax){
x.addDirectedAssociationTo(y);
targetScore = Integer.parseInt(y.getCustomParam("score"));
y.editCustomParameter("score", Integer.toString(targetScore+1));
added_links++;
no_link=0;
} else {
no_link++;
}
last = r1;
if(x.getLenDirectedAssociationsTo()==(Integer.parseInt(args[0])-1)) {
popular_kids.add(r1);
}
}
try {
popular_kids.get(0);
if (added_links>=((Integer.parseInt(args[0])*(Integer.parseInt(args[0])+1)/2)-Integer.parseInt(args[0]))) {
exit=true;
}
/* rien */
} catch (IndexOutOfBoundsException e) {
/* rien */
} finally {
if(no_link >= 10000){
exit=true;
}
}
}
/* affichage (dans la console) */
System.out.println("les sites sont classés dans l'ordre du plus haut score au plus bas:");
for(int i=0; i<Integer.parseInt(args[0]); i++){
scoremax=-1;
targetScore=0;
for(int j=0; j<Integer.parseInt(args[0])-i; j++){
if(Integer.parseInt(pool.get(j).getCustomParam("score"))>scoremax){
scoremax=Integer.parseInt(pool.get(j).getCustomParam("score"));
pertmax=Double.parseDouble(pool.get(j).getCustomParam("pertinence"));
targetScore = j;
}
}
System.out.println(" "+Integer.toString(i+1)+") "+pool.get(targetScore).getVal()+" - score:"+pool.get(targetScore).getCustomParam("score")+" - pertinence:"+pool.get(targetScore).getCustomParam("pertinence"));
pool.remove(targetScore);
}
System.out.println("\nfin programme");
}
}
/*NOTES:
La classe Node.java est une classe que j'avais codé anciennement;
je l'ai reprise et adapté à l'exercice.
Les segments du code pertinents par rapport à l'exercice sont
codés dans les sections suivantes de la classe Node :
-PARAMETRES CUSTOMISES
-methodes associations dirigées
-methodes params customs
-CONSTRUCTEUR(s)
J'avais, pour des raisons que j'ignore, des problèmes avec la division des pertinences par 100 pour une représentation correcte.
La représentation est donc en pourcentage.
*/