comp.lang.ada
 help / color / mirror / Atom feed
* Function definitions - with clarification
@ 2014-06-22 11:34 montgrimpulo
  2014-06-22 12:04 ` Simon Clubley
                   ` (8 more replies)
  0 siblings, 9 replies; 20+ messages in thread
From: montgrimpulo @ 2014-06-22 11:34 UTC (permalink / raw)


Here is another try to describe my problem. 

I want to conduct a genetic search-program. There is a function F (the objective function), 
and a function G (the constraint function) which I want to define only at runtime. The search-program handles individuals within a population. The size of an individual as well as the size of the population is dynamic and is known at runtime. 

There is a proposed solution to define (in search.ads)

...
generic 
with function F (V : Individual) return Float; 
with function G (V : Individual; M : Positive) return Float; 
procedure Search (V : Individual); 

which seems to be an appropriate solution for that part. 

The search program handles individuals from a population. 

type x_array is array (positive range <>) of Float; 
type y_array is array (positive range <>) of Integer; 
type z_array is array (positive range <>) of Boolean; 

type Individual (P, Q, R) is record 
X : x_array (0..P); 
Y : y_array (0..Q); 
Z : z_array (0..R); 
end record; 

P,Q and R are only known at runtime. 

A population has a number (Popsize) of individuals (definition see above), which is also only known at runtime. 

Due to some reading, I learned that dynamic arrays in Ada 

- can be declared in blocks, meaning no inheritance 
- by limits passed as parameters in subprograms 
- by use of linked lists 
- by use of containers ? 
- by use of discriminated records ? 

Using  the proposed record type from above, 
my search-program may look like: (in search.adb)

procedure Search (V : Individual) is 

P : Natural := V.P; 
Q : Natural := V.Q; 
R : Natural := V.R; 
Vi : Individual := V; 


-- type population is array (1 .. Popsize) of Individual; 
-- gives: unconstrained element in array declaration 

... 
begin 
-- fill Vi with distinct values and put it into an (array?, container ?, List ?, whatever) 
-- to get a population with size Popsize
-- work on all individuals within that population 
... 
end Search; 

What I still need is a definition for the population (a collection of Individuals) of size Popsize, which is only defined at runtime.

I want to use my search program for a specific objective- and for a specific constraint-function with a certain size for an individual and a certain size of population. If I do not find a solution then I want to change some elements, eg. size of the population, size of an individual, or to try modified objective- or constraint-functions. 

After I have found a solution or not, I want to use the same search program with a new and different objective- and a new and different constraint function with a different size of an individual and  different population size. 



^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: Function definitions - with clarification
  2014-06-22 11:34 Function definitions - with clarification montgrimpulo
@ 2014-06-22 12:04 ` Simon Clubley
  2014-06-22 14:25 ` montgrimpulo
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 20+ messages in thread
From: Simon Clubley @ 2014-06-22 12:04 UTC (permalink / raw)


On 2014-06-22, montgrimpulo <aghte@hotlinemail.com> wrote:
> Here is another try to describe my problem. 
>
> I want to conduct a genetic search-program. There is a function F (the objective function), 
> and a function G (the constraint function) which I want to define only at runtime. The search-program handles individuals within a population. The size of an individual as well as the size of the population is dynamic and is known at runtime. 
>

Have you read the answers (and questions) we have posted ?

Did they not answer your questions or did you not understand the
responses you received ?

Before we can help you any further, I think you need to explain why
the responses posted so far don't help you.

Is this part of a student assignment or is this something you are
investigating on your own ?

I am wondering if we are giving you answers that you don't yet
understand because you have not yet covered this material in class.

Simon.

