Ex2. Tower of Hanio 1/11 Write down a C program to solve the Tower of Hanoi ga me (3 poles and 4 disks) by using CBMC – Hint: you may non-deterministically.

Slides:



Advertisements
Similar presentations
The Towers of Hanoi or Apocalypse When?.
Advertisements

Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
Recursion and Induction
CSC 205 Programming II Lecture 10 Towers of Hanoi.
More Recursion: Permutations and Towers of Hanoi COP 3502.
HW4:Royal Garden’s Puzzle as a Model Checking Problem Pictures from UbiSoft.
Array_strcpy void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0'; }
Union, bitfield, typedef, enum union nama_u{ }; union nama_u{ struct nama_s byte; }; enum{ }; Tipedef var BYTE.
Introduction To C Programming ปรีดา เลิศพงศ์วิภูษณะ ภาควิชาวิศวกรรมคอมพิวเตอร์ คณะวิศวกรรมศาสตร์ มหาวิทยาลัยเกษตรศาสตร์
COSO 1030 Section 4 Recursion. What is about Towers of Hanoi Divide and Conquer Strategy Recursion and Induction Thinking Recursively Recursion Pitfalls.
Nested Loops. Problem Print The only printfs you can use are: –printf(“*”); –printf(“\n”); *****
Fall 2007CS 2251 Proof by Induction. Fall 2007CS 2252 Proof by Induction There are two forms of induction Standard Induction –Prove the theorem is true.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Variables and constants Applications of Computer Programming in Earth Sciences Instructor: Dr. Cheng-Chien LiuCheng-Chien Liu Department of Earth Sciences.
10 ThinkOfANumber program1July ThinkOfANumber program CE : Fundamental Programming Techniques.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Ceng-112 Data Structures I Chapter 6 Recursion.
Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms.
CS Final Project 2010 Rohan Paramesh. The Programs  Implementing 3 programs each in C# and Python  Student Scheduler  Assigns courses and students,
JAVA Advanced Control Structures. Objectives Be able to use a switch statement for selective execution. Be able to implement repetition using alternative.
Advanced Data types and Sorting
CSCI 130 Chapter 3. Variables & Names Variable Declarations: –reserve a storage location in memory –identify the name of the variable –identify the type.
Chapter 10.7 Notes: Solve Quadratic Equations by the Quadratic Formula Goal: You will solve quadratic equations by using the Quadratic Formula.
CHAPTER 4 RECURSION. BASICALLY, A FUNCTION IS RECURSIVE IF IT INCLUDES A CALL TO ITSELF.
Chapter 5 Recursion. Basically, a method is recursive if it includes a call to itself.
HW6: Due Dec 8th 23:59 1.Describe test cases to reach full path coverage of the triangle program by completing the path condition table below. Also, draw.
HW#3: Due Nov 9 NOTE. Submit both hardcopy and softcopy. 1. Formal verification of a flash memory reading unit (70 pts) – Show the correctness of the flash_read()
Q1:Royal Garden’s Puzzle as a Model Checking Problem Pictures from UbiSoft HW6: Due Dec 4th 23:59.
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
1 Chapter 3: Loops and Logic. 2 Control Statements If statement Example NumberCheck.java Relational operators (, >=, ==, !=) Using code blocks with If.
1 Recursion. 2 A process by which a function calls itself repeatedly  Either directly. X calls X  Or cyclically in a chain. X calls Y, and Y calls X.
TOWERS OF HANOI. : If n = 1, move disk 1 from pole 'A' to pole 'B'. else: 1.First, move n-1 disks from pole 'A' to pole 'C', using pole 'B' as.
UNIT 17 Recursion: Towers of Hanoi.
HW #5 Due Nov 14 23:59 1. Write down a C program and a Promela model to solve the Tower of Hanoi game (3 poles and 4 disks) by using CBMC and Spin Hint:
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
Computer Programming 12 Lesson 6 – Loop structure By: Dan Lunney.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
1 Towers of Hanoi Three pegs, one with n disks of decreasing diameter; two other pegs are empty Task: move all disks to the third peg under the following.
Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative.
Programming Application in Civil Engineering
Algorithm Analysis 1.
Analysis of Algorithms
Analysis of Algorithms
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Loops in C C has three loop statements: the while, the for, and the do…while. The first two are pretest loops, and the the third is a post-test loop. We.
INC 161 , CPE 100 Computer Programming
Software Model Checking
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Introduction to Programming
פרטים נוספים בסילבוס של הקורס
Key Difference between Manual Testing and Model Checking
HW#2: Due Oct 18 NOTE. Submit both hardcopy and softcopy.
Structured Program Development in C
פרטים נוספים בסילבוס של הקורס
Key Difference between Manual Testing and Model Checking
HW5: Due Dec 6 23:59 Show the equivalence of the following two circuits by using a SMT solver 1.1 Specify the left circuit in QF_UF 1.2 Specify the right.
Analysis of Algorithms
The Hanoi Tower Problem
Recursion Definition: A method that calls itself. Examples
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Programming Language C Language.
1. (50 pts) Concolic testing the sort function
1. Show the correctness of the following max() (50 pts)
FIRST SOLUTION Producer Process Consumer Process
Recursive Thinking.
Q1:Royal Garden’s Puzzle as a Model Checking Problem
Analysis of Algorithms
HW#7 Describe test cases to reach full path coverage of the triangle program by completing the path condition table below. Also, draw the complete execution.
Presentation transcript:

