Partage
  • Partager sur Facebook
  • Partager sur Twitter

comment puis-je vérifier l'éxistance du cas suivant ?

    25 janvier 2012 à 16:02:04

    Bonjours les gros;
    le code qui j'ai il généré tous les cas possible de carré latin
    mon souci c'est comment puis-je vérifier l'éxistance du cas du carré d'ordre 4
    le cas :
    1 2 3 4
    2 1 4 3
    3 4 1 2
    4 3 2 1

    sachant que j'utilise le solveur choco (librairie java choco.jar) :(
    et je serai vraiment très reconnaissant pour ceux qui peuvent m'aidez
    le code c'est :



    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.List;
    
    import choco.Problem;
    import choco.Solution;
    import choco.Solver;
    import choco.integer.IntDomainVar;
    import choco.integer.IntExp;
    
    
    public class MyMagicSquare {
    	public static void main(String[] args) throws IOException {
    		
    		Problem p;
    		int n = 0;
    		IntDomainVar[] carre;
    		IntDomainVar sumVar;
    		IntDomainVar[] col;
    		IntDomainVar[] row;
    		IntDomainVar[] diag1;
    		IntDomainVar[] diag2;
    		
    		boolean stop = false;
    		
    		while(!stop){
    			System.out.println("");
    			System.out.println("***************** Choix du type du carré latin entrez 1 ******************");
    			System.out.println("1 : carré latin ");
    			//System.out.println("2 : Carré magique avec une case initialisé");
    			//System.out.println("3 : Carré magique avec des sommes différentes");
    			//System.out.println("4 : Carré magique avec des sommes successives");
    			//System.out.println("(-1) : Pour quitter");
    			BufferedReader clavier = new BufferedReader(new InputStreamReader(System.in));
    			
    			String choix = clavier.readLine();
    			int type = Integer.parseInt(choix);
    			int sum;
    			
    			switch(type){
    			case 1:
    				System.out.println("Entrez la taille du QuasiGroupe : ");
    				
    				String taille = clavier.readLine();
    				n = Integer.parseInt(taille);
    				
    				if(n !=-1){
    					
    					System.out.println("Le QuasiGroupe de taille "+n);
    					
    					p = new Problem();
    					
    					//Création des variables
    					carre = new IntDomainVar[n*n];
    					
    					for(int i = 0; i < n; i++)
    						for(int j = 0; j < n; j++)
    							carre[i*n+j] = p.makeBoundIntVar("C" + i + j, 1, n*n);
    					
    					sum = n*(n*n +1)/2; 
    					sumVar = p.makeBoundIntVar("SUM",sum,sum);
    					
    					col = new IntDomainVar[n];
    					row = new IntDomainVar[n];
    					diag1 = new IntDomainVar[n];
    					diag2 = new IntDomainVar[n];
    					
    					//Créations des contraintes
    					p.post(p.allDifferent(carre));
    					
    					for (int i = 0; i < n; i++){
    						for (int j = 0; j < n; j++)
    							col[j]= carre[i*n+j];
    						p.post(p.eq(p.sum(col), sumVar));
    					}
    					
    					for (int j = 0; j < n; j++){
    						for (int i = 0; i < n; i++)
    							row[i]= carre[i*n+j];
    						p.post(p.eq(p.sum(row), sumVar));
    					}
    					
    					//Diagonale directe
    					for (int i = 0; i < diag1.length; i++) {
    						diag1[i]=carre[i*n+i];
    					}
    					p.post(p.eq(p.sum(diag1),sumVar));
    					
    					//Diagonale indirecte
    					for (int i = 0; i < diag2.length; i++) {
    						diag2[i]=carre[i*n+(n-(i+1))];
    					}
    					p.post(p.eq(p.sum(diag2),sumVar));
    					
    					//affichage de la solution
    					//afficherLaPremiereSolution(carre,n,p);
    					System.out.println(afficherToutesLesSolutions(p,n));
    					
    				}			
    				break;
    			case 2:
    				System.out.println("2 : Carré magique avec une case initialisé");
    				System.out.println("Entrez la taille du carre magique : ");
    				taille = clavier.readLine();
    				n = Integer.parseInt(taille);
    				System.out.println("Entrez la ligne de la case à initialiser : ");
    				String ligne = clavier.readLine();
    				int l = Integer.parseInt(ligne);
    				System.out.println("Entrez la colonne de la case à initialiser : ");
    				String colonne= clavier.readLine();
    				int c = Integer.parseInt(colonne);
    				System.out.println("Entrez la valeur de la case à initialiser : ");
    				String valeur = clavier.readLine();
    				int v = Integer.parseInt(valeur);
    				
    				System.out.println("Le carré magique de taille "+n);
    				System.out.println("( "+l+","+c+" ) = "+v);
    				
    				p = new Problem();
    				
    				//Création des variables
    				carre = new IntDomainVar[n*n];
    				
    				for(int i = 0; i < n; i++)
    					for(int j = 0; j < n; j++)
    						carre[i*n+j] = p.makeBoundIntVar("C" + i + j, 1, n*n);
    				
    				sum = n*(n*n +1)/2; 
    				sumVar = p.makeBoundIntVar("SUM",sum,sum);
    				
    				col = new IntDomainVar[n];
    				row = new IntDomainVar[n];
    				diag1 = new IntDomainVar[n];
    				diag2 = new IntDomainVar[n];
    				
    				//Créations des contraintes
    				p.post(p.allDifferent(carre));
    				
    				for (int i = 0; i < n; i++){
    					for (int j = 0; j < n; j++)
    						col[j]= carre[i*n+j];
    					p.post(p.eq(p.sum(col), sumVar));
    				}
    				
    				for (int j = 0; j < n; j++){
    					for (int i = 0; i < n; i++)
    						row[i]= carre[i*n+j];
    					p.post(p.eq(p.sum(row), sumVar));
    				}
    				
    				//Diagonale directe
    				for (int i = 0; i < diag1.length; i++) {
    					diag1[i]=carre[i*n+i];
    				}
    				p.post(p.eq(p.sum(diag1),sumVar));
    				
    				//Diagonale indirecte
    				for (int i = 0; i < diag2.length; i++) {
    					diag2[i]=carre[i*n+(n-(i+1))];
    				}
    				p.post(p.eq(p.sum(diag2),sumVar));
    				
    				p.post(p.eq(carre[l*n+c],v));
    				//affichage de la solution
    				//afficherLaPremiereSolution(carre,n,p);
    				System.out.println(afficherToutesLesSolutions(p,n));
    								
    				break;
    			case 3:
    				System.out.println("3 : Carré magique avec des sommes différentes");
    				System.out.println("Entrez la taille du carre magique : ");
    				
    				taille = clavier.readLine();
    				n = Integer.parseInt(taille);
    				IntExp[] sommes = new IntExp[2*n+2];
    				
    				if(n !=-1){
    					
    					System.out.println("Le carré magique de taille "+n);
    					
    					p = new Problem();
    					
    					//Création des variables
    					carre = new IntDomainVar[n*n];
    					
    					for(int i = 0; i < n; i++)
    						for(int j = 0; j < n; j++)
    							carre[i*n+j] = p.makeBoundIntVar("C" + i + j, 1, n*n);
    					
    					/*sum = n*n*(n*n +1)/2; 
    					sumVar = p.makeEnumIntVar("SUM",1,sum);*/
    					
    					col = new IntDomainVar[n];
    					row = new IntDomainVar[n];
    					diag1 = new IntDomainVar[n];
    					diag2 = new IntDomainVar[n];
    					
    					for(int i = 0; i < 2*n+2; i++)
    						sommes[i] = p.makeBoundIntVar("S"+i,1,(n*n)*(n*n +1)/2);
    					
    					//Créations des contraintes
    					p.post(p.allDifferent(carre));
    					
    					for (int i = 0; i < n; i++){
    						for (int j = 0; j < n; j++)
    							col[j]= carre[i*n+j];
    						//p.post(p.eq(p.sum(col), sumVar));
    						sommes[i] = p.sum(col);
    					}
    					
    					for (int j = 0; j < n; j++){
    						for (int i = 0; i < n; i++)
    							row[i]= carre[i*n+j];
    						//p.post(p.eq(p.sum(row), sumVar));
    						sommes[j+n] = p.sum(row); 
    					}
    					
    					//Diagonale directe
    					for (int i = 0; i < diag1.length; i++) {
    						diag1[i]=carre[i*n+i];
    					}
    					//p.post(p.eq(p.sum(diag1),sumVar));
    					sommes[2*n+1] = p.sum(diag1);
    					
    					//Diagonale indirecte
    					for (int i = 0; i < diag2.length; i++) {
    						diag2[i]=carre[i*n+(n-(i+1))];
    					}
    					//p.post(p.eq(p.sum(diag2),sumVar));
    					sommes[2*n+1] = p.sum(diag2);
    				
    					//Toutes les sommes sont différentes
    					for (int i = 0; i < 2*n+1; i++) {
    					      for (int j = i + 1; j < 2*n+1; j++) {
    					    	  p.post(p.neq(sommes[i], sommes[j]));				
    					        	        
    					      }
    					    }
    					
    					//affichage de la solution
    					afficherLaPremiereSolution(carre,n,p);
    					//System.out.println(afficherToutesLesSolutions(p,n));
    					
    				}
    				break;
    			case 4:
    				System.out.println("4 : Carré magique avec des sommes successives");
    				System.out.println("Entrez la taille du carre magique : ");
    				
    				taille = clavier.readLine();
    				n = Integer.parseInt(taille);
    				sommes = new IntExp[2*n+2];
    				
    				if(n !=-1){
    					
    					System.out.println("Le carré magique de taille "+n);
    					
    					p = new Problem();
    					
    					//Création des variables
    					carre = new IntDomainVar[n*n];
    					
    					for(int i = 0; i < n; i++)
    						for(int j = 0; j < n; j++)
    							carre[i*n+j] = p.makeBoundIntVar("C" + i + j, 1, n*n);
    					
    					col = new IntDomainVar[n];
    					row = new IntDomainVar[n];
    					diag1 = new IntDomainVar[n];
    					diag2 = new IntDomainVar[n];
    					
    					for(int i = 0; i < 2*n+2; i++)
    						sommes[i] = p.makeBoundIntVar("S"+i,1,(n*n)*(n*n +1)/2);
    					
    					//Créations des contraintes
    					p.post(p.allDifferent(carre));
    					
    					for (int i = 0; i < n; i++){
    						for (int j = 0; j < n; j++)
    							col[j]= carre[i*n+j];
    						sommes[i] = p.sum(col);
    					}
    					
    					for (int j = 0; j < n; j++){
    						for (int i = 0; i < n; i++)
    							row[i]= carre[i*n+j];
    						sommes[j+n] = p.sum(row); 
    					}
    					
    					//Diagonale directe
    					for (int i = 0; i < diag1.length; i++) {
    						diag1[i]=carre[i*n+i];
    					}
    					sommes[2*n+1] = p.sum(diag1);
    					
    					//Diagonale indirecte
    					for (int i = 0; i < diag2.length; i++) {
    						diag2[i]=carre[i*n+(n-(i+1))];
    					}
    					sommes[2*n+1] = p.sum(diag2);
    					
    					//Toutes les sommes sont successives
    					for (int i = 0; i < 2*n+1; i++) {
    					    	  p.post(p.or(p.eq(p.minus(sommes[i],sommes[i+1]),1), p.eq(p.minus(sommes[i],sommes[i+1]),-1)));
    					    }
    					
    					//affichage de la solution
    					afficherLaPremiereSolution(carre,n,p);
    					//System.out.println(afficherToutesLesSolutions(p,n));
    					
    				}
    				break;
    			case -1:
    				stop = true;
    				System.out.println("Merci d'avoir testé notre jeu et à bientôt :)");
    			}
    			
    		}
    		
    	}
    	
    	public static IntExp somme(IntDomainVar[] v, Problem p){
    		IntExp s=null;
    		for (int i = 0; i < v.length; i++) {
    			s = p.plus(v[i],s);
    		}
    		return s;
    	}
    	
    	public static void afficherLaPremiereSolution(IntDomainVar[] carre, int n, Problem p){
    		//Lancer le solveur
    		p.solve();
    		if(!p.isFeasible())
    			System.out.println("Le problème n'est pas faisable :'(");
    		else{
    			//System.out.println("Le problème est faisable et la somme est égale à "+sum+".");
    			System.out.println("Le nombre de solutions possibles est : "+p.getSolver().getNbSolutions());
    			
    			for(int i = 0; i < n; i++)
    				for(int j = 0; j < n; j++){
    					if(j%n==0)
    						System.out.println("");
    					else System.out.print(" | ");
    					
    					if(j%n==0){
    						for (int k = 0; k < 2*n-1; k++)
    							System.out.print("--");
    						System.out.println("");
    					}
    					System.out.print(carre[i*n+j].getVal());
    				}		
    			System.out.println("");
    			for (int k = 0; k < 2*n-1; k++)
    				System.out.print("--");
    			System.out.println("");	
    		}
    		
    	}
    	
    	public static StringBuffer afficherToutesLesSolutions(Problem p, int n){
    		//Lancer le solveur
    		p.solveAll();
    		Solver solver = p.getSolver();
    		StringBuffer s = new StringBuffer();
    		List solutions = solver.getSearchSolver().solutions;
    		
    		if(!p.isFeasible())
    			System.out.println("Le problème n'est pas faisable :'(");
    		else{
    			//System.out.println("Le problème est faisable et la somme est égale à "+sum+".");
    			System.out.println("Le nombre de solutions possibles est : "+p.getSolver().getNbSolutions());
    			s.append("Affichage des "+solutions.size() + " premières solutions \n");
    			
    			for (int i = 0; i < solutions.size(); i++) {
    				Solution solution = (Solution) solutions.get(i);
    				s.append("Solution "+(i+1)+" :\n");
    				for (int v = 0; v < p.getNbIntVars()-1; v++) {
    					if (v % n == 0){
    						for(int j=0; j<2*n-1;j++)
    							s.append("--");
    						s.append("\n");
    					}
    					s.append(solution.getValue(v));
    					
    					if (v % n == n-1)
    						s.append("\n");
    					else
    						s.append(" | ");
    				}
    				for(int j=0; j<2*n-1;j++)
    					s.append("--");
    				s.append("\n");
    			}
    		}
    		return s;
    	}
    }
    
    • Partager sur Facebook
    • Partager sur Twitter
      26 janvier 2012 à 12:32:11

      Salut,

      Citation : étudiant

      vérifier l'éxistance du cas du carré d'ordre 4


      tu peut être plus clair?
      • Partager sur Facebook
      • Partager sur Twitter

      comment puis-je vérifier l'éxistance du cas suivant ?

      × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
      × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
      • Editeur
      • Markdown