-- 
Simon Clubley, clubley@remove_me.eisner.decus.org-Earth.UFP
Microsoft: Bringing you 1980s technology to a 21st century world

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: Function definitions - with clarification
  2014-06-22 11:34 Function definitions - with clarification montgrimpulo
  2014-06-22 12:04 ` Simon Clubley
@ 2014-06-22 14:25 ` montgrimpulo
  2014-06-22 15:30 ` Georg Bauhaus
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 20+ messages in thread
From: montgrimpulo @ 2014-06-22 14:25 UTC (permalink / raw)


Simon Clubley wrote:

> Have you read the answers (and questions) we have posted ? 

Yes.

> Did they not answer your questions or did you not understand the 
> responses you received ?

Same question to you in return.

> Before we can help you any further, I think you need to explain why 
> the responses posted so far don't help you.

Am I talking to a robot program like "Eliza" ? 

> Is this part of a student assignment or is this something you are 
> investigating on your own ?

Irrelevant question 

> I am wondering if we are giving you answers that you don't yet 
> understand because you have not yet covered this material in class.

Irrelevant question.

"you" means here some people in the rest of the group.

"You" seem to prefer to give answers to questions which I have never asked.

Furthermore, "you" seem to deliberately search for those of my statements which
can be misinterpreted, and show no phantasy to tackle the core of my questions.

You can assume that everything you wrote is understood. I did not explicitly instantiated
the procedure search, as I want to do it later in a different file than search.adb. However, there are still
open questions, where your answer would show how you would have solved the problem
in your own understanding.

> Simon. 


On Sunday, June 22, 2014 1:34:09 PM UTC+2, montgrimpulo wrote:
> Here is another try to describe my problem. 
> 
> 
> 
> I want to conduct a genetic search-program. There is a function F (the objective function), 
> 
> and a function G (the constraint function) which I want to define only at runtime. The search-program handles individuals within a population. The size of an individual as well as the size of the population is dynamic and is known at runtime. 
> 
> 
> 
> There is a proposed solution to define (in search.ads)
> 
> 
> 
> ...
> 
> generic 
> 
> with function F (V : Individual) return Float; 
> 
> with function G (V : Individual; M : Positive) return Float; 
> 
> procedure Search (V : Individual); 
> 
> 
> 
> which seems to be an appropriate solution for that part. 
> 
> 
> 
> The search program handles individuals from a population. 
> 
> 
> 
> type x_array is array (positive range <>) of Float; 
> 
> type y_array is array (positive range <>) of Integer; 
> 
> type z_array is array (positive range <>) of Boolean; 
> 
> 
> 
> type Individual (P, Q, R : Natural) is record 
> 
> X : x_array (0..P); 
> 
> Y : y_array (0..Q); 
> 
> Z : z_array (0..R); 
> 
> end record; 
> 
> 
> 
> P,Q and R are only known at runtime. 
> 
> 
> 
> A population has a number (Popsize) of individuals (definition see above), which is also only known at runtime. 
> 
> 
> 
> Due to some reading, I learned that dynamic arrays in Ada 
> 
> 
> 
> - can be declared in blocks, meaning no inheritance 
> 
> - by limits passed as parameters in subprograms 
> 
> - by use of linked lists 
> 
> - by use of containers ? 
> 
> - by use of discriminated records ? 
> 
> 
> 
> Using  the proposed record type from above, 
> 
> my search-program may look like: (in search.adb)
> 
> 
> 
> procedure Search (V : Individual) is 
> 
> 
> 
> P : Natural := V.P; 
> 
> Q : Natural := V.Q; 
> 
> R : Natural := V.R; 
> 
> Vi : Individual := V; 
> 
> 
> 
> 
> 
> -- type population is array (1 .. Popsize) of Individual; 
> 
> -- gives: unconstrained element in array declaration 
> 
> 
> 
> ... 
> 
> begin 
> 
> -- fill Vi with distinct values and put it into an (array?, container ?, List ?, whatever) 
> 
> -- to get a population with size Popsize
> 
> -- work on all individuals within that population 
> 
> ... 
> 
> end Search; 
> 
> 
> 
> What I still need is a definition for the population (a collection of Individuals) of size Popsize, which is only defined at runtime.
> 
> 
> 
> I want to use my search program for a specific objective- and for a specific constraint-function with a certain size for an individual and a certain size of population. If I do not find a solution then I want to change some elements, eg. size of the population, size of an individual, or to try modified objective- or constraint-functions. 
> 
> 
> 
> After I have found a solution or not, I want to use the same search program with a new and different objective- and a new and different constraint function with a different size of an individual and  different population size.



^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: Function definitions - with clarification
  2014-06-22 11:34 Function definitions - with clarification montgrimpulo
  2014-06-22 12:04 ` Simon Clubley
  2014-06-22 14:25 ` montgrimpulo
@ 2014-06-22 15:30 ` Georg Bauhaus
  2014-06-22 17:29 ` montgrimpulo
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 20+ messages in thread
From: Georg Bauhaus @ 2014-06-22 15:30 UTC (permalink / raw)


On 22/06/14 13:34, montgrimpulo wrote:
> -- type population is array (1 .. Popsize) of Individual;
> -- gives: unconstrained element in array declaration

This means that type Individual is a record type that has
discriminants without default values for the discriminants.

The compiler has no way of knowing in advance the size of record
objects, since the size may differ per object. (Given that the record
definition can have variant parts whose structure and size will
depend on the discriminants, the absence of default values leaves
this open until actual objects are declared, making the objects'
size and structure unknown at the point of the array declaration.)

Therefore, an array of objects of type Individual would have
unconstrained elements (of type Individual, of unknown size
and structure, possibly varying), which arrays may not have.




^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: Function definitions - with clarification
  2014-06-22 11:34 Function definitions - with clarification montgrimpulo
                   ` (2 preceding siblings ...)
  2014-06-22 15:30 ` Georg Bauhaus
@ 2014-06-22 17:29 ` montgrimpulo
  2014-06-22 17:45 ` Shark8
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 20+ messages in thread
From: montgrimpulo @ 2014-06-22 17:29 UTC (permalink / raw)


...
> -- type population is array (1 .. Popsize) of Individual; 
> -- gives: unconstrained element in array declaration 

Georg Bauhaus wrote

" this means that type individual is a record that has
discriminants without default values ...
...
"
Sorry, this was not the question. The implicit question (also
put later in my text) was which alternatives do I have, 
as I want to finish programming my complete system 
first without default values, and specify then the size 
before running the system.

On Sunday, June 22, 2014 1:34:09 PM UTC+2, montgrimpulo wrote:
> Here is another try to describe my problem. 
> 
> 
> 
> I want to conduct a genetic search-program. There is a function F (the objective function), 
> 
> and a function G (the constraint function) which I want to define only at runtime. The search-program handles individuals within a population. The size of an individual as well as the size of the population is dynamic and is known at runtime. 
> 
> 
> 
> There is a proposed solution to define (in search.ads)
> 
> 
> 
> ...
> 
> generic 
> 
> with function F (V : Individual) return Float; 
> 
> with function G (V : Individual; M : Positive) return Float; 
> 
> procedure Search (V : Individual); 
> 
> 
> 
> which seems to be an appropriate solution for that part. 
> 
> 
> 
> The search program handles individuals from a population. 
> 
> 
> 
> type x_array is array (positive range <>) of Float; 
> 
> type y_array is array (positive range <>) of Integer; 
> 
> type z_array is array (positive range <>) of Boolean; 
> 
> 
> 
> type Individual (P, Q, R) is record 
> 
> X : x_array (0..P); 
> 
> Y : y_array (0..Q); 
> 
> Z : z_array (0..R); 
> 
> end record; 
> 
> 
> 
> P,Q and R are only known at runtime. 
> 
> 
> 
> A population has a number (Popsize) of individuals (definition see above), which is also only known at runtime. 
> 
> 
> 
> Due to some reading, I learned that dynamic arrays in Ada 
> 
> 
> 
> - can be declared in blocks, meaning no inheritance 
> 
> - by limits passed as parameters in subprograms 
> 
> - by use of linked lists 
> 
> - by use of containers ? 
> 
> - by use of discriminated records ? 
> 
> 
> 
> Using  the proposed record type from above, 
> 
> my search-program may look like: (in search.adb)
> 
> 
> 
> procedure Search (V : Individual) is 
> 
> 
> 
> P : Natural := V.P; 
> 
> Q : Natural := V.Q; 
> 
> R : Natural := V.R; 
> 
> Vi : Individual := V; 
> 
> 
> 
> 
> 
> -- type population is array (1 .. Popsize) of Individual; 
> 
> -- gives: unconstrained element in array declaration 
> 
> 
> 
> ... 
> 
> begin 
> 
> -- fill Vi with distinct values and put it into an (array?, container ?, List ?, whatever) 
> 
> -- to get a population with size Popsize
> 
> -- work on all individuals within that population 
> 
> ... 
> 
> end Search; 
> 
> 
> 
> What I still need is a definition for the population (a collection of Individuals) of size Popsize, which is only defined at runtime.
> 
> I want to use my search program for a specific objective- and for a specific constraint-function with a certain size for an individual and a certain size of population. If I do not find a solution then I want to change some elements, eg. size of the population, size of an individual, or to try modified objective- or constraint-functions. 
> 
> After I have found a solution or not, I want to use the same search program with a new and different objective- and a new and different constraint function with a different size of an individual and  different population size.

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: Function definitions - with clarification
  2014-06-22 11:34 Function definitions - with clarification montgrimpulo
                   ` (3 preceding siblings ...)
  2014-06-22 17:29 ` montgrimpulo
@ 2014-06-22 17:45 ` Shark8
  2014-06-22 18:03   ` montgrimpulo
  2014-06-23 16:26 ` Adam Beneschan
                   ` (3 subsequent siblings)
  8 siblings, 1 reply; 20+ messages in thread
From: Shark8 @ 2014-06-22 17:45 UTC (permalink / raw)


On 22-Jun-14 05:34, montgrimpulo wrote:
> type Individual (P, Q, R) is record
> X : x_array (0..P);
> Y : y_array (0..Q);
> Z : z_array (0..R);
> end record;

The above won't work, your discriminants have no type-indication -- how 
is the compiler supposed to know what P, Q, and R are in the context of 
the type?

It;s a lot like nested scopes the following won't work because the new 
definition of X has no type;

Procedure F(X: in Integer) is
   Procedure G is
     X; -- Variable declaration missing type.
   begin
    null;
   end G;
begin
   null;
end F;

You want that to be like this:

type Individual (P, Q, R : Positive) is record
X : x_array (0..P);
Y : y_array (0..Q);
Z : z_array (0..R);
end record;

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: Function definitions - with clarification
  2014-06-22 17:45 ` Shark8
