Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
TP Optimisation
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
stefano.cirieco
TP Optimisation
Commits
2f4c5d55
Commit
2f4c5d55
authored
3 years ago
by
stefano.cirieco
Browse files
Options
Downloads
Patches
Plain Diff
Ajout de commentaires
parent
fd7386f5
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
main.c
+41
-17
41 additions, 17 deletions
main.c
opti_num.c
+3
-2
3 additions, 2 deletions
opti_num.c
with
44 additions
and
19 deletions
main.c
+
41
−
17
View file @
2f4c5d55
...
...
@@ -16,24 +16,32 @@ int main() {
srand
(
time
(
0
));
int
N
=
NB_G
*
200
;
// NB_G = Nombre de groupe
int
N
=
NB_G
*
3000
;
vec
*
points
=
malloc
(
sizeof
(
vec
)
*
N
);
// Génération de la fonction linéaire
double
c
=
randNum
(),
d
=
randNum
();
double
r
;
double
r
,
amp
=
10
.
0
;
for
(
int
i
=
0
;
i
<
N
;
i
++
)
{
vec
tmp
;
// Préimage aléatoire
tmp
.
x
=
randNum
();
r
=
randNum
()
/
10
.
0
;
r
=
randNum
()
/
amp
-
1
/
(
2
*
amp
)
;
// Image de tmp.x avec le décalage r
tmp
.
y
=
c
*
tmp
.
x
+
d
+
r
;
points
[
i
]
=
tmp
;
}
// Génération des indexes aléatoire pour remplir les groupes
int
*
rnd_indexes
=
rand_order_indexes
(
N
);
// Répartion aléatoire des points grâce aux indexes aléatoire
vec
**
groups
=
cut_table
(
rnd_indexes
,
N
,
points
);
vec
*
ab
=
malloc
(
sizeof
(
vec
)
*
NB_G
);
...
...
@@ -41,6 +49,9 @@ int main() {
ab
[
i
].
x
=
randNum
();
ab
[
i
].
y
=
randNum
();
/* other = l'autre groupe modèle
* to_test = le groupe test
*/
int
other
=
i
+
1
,
to_test
=
i
-
1
;
if
(
other
>=
NB_G
)
{
other
=
0
;
...
...
@@ -48,27 +59,35 @@ int main() {
to_test
=
NB_G
-
1
;
}
// Modèle
minimiser
(
groups
[
i
],
groups
[
other
],
N
/
NB_G
,
1e-6
,
&
ab
[
i
]);
printf
(
"model: a: %g, b: %g
\n
"
,
ab
[
i
].
x
,
ab
[
i
].
y
);
// Test
vec
test_ab
;
calc_ab
(
groups
[
to_test
],
NB_G
,
&
test_ab
);
printf
(
"test: a: %g, b: %g
\n\n
"
,
test_ab
.
x
,
test_ab
.
y
);
calc_ab
(
groups
[
to_test
],
N
/
NB_G
,
&
test_ab
);
printf
(
"test: a: %g, b: %g
\n
"
,
test_ab
.
x
,
test_ab
.
y
);
printf
(
"Marge d'erreur: %g, %g
\n\n
"
,
test_ab
.
x
-
ab
[
i
].
x
,
test_ab
.
y
-
ab
[
i
].
y
);
}
//minimiser(points, N, 0.001, &ab);
printf
(
"c: %g, d: %g
\n
"
,
c
,
d
);
printf
(
"c: %g, d: %g
\n
"
,
c
,
d
);
/* Écriture des groupes dans des fichiers texte */
for
(
int
i
=
0
;
i
<
NB_G
;
i
++
)
{
char
fname
[
1
1
];
char
fname
[
1
0
];
sprintf
(
fname
,
"G%d.txt"
,
i
+
1
);
write_to_file
(
groups
[
i
],
N
/
NB_G
,
fname
);
}
// Écriture des a et b dans un fichier texte
write_to_file
(
ab
,
NB_G
,
"ab.txt"
);
system
(
"python3 graph.py"
);
/* Free des tableaux */
free
(
ab
);
free
(
points
);
free
(
rnd_indexes
);
...
...
@@ -81,40 +100,45 @@ int main() {
return
EXIT_SUCCESS
;
}
/* Génère un nombre aléatoire entre 0 et 1 */
double
randNum
()
{
return
rand
()
/
(
double
)
RAND_MAX
;
}
/* Répartie N points, dans NB_G groupes avec des indexes données
* Retourne un tableau avec NB_G tableau de points
*/
vec
**
cut_table
(
int
*
rand_indexes
,
int
N
,
vec
*
points
)
{
vec
**
groups
=
malloc
(
sizeof
(
vec
*
)
*
NB_G
);
int
frac
=
N
/
NB_G
;
for
(
int
i
=
0
;
i
<
NB_G
;
i
++
)
{
groups
[
i
]
=
malloc
(
sizeof
(
vec
)
*
frac
);
}
for
(
int
i
=
0
;
i
<
N
;
i
++
)
{
if
(
i
%
frac
==
0
)
{
groups
[
i
/
frac
]
=
malloc
(
sizeof
(
vec
)
*
frac
);
}
groups
[
i
/
frac
][
i
%
frac
]
=
points
[
rand_indexes
[
i
]];
}
return
groups
;
}
/* Génère un tableau d'indexes ordonés aléatoirement */
int
*
rand_order_indexes
(
int
N
)
{
int
*
indexes
=
calloc
(
0
,
sizeof
(
int
)
*
N
);
for
(
int
i
=
1
;
i
<
N
;
i
++
)
{
int
rnd_index
=
rand
()
%
N
;
while
(
indexes
[
rnd_index
]
!=
0
)
{
int
rnd_index
;
do
{
rnd_index
=
rand
()
%
N
;
}
}
while
(
indexes
[
rnd_index
]
!=
0
);
indexes
[
rnd_index
]
=
i
;
}
return
indexes
;
}
/* Écrit un tableau de points dans un fichier */
void
write_to_file
(
vec
*
points
,
int
N
,
char
*
filename
)
{
FILE
*
fp
=
fopen
(
filename
,
"w"
);
...
...
This diff is collapsed.
Click to expand it.
opti_num.c
+
3
−
2
View file @
2f4c5d55
...
...
@@ -6,6 +6,7 @@
void
minimiser
(
vec
*
vecs1
,
vec
*
vecs2
,
int
N
,
double
lambda
,
vec
*
ab
)
{
double
sum_x
=
0
,
sum_xx
=
0
,
sum_xy
=
0
,
sum_y
=
0
;
/* Calculer les sommes */
for
(
int
i
=
0
;
i
<
2
*
N
;
i
++
)
{
if
(
i
<
N
)
{
sum_x
+=
vecs1
[
i
].
x
;
...
...
@@ -45,7 +46,7 @@ void calc_ab(vec *points, int N, vec *ab) {
sum_y
+=
points
[
i
].
y
;
}
ab
->
x
=
(
sum_
y
*
sum_
x
-
N
*
sum_xy
)
/
(
sum_x
*
sum_x
-
N
*
sum_xx
);
ab
->
x
=
(
sum_
x
*
sum_
y
-
N
*
sum_xy
)
/
(
sum_x
*
sum_x
-
N
*
sum_xx
);
ab
->
y
=
(
sum_y
-
ab
->
x
*
sum_x
)
/
N
;
ab
->
y
=
(
sum_
x
y
-
ab
->
x
*
sum_x
)
/
N
;
}
\ No newline at end of file
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