TPCCLIB
Loading...
Searching...
No Matches
constraints.c File Reference

Check nonlinear fitting parameters against constraints. More...

#include "tpcclibConfig.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include "tpcextensions.h"
#include "tpcrand.h"
#include "tpcnlopt.h"

Go to the source code of this file.

Functions

unsigned int nloptCheckParameters (unsigned int n, double *pLower, double *pUpper, double *p, double *pAccept, double *penalty)
 Check that model parameters are within given limits.
unsigned int nloptForceLimits (unsigned int n, double *pLower, double *pUpper, double *p)
 Enforce the model parameters within given limits.

Detailed Description

Check nonlinear fitting parameters against constraints.

Definition in file constraints.c.

Function Documentation

◆ nloptCheckParameters()

unsigned int nloptCheckParameters ( unsigned int n,
double * pLower,
double * pUpper,
double * p,
double * pAccept,
double * penalty )

Check that model parameters are within given limits.

If one or more parameter(s) are outside of limits, then a penalty factor is optionally computed. Optionally, this function can make a list parameters that are inside the constraints.

Returns
Return the number of parameters that are inside or at the limits; 0 in case of error or if all are outside of limits.
Author
Vesa Oikonen
See also
nloptForceLimits, aicSS
Parameters
nNr of parameters.
pLowerLower limits (array of length par_nr).
pUpperUpper limits (array of length par_nr).
pParameters to test (array of length par_nr).
pAcceptPointer to corrected parameters (array of length par_nr); NULL if not needed.
penaltyPointer to variable in which the possible penalty factor will be written; 1.0 if no penalty was found is necessary, otherwise >1. Set to NULL if not needed.

Definition at line 30 of file constraints.c.

44 {
45 unsigned int pi, nAccept=0;
46 double range;
47
48 if(penalty!=NULL) *penalty=1.0;
49 if(n<1 || p==NULL || pLower==NULL || pUpper==NULL)
50 return(nAccept);
51 for(pi=0; pi<n; pi++) {
52 range=pUpper[pi]-pLower[pi]; if(range<1.0E-30) range=1.0;
53 if(p[pi]<pLower[pi]) {
54 if(pAccept!=NULL) pAccept[pi]=pLower[pi];
55 if(penalty!=NULL) *penalty += (pLower[pi]-p[pi])/range;
56 } else if(p[pi]>pUpper[pi]) {
57 if(pAccept!=NULL) pAccept[pi]=pUpper[pi];
58 if(penalty!=NULL) *penalty += (p[pi]-pUpper[pi])/range;
59 } else {
60 if(pAccept!=NULL) pAccept[pi]=p[pi];
61 nAccept++;
62 }
63 }
64 return nAccept;
65}

◆ nloptForceLimits()

unsigned int nloptForceLimits ( unsigned int n,
double * pLower,
double * pUpper,
double * p )

Enforce the model parameters within given limits.

Returns
Return the number of parameters that are inside or at the limits; 0 in case of error or if all are outside of limits.
Author
Vesa Oikonen
See also
nloptCheckParameters
Parameters
nNumber of parameters.
pLowerLower limits (array of length par_nr).
pUpperUpper limits (array of length par_nr).
pParameters to enforce inside limits (array of length par_nr).

Definition at line 76 of file constraints.c.

85 {
86 unsigned int pi, nAccept=0;
87
88 if(n<1 || p==NULL || pLower==NULL || pUpper==NULL) return(nAccept);
89 for(pi=0; pi<n; pi++) {
90 if(!(p[pi]>=pLower[pi])) {
91 p[pi]=pLower[pi];
92 } else if(!(p[pi]<=pUpper[pi])) {
93 p[pi]=pUpper[pi];
94 } else {
95 nAccept++;
96 }
97 }
98 return(nAccept);
99}

Referenced by nloptSimplex().