@ 2014-06-22 18:03   ` montgrimpulo
  2014-06-22 18:45     ` Simon Clubley
  2014-06-22 19:55     ` Shark8
  0 siblings, 2 replies; 20+ messages in thread
From: montgrimpulo @ 2014-06-22 18:03 UTC (permalink / raw)


Direct reply to Shark8

What is the point of saying "the above won't work" when it was
obviously forgotten to indicate a type ? What is the point of changing my
function F into a procedure F with an embedded procedure G ?
This does not make sense at all.
 What is then left as your contribution ?

On Sunday, June 22, 2014 7:45:40 PM UTC+2, Shark8 wrote:
> On 22-Jun-14 05:34, montgrimpulo wrote:
> 
> > type Individual (P, Q, R) is record
> 
> > X : x_array (0..P);
> 
> > Y : y_array (0..Q);
> 
> > Z : z_array (0..R);
> 
> > end record;
> 
> 
> 
> The above won't work, your discriminants have no type-indication -- how 
> 
> is the compiler supposed to know what P, Q, and R are in the context of 
> 
> the type?
> 
> 
> 
> It;s a lot like nested scopes the following won't work because the new 
> 
> definition of X has no type;
> 
> 
> 
> Procedure F(X: in Integer) is
> 
>    Procedure G is
> 
>      X; -- Variable declaration missing type.
> 
>    begin
> 
>     null;
> 
>    end G;
> 
> begin
> 
>    null;
> 
> end F;
> 
> 
> 
> You want that to be like this:
> 
> 
> 
> type Individual (P, Q, R : Positive) is record
> 
> X : x_array (0..P);
> 
> Y : y_array (0..Q);
> 
> Z : z_array (0..R);
> 
> end record;

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: Function definitions - with clarification
  2014-06-22 18:03   ` montgrimpulo