Ex2. Tower of Hanio 1/11 Write down a C program to solve the Tower of Hanoi ga me (3 poles and 4 disks) by using CBMC – Hint: you may non-deterministically select the disk to move – Find the shortest solution by analyzing counter examples. Also explain why your solution is the shortest one. Use non-determinism and __CPROVER_assume() properly for the moving choice Use assert statement to detect when all the disks are moved to the destination

//cbmc hanoi3.c –unwind 7 –no-unwinding-assertions 1:signed char disk[3][3] = {{3,2,1},{0,0,0},{0,0,0}}; 2:char top[3]={2,-1,-1};// The position where the top disk is located at. 3: // If the pole does not have any disk, top is -1 4:int main() { 5: unsigned char dest, src; 14: while(1) { 15: src = non_det(); 16: __CPROVER_assume(src==0 || src==1 || src==2); 17: __CPROVER_assume(top[src] != -1); 18: 19: dest= non_det(); 20: __CPROVER_assume((dest==0 || dest==1 || dest==2) && (dest != src)); 21: __CPROVER_assume(top[dest]==-1 || (disk[src][top[src]] < disk[dest][top[dest]]) ); 22: 25: top[dest]++; 26: disk[dest][top[dest]]=disk[src][top[src]]; 27: 28: disk[src][top[src]]=0; 29: top[src]--; 30: 31: assert( !(disk[2][0]==3 && disk[2][1]==2 && disk[2][2]==1 )); } } Top[3]

Ex3. Flash read verification 1. Formal verification of a flash memory reading unit – Show the correctness of the flash_read() By using randomized testing – Randomly select the physical sectors to write four characters and set the corresponding SAMs By using exhaustive testing – Create (16*15*14*13) distinct test cases » Do not print test cases in your hardcopy to save trees By using CBMC – Create environment model satisfying the invariant formula by using __CPROVER_assume() and nested loops

typedef struct _SAM_type{ unsigned char offset[SECT_PER_U]; }SAM_type; typedef struct _PU_type{ unsigned char sect[SECT_PER_U]; }PU_type; // Environment assumption // 0. Each unit contains 4 sectors. // 1. There is one logical unit containing "abcd" // 2. There are 4 physical units // 3. The value of SAM table is 255 if the corresponding // physical sector does not have a valid data void flash_read(char *buf, SAM_type *SAM, PU_type *pu ){ unsigned char nSamIdx = 0; unsigned char pu_id = 0; unsigned char n_scts = 4; // number of sectors to read unsigned char offset = 0; //offset of the physical sector to read unsigned char pBuf = 0; while(n_scts > 0){ pu_id=0; offset = 255; // read 1 character while(1) { if (SAM[pu_id].offset[nSamIdx] != 255){ offset = SAM[pu_id].offset[nSamIdx++]; buf[pBuf] = PU[pu_id].sect[offset]; break; } pu_id ++; } n_scts--; pBuf ++; }

