Skip to content

Commit a28017e

Browse files
committed
Added missing files for CAV CEGIS control front-end
Missing files for CAV benchmark front-end for CEGIS controller synthesis.
1 parent ce373f6 commit a28017e

12 files changed

+783
-0
lines changed
Lines changed: 299 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,299 @@
1+
#include <stdio.h>
2+
3+
typedef __CPROVER_fixedbv[24][12] __CPROVER_EIGEN_fixedbvt;
4+
5+
typedef struct {
6+
//__CPROVER_EIGEN_fixedbvt A[3][3];
7+
__CPROVER_EIGEN_fixedbvt A[4][4];
8+
//__CPROVER_EIGEN_fixedbvt B[3][1];
9+
__CPROVER_EIGEN_fixedbvt B[4][4];
10+
//__CPROVER_EIGEN_fixedbvt C[1][3];
11+
__CPROVER_EIGEN_fixedbvt C[4][4];
12+
//__CPROVER_EIGEN_fixedbvt D[1][1];
13+
__CPROVER_EIGEN_fixedbvt D[4][4];
14+
__CPROVER_EIGEN_fixedbvt states[4][4];
15+
__CPROVER_EIGEN_fixedbvt outputs[4][4];
16+
__CPROVER_EIGEN_fixedbvt inputs[1][1];
17+
__CPROVER_EIGEN_fixedbvt K[4][4];
18+
unsigned int nStates;
19+
unsigned int nInputs;
20+
unsigned int nOutputs;
21+
} digital_system_state_space;
22+
23+
__CPROVER_EIGEN_fixedbvt nondet_double(void);
24+
25+
//implementation <7,3>
26+
//states = 3;
27+
//inputs = 1;
28+
//outputs = 1;
29+
//A = [4.6764e-166,0,0;5.1253e-144,0,0;0,2.5627e-144,0]
30+
//B = [0.125;0;0]
31+
//C = [0.16,-5.9787e-32,0]
32+
//D = [0]
33+
//inputs = [1]
34+
#define NSTATES 3u
35+
#define NINPUTS 1u
36+
#define NOUTPUTS 1u
37+
38+
const digital_system_state_space _controller=
39+
{
40+
.A = { { 4.6764e-166,0.0,0.0,0.0 }, { 5.1253e-144,0.0,0.0,0.0 }, { 0,2.5627e-144,0.0,0.0 } },
41+
.B = { { 0.125,0.0,0.0,0.0 }, { 0.0,0.0,0.0,0.0 }, { 0.0,0.0,0.0,0.0 } },
42+
.C = { { 0.16,-5.9787e-32,0.0,0.0 } },
43+
.D = { { 0.0 } },
44+
.K = { { nondet_double(), nondet_double(), nondet_double() } },
45+
.inputs = { { 1.0 } },
46+
.nStates = NSTATES,
47+
.nInputs = NINPUTS,
48+
.nOutputs = NOUTPUTS
49+
};
50+
51+
52+
//typedef int __CPROVER_EIGEN_fixedbvt;
53+
#define __CPROVER_EIGEN_MATRIX_SIZE 3u
54+
#define __CPROVER_EIGEN_POLY_SIZE (__CPROVER_EIGEN_MATRIX_SIZE + 1u)
55+
//const __CPROVER_EIGEN_fixedbvt __CPROVER_EIGEN_TEST_A[__CPROVER_EIGEN_MATRIX_SIZE][__CPROVER_EIGEN_MATRIX_SIZE] = { { 0.0, 1.0, 0.0 }, { 0.0, 0.0, 1.0 }, { 1.0, 0.0, 0.0 } };
56+
extern const __CPROVER_EIGEN_fixedbvt __CPROVER_EIGEN_TEST_A[__CPROVER_EIGEN_MATRIX_SIZE][__CPROVER_EIGEN_MATRIX_SIZE];
57+
__CPROVER_EIGEN_fixedbvt __CPROVER_EIGEN_poly[__CPROVER_EIGEN_POLY_SIZE];
58+
59+
__CPROVER_EIGEN_fixedbvt internal_pow(__CPROVER_EIGEN_fixedbvt a, unsigned int b){
60+
unsigned int i;
61+
__CPROVER_EIGEN_fixedbvt acc = 1.0;
62+
for (i=0; i < b; i++){
63+
acc = acc*a;
64+
}
65+
return acc;
66+
}
67+
68+
__CPROVER_EIGEN_fixedbvt internal_abs(__CPROVER_EIGEN_fixedbvt a){
69+
return a < 0 ? -a : a;
70+
}
71+
72+
int check_stability(void){
73+
#define __a __CPROVER_EIGEN_poly
74+
#define __n __CPROVER_EIGEN_POLY_SIZE
75+
int lines = 2 * __n - 1;
76+
int columns = __n;
77+
__CPROVER_EIGEN_fixedbvt m[lines][__n];
78+
int i,j;
79+
80+
/* to put current values in stability counter-example
81+
* look for current_stability (use: --no-slice) */
82+
__CPROVER_EIGEN_fixedbvt current_stability[__n];
83+
for (i=0; i < __n; i++){
84+
current_stability[i] = __a[i];
85+
}
86+
87+
/* check the first constraint condition F(1) > 0 */
88+
__CPROVER_EIGEN_fixedbvt sum = 0;
89+
for (i=0; i < __n; i++){
90+
sum += __a[i];
91+
}
92+
if (sum <= 0){
93+
printf("[DEBUG] the first constraint of Jury criteria failed: (F(1) > 0)");
94+
return 0;
95+
}
96+
97+
/* check the second constraint condition F(-1)*(-1)^n > 0 */
98+
sum = 0;
99+
for (i=0; i < __n; i++){
100+
sum += __a[i] * internal_pow(-1, __n-1-i);
101+
}
102+
sum = sum * internal_pow(-1, __n-1);
103+
if (sum <= 0){
104+
printf("[DEBUG] the second constraint of Jury criteria failed: (F(-1)*(-1)^n > 0)");
105+
return 0;
106+
}
107+
108+
/* check the third constraint condition abs(a0 < an*(z^n) */
109+
if (internal_abs(__a[__n-1]) > __a[0]){
110+
printf("[DEBUG] the third constraint of Jury criteria failed: (abs(a0) < a_{n}*z^{n})");
111+
return 0;
112+
}
113+
114+
/* check the fourth constraint of condition (Jury Table) */
115+
for (i=0; i < lines; i++){
116+
for (j=0; j < columns; j++){
117+
m[i][j] = 0;
118+
}
119+
}
120+
for (i=0; i < lines; i++){
121+
for (j=0; j < columns; j++){
122+
if (i == 0){
123+
m[i][j] = __a[j];
124+
continue;
125+
}
126+
if (i % 2 != 0 ){
127+
int x;
128+
for(x=0; x<columns;x++){
129+
m[i][x] = m[i-1][columns-x-1];
130+
}
131+
columns = columns - 1;
132+
j = columns;
133+
}else{
134+
m[i][j] = m[i-2][j] - (m[i-2][columns] / m[i-2][0]) * m[i-1][j];
135+
}
136+
}
137+
}
138+
int first_is_positive = m[0][0] >= 0 ? 1 : 0;
139+
for (i=0; i < lines; i++){
140+
if (i % 2 == 0){
141+
int line_is_positive = m[i][0] >= 0 ? 1 : 0;
142+
if (first_is_positive != line_is_positive){
143+
return 0;
144+
}
145+
continue;
146+
}
147+
}
148+
return 1;
149+
}
150+
151+
152+
// P(s)=(s-m11)*(s-m22)*(s-m33) - m13*m31*(s-m22) - m12*m21*(s-m33) - m23*m32*(s-m11) - m12*m23*m31 - m13*m21*m32
153+
// P(s)=s^3 + (-m_11 - m_22 - m_33) * s^2 + (m_11*m_22 + m_11*m_33 - m_12*m_21 - m_13*m_31 + m_22*m_33 - m_23*m_32) * s - m_11*m_22*m_33 + m_11*m_23*m_32 + m_12*m_21*m_33 - m_12*m_23*m_31 - m_13*m_21*m_32 + m_13*m_22*m_31
154+
void __CPROVER_EIGEN_charpoly(void) {
155+
#define __m __CPROVER_EIGEN_TEST_A
156+
// m_11*m_22*m_33 + m_11*m_23*m_32 + m_12*m_21*m_33 - m_12*m_23*m_31 - m_13*m_21*m_32 + m_13*m_22*m_31
157+
__CPROVER_EIGEN_poly[0] = __m[0][0] * __m[1][1] * __m[2][2] + __m[0][0] * __m[1][2] * __m[2][1] + __m[0][1] * __m[1][0] * __m[2][2] - __m[0][1] * __m[1][2] * __m[2][0] - __m[0][2] * __m[1][0] * __m[2][1] + __m[0][2] * __m[1][1] * __m[2][0];
158+
// (m_11*m_22 + m_11*m_33 - m_12*m_21 - m_13*m_31 + m_22*m_33 - m_23*m_32) * s
159+
__CPROVER_EIGEN_poly[1] = __m[0][0] * __m[1][1] + __m[0][0] * __m[2][2] - __m[0][1] * __m[1][0] - __m[0][2] * __m[2][0] + __m[1][1] * __m[2][2] - __m[1][2] * __m[2][1];
160+
// (-m_11 - m_22 - m_33) * s^2
161+
__CPROVER_EIGEN_poly[2] = -__m[0][0] - __m[1][1] - __m[2][2];
162+
// s^3
163+
__CPROVER_EIGEN_poly[3] = 1.0;
164+
}
165+
166+
/*void init(void) {
167+
_controller.K[0][0] = nondet_double();
168+
_controller.K[0][1] = nondet_double();
169+
_controller.K[0][2] = nondet_double();
170+
}*/
171+
172+
__CPROVER_EIGEN_fixedbvt __CPROVER_EIGEN_matrix_multiplication_result[4][4];
173+
174+
void double_sub_matrix(void/* unsigned int lines, unsigned int columns, __CPROVER_EIGEN_fixedbvt m1[4][4], __CPROVER_EIGEN_fixedbvt m2[4][4], __CPROVER_EIGEN_fixedbvt result[4][4]*/){
175+
#define __sm_lines NSTATES
176+
#define __sm_columns NSTATES
177+
#define __sm_m1 _controller.A
178+
#define __sm_m2 __CPROVER_EIGEN_matrix_multiplication_result
179+
#define __sm_m3 _controller.A
180+
unsigned int i, j;
181+
for (i = 0; i < __sm_lines; i++){
182+
for (j = 0; j < __sm_columns; j++){
183+
__sm_m3[i][j] = __sm_m1[i][j] - __sm_m2[i][j];
184+
185+
}
186+
}
187+
}
188+
189+
void double_matrix_multiplication(void/* unsigned int i1, unsigned int j1, unsigned int i2, unsigned int j2, __CPROVER_EIGEN_fixedbvt m1[4][4], __CPROVER_EIGEN_fixedbvt m2[4][4], __CPROVER_EIGEN_fixedbvt m3[4][4]*/){
190+
#define __mm_i1 NSTATES
191+
//unsigned int __mm_i1;
192+
#define __mm_j1 NINPUTS
193+
//unsigned int __mm_j1;
194+
#define __mm_i2 NINPUTS
195+
//unsigned int __mm_i2;
196+
#define __mm_j2 NSTATES
197+
//unsigned int __mm_j2;
198+
#define __mm_m1 _controller.B
199+
//__CPROVER_EIGEN_fixedbvt __mm_m1[4][4];
200+
#define __mm_m2 _controller.K
201+
//__CPROVER_EIGEN_fixedbvt __mm_m2[4][4];
202+
#define __mm_m3 __CPROVER_EIGEN_matrix_multiplication_result
203+
//__CPROVER_EIGEN_fixedbvt __mm_m3[4][4];
204+
205+
unsigned int i, j, k;
206+
if (__mm_j1 == __mm_i2) {
207+
208+
/*for (i=0; i<__mm_i1; i++) {
209+
for (j=0; j<__mm_j2; j++) {
210+
__mm_m3[i][j] = 0;
211+
}
212+
}*/
213+
214+
for (i=0;i<__mm_i1; i++) {
215+
for (j=0; j<__mm_j2; j++) {
216+
for (k=0; k<__mm_j1; k++) {
217+
218+
__CPROVER_EIGEN_fixedbvt mult = (__mm_m1[i][k] * __mm_m2[k][j]);
219+
220+
221+
__mm_m3[i][j] = __mm_m3[i][j] + (__mm_m1[i][k] * __mm_m2[k][j]);
222+
223+
224+
}
225+
226+
}
227+
}
228+
} else {
229+
printf("\nError! Operation invalid, please enter with valid matrices.\n");
230+
}
231+
}
232+
233+
#define LIMIT 4u
234+
235+
void closed_loop(void)
236+
{
237+
//__CPROVER_EIGEN_fixedbvt result1[LIMIT][LIMIT];
238+
239+
/*int i, j, k;
240+
for(i=0; i<LIMIT;i++)
241+
for(j=0; j<LIMIT;j++)
242+
result1[i][j]=0;*/
243+
244+
// B*K
245+
/*double_matrix_multiplication(
246+
NSTATES,
247+
NINPUTS,
248+
NINPUTS,
249+
NSTATES,
250+
_controller.B,
251+
_controller.K,
252+
result1);*/
253+
double_matrix_multiplication();
254+
255+
/*double_sub_matrix(
256+
_controller.nStates,
257+
_controller.nStates,
258+
_controller.A,
259+
result1,
260+
_controller.A);*/
261+
double_sub_matrix();
262+
263+
/*for(i=0; i<LIMIT;i++)
264+
for(j=0; j<LIMIT;j++)
265+
result1[i][j]=0;
266+
267+
//D*K
268+
double_matrix_multiplication(
269+
_controller.nOutputs,
270+
_controller.nInputs,
271+
_controller.nInputs,
272+
_controller.nStates,
273+
_controller.D,
274+
_controller.K,
275+
result1);
276+
277+
double_sub_matrix(
278+
_controller.nOutputs,
279+
_controller.nStates,
280+
_controller.C,
281+
result1,
282+
_controller.C);*/
283+
}
284+
285+
int main(void) {
286+
//init();
287+
closed_loop();
288+
__CPROVER_EIGEN_charpoly();
289+
//__CPROVER_assert(check_stability(), "");
290+
__CPROVER_assume(check_stability() == 0);
291+
__CPROVER_EIGEN_fixedbvt __trace_K0 = _controller.K[0][0];
292+
__CPROVER_EIGEN_fixedbvt __trace_K1 = _controller.K[0][1];
293+
__CPROVER_EIGEN_fixedbvt __trace_K2 = _controller.K[0][2];
294+
__CPROVER_EIGEN_fixedbvt counterexample_var;
295+
__CPROVER_assume(0.0 <= counterexample_var && counterexample_var <= 10.0);
296+
__CPROVER_assert(__trace_K0 > counterexample_var, "");
297+
//__CPROVER_assert(0 == 1, "");
298+
return 0;
299+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CORE
2+
eigen_charpoly.c
3+
--gcc --fixedbv --round-to-minus-inf --cegis-control --cegis-statistics --cegis-genetic --cegis-max-size 1 --cegis-show-iterations
4+
EXIT=0
5+
SIGNAL=0
6+
--
7+
warning: ignoring
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <ansi-c/expr2c.h>
2+
#include <util/namespace.h>
3+
4+
#include <cegis/control/learn/print_control_solution.h>
5+
6+
void print_control_array(messaget::mstreamt &os, const array_exprt &array,
7+
const char * name, const symbol_tablet &st)
8+
{
9+
const namespacet ns(st);
10+
const array_exprt::operandst &ops=array.operands();
11+
os << '<' << name << '>' << messaget::endl;
12+
for (const array_exprt::operandst::value_type &value : ops)
13+
os << "<item>" << expr2c(value, ns) << "</item>" << messaget::endl;
14+
os << "</" << name << '>' << messaget::endl;
15+
os << '<' << name << "_size>" << ops.size();
16+
os << "</" << name << "_size>" << messaget::endl;
17+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*******************************************************************
2+
3+
Module: Counterexample-Guided Inductive Synthesis
4+
5+
Author: Daniel Kroening, [email protected]
6+
Pascal Kesseli, [email protected]
7+
8+
\*******************************************************************/
9+
10+
#ifndef CEGIS_CONTROL_LEARN_PRINT_CONTROL_SOLUTION_H_
11+
#define CEGIS_CONTROL_LEARN_PRINT_CONTROL_SOLUTION_H_
12+
13+
#include <util/message.h>
14+
#include <util/std_expr.h>
15+
#include <util/symbol_table.h>
16+
17+
/**
18+
* @brief
19+
*
20+
* @details
21+
*
22+
* @param os
23+
* @param array
24+
* @param name
25+
* @param st
26+
*/
27+
void print_control_array(
28+
messaget::mstreamt &os,
29+
const array_exprt &array,
30+
const char * name,
31+
const symbol_tablet &st);
32+
33+
#endif /* CEGIS_CONTROL_LEARN_PRINT_CONTROL_SOLUTION_H_ */

0 commit comments

Comments
 (0)