@ 2014-06-22 18:45     ` Simon Clubley
  2014-06-22 19:28       ` montgrimpulo
  2014-06-22 21:17       ` Jeffrey Carter
  2014-06-22 19:55     ` Shark8
  1 sibling, 2 replies; 20+ messages in thread
From: Simon Clubley @ 2014-06-22 18:45 UTC (permalink / raw)


On 2014-06-22, montgrimpulo <aghte@hotlinemail.com> wrote:
> Direct reply to Shark8
>
> What is the point of saying "the above won't work" when it was
> obviously forgotten to indicate a type ? What is the point of changing my
> function F into a procedure F with an embedded procedure G ?
> This does not make sense at all.
>  What is then left as your contribution ?
>

You need to lose the attitude.

There are a number of people here who are trying to help you but
both your problem description and your level of Ada knowledge
are unclear.

Shark8's response was a good faith effort to correct you on Ada
syntax because it's unclear what your level of Ada knowledge is.
My questions, along with the questions others are asking you, are
good faith attempts to try and help you clarify the problem so
someone can offer more precise guidance.

Simon.

-- 
Simon Clubley, clubley@remove_me.eisner.decus.org-Earth.UFP
Microsoft: Bringing you 1980s technology to a 21st century world


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: Function definitions - with clarification
  2014-06-22 18:45     ` Simon Clubley
@ 2014-06-22 19:28       ` montgrimpulo
  2014-06-22 21:04         ` Simon Wright
  2014-06-22 21:17       ` Jeffrey Carter
  1 sibling, 1 reply; 20+ messages in thread
From: montgrimpulo @ 2014-06-22 19:28 UTC (permalink / raw)


Direct reply to Simon Clubley:

I think you have to live with my attitude. Why should I always repeat myself
despite my problem description is now stable. Concerning the level of Ada knowledge
it is your level which governs the communication. An Example: If people
talk to a foreigner who apparently does not speak their mother tongue
very well, they tend to fall into something like "baby-talk". However this
is no help in making progress.

Concerning my questions: I expected something like: Under my assumptions
of ... my suggestion for a solution looks like ... .

Then I would be able to see how I was understood, and to learn what is possible
in Ada.
Btw. a prototype of a working system which does exactly what I want, was written
by me in Mathematica.
  
On Sunday, June 22, 2014 8:45:21 PM UTC+2, Simon Clubley wrote:
> On 2014-06-22, montgrimpulo <aghte@hotlinemail.com> wrote:
> 
> > Direct reply to Shark8
> 
> >
> 
> > What is the point of saying "the above won't work" when it was
> 
> > obviously forgotten to indicate a type ? What is the point of changing my
> 
> > function F into a procedure F with an embedded procedure G ?
> 
> > This does not make sense at all.
> 
> >  What is then left as your contribution ?
> 
> >
> 
> 
> 
> You need to lose the attitude.
> 
> 
> 
> There are a number of people here who are trying to help you but
> 
> both your problem description and your level of Ada knowledge
> 
> are unclear.
> 
> 
> 
> Shark8's response was a good faith effort to correct you on Ada
> 
> syntax because it's unclear what your level of Ada knowledge is.
> 
> My questions, along with the questions others are asking you, are
> 
> good faith attempts to try and help you clarify the problem so
> 
> someone can offer more precise guidance.
> 
> 
> 
> Simon.
> 
> 
> 
> -- 
> 
> Simon Clubley, clubley@remove_me.eisner.decus.org-Earth.UFP
> 
> Microsoft: Bringing you 1980s technology to a 21st century world



^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: Function definitions - with clarification
  2014-06-22 18:03   ` montgrimpulo
  2014-06-22 18:45     ` Simon Clubley
@ 2014-06-22 19:55     ` Shark8
  1 sibling, 0 replies; 20+ messages in thread
From: Shark8 @ 2014-06-22 19:55 UTC (permalink / raw)


On 22-Jun-14 12:03, montgrimpulo wrote:
> Direct reply to Shark8
>
> What is the point of saying "the above won't work" when it was
> obviously forgotten to indicate a type?

That's exactly the point.
If you're giving us incorrect stuff then it's hard to determine what 
your intent really is. This is opposed to either writing out psudo-code 
OR non-compiling code within otherwise syntactically-correct code [i.e 
"what's the syntax for..."].

IOW, the intent is a necessary, but not sufficient condition for others 
to understand/help.

To further clarify, there's the high-level intent of problem 
description, which you've given; but there's also the intent with 
respect as to how you are going to implement the solution. (As I pointed 
out earlier, you could have used generic-parameters on your solution OR 
discriminated record. Both are equally valid for a solution.)

> What is the point of changing my
> function F into a procedure F with an embedded procedure G ?

To show that there are different scopes and you cannot have a name infer 
types from outer scopes... which is what it really looked like you were 
trying to do by having the discriminants w/o type.

(It's also a bad idea for you to be relying on parameter/variable values 
in type/subtype definitions, until you really understand the implications.)

> This does not make sense at all.
> What is then left as your contribution?

If you don't want my help, you don't have to take it.


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: Function definitions - with clarification
  2014-06-22 19:28       ` montgrimpulo