#include #define SECT_PER_U 4 #define NUM_PHI_U 4 typedef struct _SAM_type{ unsigned char offset[SECT_PER_U]; SAM_type; typedef struct _PU_type{ unsigned char sect[SECT_PER_U]; }PU_type; char data[SECT_PER_U] = "abcd"; PU_type pu[NUM_PHI_U]; SAM_type SAM[NUM_PHI_U]; void randomized_test(){ unsigned int i = 0, j = 0; unsigned char ind_pu, ind_Sect; // Initialization for(i = 0;i < NUM_PHI_U;i++){ for(j = 0;j < SECT_PER_U;j++){ SAM[i].offset[j] = 255; pu[i].sect[j] = 0; }} while (i< SECT_PER_U) { ind_pu = rand()%4; ind_Sect= rand()%4; if(pu[ind_pu].sect[ind_Sect] == 0){ pu[ind_pu].sect[ind_Sect] = data[i]; SAM[ind_pu].offset[i] = ind_Sect; i++; } void main(){ char res[50]; int tc = 0; int count = 0; int nTC = 43680;// # of possible distribution // 16*15*14*13 while(tc++ < nTC){ randomized_test(); flash_read(&res[count],SAM,pu); assert(res[0] == 'a' && res[1] == 'b' && res[2] == 'c' && res[3] =='d'); }} Problem #1. Random solution ind_pu ind_sect

Problem #1. Exhaustive solution void exhaustive_test(int *data_pos){ unsigned int i = 0, j = 0; unsigned char ind_pu, ind_Sect; for(i = 0;i < NUM_PHI_U;i++){ for(j = 0;j < SECT_PER_U;j++){ SAM[i].offset[j] = 255; pu[i].sect[j] = 0; } for(i = 0;i < NUM_PHI_U;i++){ ind_pu = data_pos[i]/4; ind_Sect = data_pos[i]%4; pu[ind_pu].sect[ind_Sect] = data[i]; SAM[ind_pu].offset[i] = ind_Sect; } void main(){ char res[4]; int i, j,k,l, data_pos[4]; //# of all distributions = 16*15*14*13 for(i = 0;i < NUM_PHI_U * SECT_PER_U;i++){ for(j = 0;j < NUM_PHI_U * SECT_PER_U;j++){ if (j == i) continue; for(k = 0;k < NUM_PHI_U * SECT_PER_U;k++){ if (k == i || k == j) continue; for(l = 0;l < NUM_PHI_U * SECT_PER_U;l++){ if(l == i || l == j || l == k) continue; data_pos[0] = i; data_pos[1] = j; data_pos[2] = k; data_pos[3] = l; exhaustive_test(data_pos); flash_read(&res[count],SAM,pu); assert(res[0] == 'a' && res[1] == 'b' && res[2] == 'c' && res[3] == 'd'); } } } } } a b c d ind_pu ind_sect data_pos

Problem #1. CBMC solution void CBMC_environ_setting(){ unsigned int i = 0, j = 0; unsigned char ind_pu, ind_Sect; for(i = 0;i < NUM_PHI_U;i++){ for(j = 0;j < SECT_PER_U;j++){ SAM[i].offset[j] = 255; pu[i].sect[j] = 0; }} for(i = 0;i < SECT_PER_U;i++){ ind_pu = nondet_char(); ind_Sect = nondet_char(); __CPROVER_assume(ind_pu>=0 && ind_pu <NUM_PHI_U); __CPROVER_assume(ind_Sect>=0 && ind_Sect <SECT_PER_U); __CPROVER_assume(pu[ind_pu].sect[ind_Sect] == 0); pu[ind_pu].sect[ind_Sect] = data[i]; SAM[ind_pu].offset[i] = ind_Sect; } void main(){ char res[50]; int count = 0; CBMC_environ_setting(); flash_read(&res[count],SAM,pu); assert(res[0] == 'a' && res[1] == 'b' && res[2] == 'c' && res[3] == 'd'); }