@ 2014-06-22 21:04         ` Simon Wright
  0 siblings, 0 replies; 20+ messages in thread
From: Simon Wright @ 2014-06-22 21:04 UTC (permalink / raw)


montgrimpulo <aghte@hotlinemail.com> writes:

> Direct reply to Simon Clubley:
>
> I think you have to live with my attitude. Why should I always repeat
> myself despite my problem description is now stable. Concerning the
> level of Ada knowledge it is your level which governs the
> communication. An Example: If people talk to a foreigner who
> apparently does not speak their mother tongue very well, they tend to
> fall into something like "baby-talk". However this is no help in
> making progress.

No one here has to live with your attitude. You will very soon find
that you are talking to yourself.

> Concerning my questions: I expected something like: Under my
> assumptions of ... my suggestion for a solution looks like ... .

But we probably aren't interested in your problem, so why should we
spend our time guessing what it might be and inventing solutions to what
may not be your problem at all?

> Then I would be able to see how I was understood, and to learn what is
> possible in Ada.

Seems pretty clear that so far you are not understood.

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: Function definitions - with clarification
  2014-06-22 18:45     ` Simon Clubley
  2014-06-22 19:28       ` montgrimpulo
@ 2014-06-22 21:17       ` Jeffrey Carter
  1 sibling, 0 replies; 20+ messages in thread
From: Jeffrey Carter @ 2014-06-22 21:17 UTC (permalink / raw)


On 06/22/2014 11:45 AM, Simon Clubley wrote:
>
> You need to lose the attitude.
>
> There are a number of people here who are trying to help you but
> both your problem description and your level of Ada knowledge
> are unclear.

This is beginning to feel like a troll to me. If not actually a troll, probably 
someone who will never be able to actually use Ada.

Luckily, we have kill files.

-- 
Jeff Carter
"IMHO, Interfaces are worthless."
Randy Brukardt
117

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: Function definitions - with clarification
  2014-06-22 11:34 Function definitions - with clarification montgrimpulo
                   ` (4 preceding siblings ...)
  2014-06-22 17:45 ` Shark8
@ 2014-06-23 16:26 ` Adam Beneschan
  2014-06-29 15:31 ` cotswold
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 20+ messages in thread
From: Adam Beneschan @ 2014-06-23 16:26 UTC (permalink / raw)


On Sunday, June 22, 2014 4:34:09 AM UTC-7, montgrimpulo wrote:

> type Individual (P, Q, R) is record 
> X : x_array (0..P); 
> Y : y_array (0..Q); 
> Z : z_array (0..R); 
> end record; 

> -- type population is array (1 .. Popsize) of Individual; 
> -- gives: unconstrained element in array declaration 

If you are trying to set up an array of Individuals where the Individuals could all have different discriminants: you can't do that in Ada.  Sorry.  The reason is that to set up an array, the elements all have to be the same size, and an Individual(P=>3, Q=>5, R=>4) will have a different size than an Individual(P=>2, Q=>2, R=>1).  [That may not be true on all compilers.  But the language was designed so that it would work on compilers that do allocate different sizes for those different records.]

If this is what you want, you will have to start using access types.  I.e.

    type Individual_Acc is access Individual;
    type population is array (1 .. Popsize) of Individual_Acc;

Now, all the array elements are accesses (pointers), so they will have the same size.

If you want a Population where each Individual has the **same** discriminants, you could get it to work by putting discriminants on the array.  The problem is that you can't put discriminants on an array, but you can put them on a record containing the array:

    type Population (P, Q, R : Natural) is record
        Population_Data : array (1 .. Popsize) of Individual(P, Q, R);
    end record;

I haven't tested this but I *think* it will work.

                           -- Adam

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: Function definitions - with clarification
  2014-06-22 11:34 Function definitions - with clarification montgrimpulo
                   ` (5 preceding siblings ...)
  2014-06-23 16:26 ` Adam Beneschan
@ 2014-06-29 15:31 ` cotswold
  2014-06-29 19:20 ` montgrimpulo
  2014-06-30 13:23 ` montgrimpulo
  8 siblings, 0 replies; 20+ messages in thread
From: cotswold @ 2014-06-29 15:31 UTC (permalink / raw)


Is it possible that the OP does not understand (or has not attempted to understand)the meaning and purpose of "default" and "default value" in the context of Ada. 


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: Function definitions - with clarification
  2014-06-22 11:34 Function definitions - with clarification montgrimpulo
                   ` (6 preceding siblings ...)
  2014-06-29 15:31 ` cotswold
@ 2014-06-29 19:20 ` montgrimpulo
  2014-06-30  9:29   ` G.B.
  2014-06-30 13:23 ` montgrimpulo
  8 siblings, 1 reply; 20+ messages in thread
From: montgrimpulo @ 2014-06-29 19:20 UTC (permalink / raw)


I've tried to figure out how my problem could be programmed
using generic functions and access types. Any comments ?

package paradigm is
   type Individual;
   type Rvp is array (Positive range <>) of Long_Float;
   type Ivq is array (Positive range <>) of Long_Integer;
   type Bvr is array (Positive range <>) of Natural range 0 .. 1;
   type Lux is array (Positive range <>, Positive range <>) of Long_Float;
   type Luy is array (Positive range <>, Positive range <>) of Long_Integer;
   type Individual (P,Q,R : Natural) is record
      X : Rvp (1..P);
      Y : Ivq (1..Q);
      Z : Bvr (1..R);
   end record;

   generic
      with function F (V : Individual) return Long_Float;
      with function G (V : Individual; M : Natural) return Long_Float;
   procedure EA (P1, Q1, R1, Popsize : Natural);

end paradigm;

package body paradigm is

   procedure EA (P1,Q1,R1,Popsize : Natural) is

      type vi is access Individual;
      V : vi := new Individual (P=>P1,Q=>Q1,R=>R1);
      type population is array (1 .. Popsize) of vi; 
   begin
      V.Y(1) := 1;
   end EA;

end paradigm;


On Sunday, June 22, 2014 1:34:09 PM UTC+2, montgrimpulo wrote:
> Here is another try to describe my problem. 
> 
> 
> 
> I want to conduct a genetic search-program. There is a function F (the objective function), 
> 
> and a function G (the constraint function) which I want to define only at runtime. The search-program handles individuals within a population. The size of an individual as well as the size of the population is dynamic and is known at runtime. 
> 
> 
> 
> There is a proposed solution to define (in search.ads)
> 
> 
> 
> ...
> 
> generic 
> 
> with function F (V : Individual) return Float; 
> 
> with function G (V : Individual; M : Positive) return Float; 
> 
> procedure Search (V : Individual); 
> 
> 
> 
> which seems to be an appropriate solution for that part. 
> 
> 
> 
> The search program handles individuals from a population. 
> 
> 
> 
> type x_array is array (positive range <>) of Float; 
> 
> type y_array is array (positive range <>) of Integer; 
> 
> type z_array is array (positive range <>) of Boolean; 
> 
> 
> 
> type Individual (P, Q, R) is record 
> 
> X : x_array (0..P); 
> 
> Y : y_array (0..Q); 
> 
> Z : z_array (0..R); 
> 
> end record; 
> 
> 
> 
> P,Q and R are only known at runtime. 
> 
> 
> 
> A population has a number (Popsize) of individuals (definition see above), which is also only known at runtime. 
> 
> 
> 
> Due to some reading, I learned that dynamic arrays in Ada 
> 
> 
> 
> - can be declared in blocks, meaning no inheritance 
> 
> - by limits passed as parameters in subprograms 
> 
> - by use of linked lists 
> 
> - by use of containers ? 
> 
> - by use of discriminated records ? 
> 
> 
> 
> Using  the proposed record type from above, 
> 
> my search-program may look like: (in search.adb)
> 
> 
> 
> procedure Search (V : Individual) is 
> 
> 
> 
> P : Natural := V.P; 
> 
> Q : Natural := V.Q; 
> 
> R : Natural := V.R; 
> 
> Vi : Individual := V; 
> 
> 
> 
> 
> 
> -- type population is array (1 .. Popsize) of Individual; 
> 
> -- gives: unconstrained element in array declaration 
> 
> 
> 
> ... 
> 
> begin 
> 
> -- fill Vi with distinct values and put it into an (array?, container ?, List ?, whatever) 
> 
> -- to get a population with size Popsize
> 
> -- work on all individuals within that population 
> 
> ... 
> 
> end Search; 
> 
> 
> 
> What I still need is a definition for the population (a collection of Individuals) of size Popsize, which is only defined at runtime.
> 
> 
> 
> I want to use my search program for a specific objective- and for a specific constraint-function with a certain size for an individual and a certain size of population. If I do not find a solution then I want to change some elements, eg. size of the population, size of an individual, or to try modified objective- or constraint-functions. 
> 
> 
> 
> After I have found a solution or not, I want to use the same search program with a new and different objective- and a new and different constraint function with a different size of an individual and  different population size.

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: Function definitions - with clarification
  2014-06-29 19:20 ` montgrimpulo
@ 2014-06-30  9:29   ` G.B.
  2014-06-30 17:55     ` Shark8
  0 siblings, 1 reply; 20+ messages in thread
From: G.B. @ 2014-06-30  9:29 UTC (permalink / raw)


On 29.06.14 21:20, montgrimpulo wrote:
>        V : vi := new Individual (P=>P1,Q=>Q1,R=>R1);
>        type population is array (1 .. Popsize) of vi;

Regarding resource management, I'd now consider, e.g., deriving
type Individual from Finalization.Controlled. By overriding
the primitive operation Finalize you could then have the
memory of V deallocated automatically, when EA exits.

Since for vi, its type Individual is constrained in EA, and
if the sizes of the component arrays of Inidividual are never
so large as to approach (implementation defined) Natural'Last,
then type Population, defined in EA, could be an array of this
constrained subtype of Individual. This will save memory
management.

If knowledge of cardinalities is reflected in types of the
program, rather than simply using Positive etc, then more control
and more clarity of intent is added to the program, I think.
It adds "categorizing" the numbers according to purpose. You could
still depend on Positive, if needed:

   type κ is new Positive [range ...];

where κ expresses the intent of using a range of positive integers.

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: Function definitions - with clarification
  2014-06-22 11:34 Function definitions - with clarification montgrimpulo
                   ` (7 preceding siblings ...)
  2014-06-29 19:20 ` montgrimpulo
@ 2014-06-30 13:23 ` montgrimpulo
  8 siblings, 0 replies; 20+ messages in thread
From: montgrimpulo @ 2014-06-30 13:23 UTC (permalink / raw)


I have slightly changed the structure:

package paradigm is

   type Individual;
   type vi is access Individual;
   type Rvp is array (Positive range <>) of Long_Float;
   type Ivq is array (Positive range <>) of Long_Integer;
   type Bvr is array (Positive range <>) of Natural range 0 .. 1;
   type Lux is array (Positive range <>, Positive range <>) of Long_Float;
   type Luy is array (Positive range <>, Positive range <>) of Long_Integer;
   type Individual (P, Q, R : Natural) is record
      X : Rvp (1 .. P);
      Y : Ivq (1 .. Q);
      Z : Bvr (1 .. R);
   end record;
   
   generic
   with function F (V : vi) return Long_Float;
   with function G (V : vi; M : Natural) return Long_Float;
   procedure EA (P1, Q1, R1, M1, Popsize : Natural);

end paradigm;

package body paradigm is

   procedure EA (P1,Q1,R1,M1,Popsize : Natural) is
      V : vi := new Individual (P=>P1,Q=>Q1,R=>R1);
      type population is array (1 .. Popsize) of vi;
      Fr, Gr : Long_Float;
   begin
      V.Y(1) := 1;
      Fr := F(V);
      Gr := G(V,M1);
   end EA;

end paradigm;

with paradigm;    use paradigm;
with Ada.Text_IO; use Ada.Text_IO;

procedure paratest is
   P2, Q2, R2, Popsize2, M2 : Natural;
   function F (V : vi) return Long_Float is
      Flf : Long_Float := 1.0;
   begin
      Put_Line ("in F");
      return Flf;
   end F;
   function G (V : vi; M : Natural) return Long_Float is
      Glf : Long_Float := 0.0;
   begin
      Put_Line ("in G");
      return Glf;
   end G;
   procedure EA1 is new EA (F, G);
begin
   P2       := 0;
   Q2       := 1;
   R2       := 0;
   Popsize2 := 20;
   M2       := 1;
   EA1 (P2, Q2, R2, M2, Popsize2);
end paratest;

Hopefully it will work. Any comments ?

On Sunday, June 22, 2014 1:34:09 PM UTC+2, montgrimpulo wrote:
> Here is another try to describe my problem. 
> 
> 
> 
> I want to conduct a genetic search-program. There is a function F (the objective function), 
> 
> and a function G (the constraint function) which I want to define only at runtime. The search-program handles individuals within a population. The size of an individual as well as the size of the population is dynamic and is known at runtime. 
> 
> 
> 
> There is a proposed solution to define (in search.ads)
> 
> 
> 
> ...
> 
> generic 
> 
> with function F (V : Individual) return Float; 
> 
> with function G (V : Individual; M : Positive) return Float; 
> 
> procedure Search (V : Individual); 
> 
> 
> 
> which seems to be an appropriate solution for that part. 
> 
> 
> 
> The search program handles individuals from a population. 
> 
> 
> 
> type x_array is array (positive range <>) of Float; 
> 
> type y_array is array (positive range <>) of Integer; 
> 
> type z_array is array (positive range <>) of Boolean; 
> 
> 
> 
> type Individual (P, Q, R) is record 
> 
> X : x_array (0..P); 
> 
> Y : y_array (0..Q); 
> 
> Z : z_array (0..R); 
> 
> end record; 
> 
> 
> 
> P,Q and R are only known at runtime. 
> 
> 
> 
> A population has a number (Popsize) of individuals (definition see above), which is also only known at runtime. 
> 
> 
> 
> Due to some reading, I learned that dynamic arrays in Ada 
> 
> 
> 
> - can be declared in blocks, meaning no inheritance 
> 
> - by limits passed as parameters in subprograms 
> 
> - by use of linked lists 
> 
> - by use of containers ? 
> 
> - by use of discriminated records ? 
> 
> 
> 
> Using  the proposed record type from above, 
> 
> my search-program may look like: (in search.adb)
> 
> 
> 
> procedure Search (V : Individual) is 
> 
> 
> 
> P : Natural := V.P; 
> 
> Q : Natural := V.Q; 
> 
> R : Natural := V.R; 
> 
> Vi : Individual := V; 
> 
> 
> 
> 
> ... 
> 
> begin 
> 
> -- fill Vi with distinct values and put it into an (array?, container ?, List ?, whatever) 
> 
> -- to get a population with size Popsize
> 
> -- work on all individuals within that population 
> 
> ... 
> 
> end Search; 
> 
> 
> 
> What I still need is a definition for the population (a collection of Individuals) of size Popsize, which is only defined at runtime.
> 
> 
> 
> I want to use my search program for a specific objective- and for a specific constraint-function with a certain size for an individual and a certain size of population. If I do not find a solution then I want to change some elements, eg. size of the population, size of an individual, or to try modified objective- or constraint-functions. 
> 
> 
> 
> After I have found a solution or not, I want to use the same search program with a new and different objective- and a new and different constraint function with a different size of an individual and  different population size.

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: Function definitions - with clarification
  2014-06-30  9:29   ` G.B.
@ 2014-06-30 17:55     ` Shark8
  2014-07-01  8:58       ` Georg Bauhaus
  0 siblings, 1 reply; 20+ messages in thread
From: Shark8 @ 2014-06-30 17:55 UTC (permalink / raw)


On 30-Jun-14 03:29, G.B. wrote:
>>    procedure EA (P1,Q1,R1,Popsize : Natural) is
>>
>>       type vi is access Individual;
>>       V : vi := new Individual (P=>P1,Q=>Q1,R=>R1);
>>       type population is array (1 .. Popsize) of vi;
>>    begin
>>       V.Y(1) := 1;
>>    end EA;

> Regarding resource management, I'd now consider, e.g., deriving
> type Individual from Finalization.Controlled. By overriding
> the primitive operation Finalize you could then have the
> memory of V deallocated automatically, when EA exits.

Isn't this unneeded?
Given that the access-type is local to the function, when the function 
goes out of scope [due returning] the access-type itself should as well, 
taking all the allocated values with it, right?


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: Function definitions - with clarification
  2014-06-30 17:55     ` Shark8
@ 2014-07-01  8:58       ` Georg Bauhaus
  2014-07-04  6:45         ` J-P. Rosen
  0 siblings, 1 reply; 20+ messages in thread
From: Georg Bauhaus @ 2014-07-01  8:58 UTC (permalink / raw)


On 30/06/14 19:55, Shark8 wrote:

> Given that the access-type is local to the function, when the function goes out of scope [due returning] the access-type itself should as well, taking all the allocated values with it, right?

The parts local to the function just become inaccessible,
but they exist, and they need to exist after return.

Consider, for example,

    type T0 is new Ada.Finalization.Controlled with record
       A: Character := '#';
    end record;

    function F return T0'Class
    is
       type T1 is new T0 with
          record
             N : Natural;
          end record;
    begin
       declare
          type Heap_Thing is access T1;
          Result : Heap_Thing;
       begin
          Result := new T1'(Controlled with A => '*', N => 42);
          return T0 (Result.all);
       end;
    end F;

If T0 has a component A : Character as indicated above,
then the following statement should print '*':

    Put (F.A);

Things will be more interesting in case F returns
an anonymous access T0'Class. (Seems a mine field, then,
at least if T0 is derived from Finalization.Controlled.)

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: Function definitions - with clarification
  2014-07-01  8:58       ` Georg Bauhaus
@ 2014-07-04  6:45         ` J-P. Rosen
  0 siblings, 0 replies; 20+ messages in thread
From: J-P. Rosen @ 2014-07-04  6:45 UTC (permalink / raw)


Le 01/07/2014 10:58, Georg Bauhaus a écrit :
> On 30/06/14 19:55, Shark8 wrote:

> The parts local to the function just become inaccessible,
> but they exist, and they need to exist after return.
> 
> Consider, for example,
> 
>    type T0 is new Ada.Finalization.Controlled with record
>       A: Character := '#';
>    end record;
> 
>    function F return T0'Class
>    is
>       type T1 is new T0 with
>          record
>             N : Natural;
>          end record;
>    begin
>       declare
>          type Heap_Thing is access T1;
>          Result : Heap_Thing;
>       begin
>          Result := new T1'(Controlled with A => '*', N => 42);
>          return T0 (Result.all);
>       end;
>    end F;
> 
> If T0 has a component A : Character as indicated above,
> then the following statement should print '*':
> 
>    Put (F.A);
> 
> Things will be more interesting in case F returns
> an anonymous access T0'Class. (Seems a mine field, then,
> at least if T0 is derived from Finalization.Controlled.)
> 

You can still free the locally allocated object, what you return is a
copy of the T0 part of Result.all.

Proof: try changing it to limited_control...
-- 
J-P. Rosen
Adalog
2 rue du Docteur Lombard, 92441 Issy-les-Moulineaux CEDEX
Tel: +33 1 45 29 21 52, Fax: +33 1 45 29 25 00
http://www.adalog.fr


^ permalink raw reply	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2014-07-04  6:45 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-06-22 11:34 Function definitions - with clarification montgrimpulo
2014-06-22 12:04 ` Simon Clubley
2014-06-22 14:25 ` montgrimpulo
2014-06-22 15:30 ` Georg Bauhaus
2014-06-22 17:29 ` montgrimpulo
2014-06-22 17:45 ` Shark8
2014-06-22 18:03   ` montgrimpulo
2014-06-22 18:45     ` Simon Clubley
2014-06-22 19:28       ` montgrimpulo
2014-06-22 21:04         ` Simon Wright
2014-06-22 21:17       ` Jeffrey Carter
2014-06-22 19:55     ` Shark8
2014-06-23 16:26 ` Adam Beneschan
2014-06-29 15:31 ` cotswold
2014-06-29 19:20 ` montgrimpulo
2014-06-30  9:29   ` G.B.
2014-06-30 17:55     ` Shark8
2014-07-01  8:58       ` Georg Bauhaus
2014-07-04  6:45         ` J-P. Rosen
2014-06-30 13:23 ` montgrimpulo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox