diff --git a/.claude/settings.local.json b/.claude/settings.local.json index 7557e0a..181442e 100644 --- a/.claude/settings.local.json +++ b/.claude/settings.local.json @@ -8,5 +8,9 @@ "Bash(dotnet build:*)", "Bash(dotnet test:*)" ] - } + }, + "enabledMcpjsonServers": [ + "aspire" + ], + "enableAllProjectMcpServers": true } diff --git a/.claude/skills/aspire/SKILL.md b/.claude/skills/aspire/SKILL.md new file mode 100644 index 0000000..79e41d6 --- /dev/null +++ b/.claude/skills/aspire/SKILL.md @@ -0,0 +1,93 @@ +--- +name: aspire +description: "Orchestrates Aspire distributed applications using the Aspire CLI for running, debugging, and managing distributed apps. USE FOR: aspire start, aspire stop, start aspire app, aspire describe, list aspire integrations, debug aspire issues, view aspire logs, add aspire resource, aspire dashboard, update aspire apphost. DO NOT USE FOR: non-Aspire .NET apps (use dotnet CLI), container-only deployments (use docker/podman), Azure deployment after local testing (use azure-deploy skill). INVOKES: Aspire CLI commands (aspire start, aspire describe, aspire otel logs, aspire docs search, aspire add), bash. FOR SINGLE OPERATIONS: Use Aspire CLI commands directly for quick resource status or doc lookups." +--- + +# Aspire Skill + +This repository uses Aspire to orchestrate its distributed application. Resources are defined in the AppHost project (`apphost.cs` or `apphost.ts`). + +## CLI command reference + +| Task | Command | +|---|---| +| Start the app | `aspire start` | +| Start isolated (worktrees) | `aspire start --isolated` | +| Restart the app | `aspire start` (stops previous automatically) | +| Wait for resource healthy | `aspire wait ` | +| Stop the app | `aspire stop` | +| List resources | `aspire describe` or `aspire resources` | +| Run resource command | `aspire resource ` | +| Start/stop/restart resource | `aspire resource start|stop|restart` | +| View console logs | `aspire logs [resource]` | +| View structured logs | `aspire otel logs [resource]` | +| View traces | `aspire otel traces [resource]` | +| Logs for a trace | `aspire otel logs --trace-id ` | +| Add an integration | `aspire add` | +| List running AppHosts | `aspire ps` | +| Update AppHost packages | `aspire update` | +| Search docs | `aspire docs search ` | +| Get doc page | `aspire docs get ` | +| List doc pages | `aspire docs list` | +| Environment diagnostics | `aspire doctor` | +| List resource MCP tools | `aspire mcp tools` | +| Call resource MCP tool | `aspire mcp call --input ` | + +Most commands support `--format Json` for machine-readable output. Use `--apphost ` to target a specific AppHost. + +## Key workflows + +### Running in agent environments + +Use `aspire start` to run the AppHost in the background. When working in a git worktree, use `--isolated` to avoid port conflicts and to prevent sharing user secrets or other local state with other running instances: + +```bash +aspire start --isolated +``` + +Use `aspire wait ` to block until a resource is healthy before interacting with it: + +```bash +aspire start --isolated +aspire wait myapi +``` + +Relaunching is safe — `aspire start` automatically stops any previous instance. Re-run `aspire start` whenever changes are made to the AppHost project. + +### Debugging issues + +Before making code changes, inspect the app state: + +1. `aspire describe` — check resource status +2. `aspire otel logs ` — view structured logs +3. `aspire logs ` — view console output +4. `aspire otel traces ` — view distributed traces + +### Adding integrations + +Use `aspire docs search` to find integration documentation, then `aspire docs get` to read the full guide. Use `aspire add` to add the integration package to the AppHost. + +After adding an integration, restart the app with `aspire start` for the new resource to take effect. + +### Using resource MCP tools + +Some resources expose MCP tools (e.g. `WithPostgresMcp()` adds SQL query tools). Discover and call them via CLI: + +```bash +aspire mcp tools # list available tools +aspire mcp tools --format Json # includes input schemas +aspire mcp call --input '{"key":"value"}' # invoke a tool +``` + +## Important rules + +- **Always start the app first** (`aspire start`) before making changes to verify the starting state. +- **To restart, just run `aspire start` again** — it automatically stops the previous instance. NEVER use `aspire stop` then `aspire run`. NEVER use `aspire run` at all. +- Use `--isolated` when working in a worktree. +- **Avoid persistent containers** early in development to prevent state management issues. +- **Never install the Aspire workload** — it is obsolete. +- Prefer `aspire.dev` and `learn.microsoft.com/dotnet/aspire` for official documentation. + +## Playwright CLI + +If configured, use Playwright CLI for functional testing of resources. Get endpoints via `aspire describe`. Run `playwright-cli --help` for available commands. \ No newline at end of file diff --git a/.github/skills/aspire/SKILL.md b/.github/skills/aspire/SKILL.md new file mode 100644 index 0000000..79e41d6 --- /dev/null +++ b/.github/skills/aspire/SKILL.md @@ -0,0 +1,93 @@ +--- +name: aspire +description: "Orchestrates Aspire distributed applications using the Aspire CLI for running, debugging, and managing distributed apps. USE FOR: aspire start, aspire stop, start aspire app, aspire describe, list aspire integrations, debug aspire issues, view aspire logs, add aspire resource, aspire dashboard, update aspire apphost. DO NOT USE FOR: non-Aspire .NET apps (use dotnet CLI), container-only deployments (use docker/podman), Azure deployment after local testing (use azure-deploy skill). INVOKES: Aspire CLI commands (aspire start, aspire describe, aspire otel logs, aspire docs search, aspire add), bash. FOR SINGLE OPERATIONS: Use Aspire CLI commands directly for quick resource status or doc lookups." +--- + +# Aspire Skill + +This repository uses Aspire to orchestrate its distributed application. Resources are defined in the AppHost project (`apphost.cs` or `apphost.ts`). + +## CLI command reference + +| Task | Command | +|---|---| +| Start the app | `aspire start` | +| Start isolated (worktrees) | `aspire start --isolated` | +| Restart the app | `aspire start` (stops previous automatically) | +| Wait for resource healthy | `aspire wait ` | +| Stop the app | `aspire stop` | +| List resources | `aspire describe` or `aspire resources` | +| Run resource command | `aspire resource ` | +| Start/stop/restart resource | `aspire resource start|stop|restart` | +| View console logs | `aspire logs [resource]` | +| View structured logs | `aspire otel logs [resource]` | +| View traces | `aspire otel traces [resource]` | +| Logs for a trace | `aspire otel logs --trace-id ` | +| Add an integration | `aspire add` | +| List running AppHosts | `aspire ps` | +| Update AppHost packages | `aspire update` | +| Search docs | `aspire docs search ` | +| Get doc page | `aspire docs get ` | +| List doc pages | `aspire docs list` | +| Environment diagnostics | `aspire doctor` | +| List resource MCP tools | `aspire mcp tools` | +| Call resource MCP tool | `aspire mcp call --input ` | + +Most commands support `--format Json` for machine-readable output. Use `--apphost ` to target a specific AppHost. + +## Key workflows + +### Running in agent environments + +Use `aspire start` to run the AppHost in the background. When working in a git worktree, use `--isolated` to avoid port conflicts and to prevent sharing user secrets or other local state with other running instances: + +```bash +aspire start --isolated +``` + +Use `aspire wait ` to block until a resource is healthy before interacting with it: + +```bash +aspire start --isolated +aspire wait myapi +``` + +Relaunching is safe — `aspire start` automatically stops any previous instance. Re-run `aspire start` whenever changes are made to the AppHost project. + +### Debugging issues + +Before making code changes, inspect the app state: + +1. `aspire describe` — check resource status +2. `aspire otel logs ` — view structured logs +3. `aspire logs ` — view console output +4. `aspire otel traces ` — view distributed traces + +### Adding integrations + +Use `aspire docs search` to find integration documentation, then `aspire docs get` to read the full guide. Use `aspire add` to add the integration package to the AppHost. + +After adding an integration, restart the app with `aspire start` for the new resource to take effect. + +### Using resource MCP tools + +Some resources expose MCP tools (e.g. `WithPostgresMcp()` adds SQL query tools). Discover and call them via CLI: + +```bash +aspire mcp tools # list available tools +aspire mcp tools --format Json # includes input schemas +aspire mcp call --input '{"key":"value"}' # invoke a tool +``` + +## Important rules + +- **Always start the app first** (`aspire start`) before making changes to verify the starting state. +- **To restart, just run `aspire start` again** — it automatically stops the previous instance. NEVER use `aspire stop` then `aspire run`. NEVER use `aspire run` at all. +- Use `--isolated` when working in a worktree. +- **Avoid persistent containers** early in development to prevent state management issues. +- **Never install the Aspire workload** — it is obsolete. +- Prefer `aspire.dev` and `learn.microsoft.com/dotnet/aspire` for official documentation. + +## Playwright CLI + +If configured, use Playwright CLI for functional testing of resources. Get endpoints via `aspire describe`. Run `playwright-cli --help` for available commands. \ No newline at end of file diff --git a/.mcp.json b/.mcp.json new file mode 100644 index 0000000..abe98b5 --- /dev/null +++ b/.mcp.json @@ -0,0 +1,11 @@ +{ + "mcpServers": { + "aspire": { + "command": "aspire", + "args": [ + "agent", + "mcp" + ] + } + } +} \ No newline at end of file diff --git a/Directory.Packages.props b/Directory.Packages.props index a3882fc..97f1e96 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -3,48 +3,53 @@ true - - - - - - + + + + + + + - - - + + + - - + + + - + + + - - - - - - - - + + + + + + + + + - - - - - - + + + + + + - + diff --git a/Sql.SemanticSearch.slnx b/Sql.SemanticSearch.slnx index 68cb282..a705486 100644 --- a/Sql.SemanticSearch.slnx +++ b/Sql.SemanticSearch.slnx @@ -15,6 +15,7 @@ + diff --git a/aspire.config.json b/aspire.config.json new file mode 100644 index 0000000..371509c --- /dev/null +++ b/aspire.config.json @@ -0,0 +1,6 @@ +{ + "appHost": { + "language": "csharp", + "path": "src/Sql.SemanticSearch.AppHost/Sql.SemanticSearch.AppHost.csproj" + } +} \ No newline at end of file diff --git a/scratch/badfile.md b/scratch/badfile.md new file mode 100644 index 0000000..7184370 --- /dev/null +++ b/scratch/badfile.md @@ -0,0 +1,6194 @@ +IEEE TRANSACTIONS ON WIRELESS COMMUNICATIONS, DOI: 10.1109/TWC.2023.3328713 + +1 + +Generalized Firefly Algorithm for Optimal Transmit +Beamforming + +Tuan Anh Le and Xin-She Yang + +3 +2 +0 +2 + +t +c +O +7 +2 + +] +T +I +. +s +c +[ + +1 +v +0 +6 +4 +8 +1 +. +0 +1 +3 +2 +: +v +i +X +r +a + +Abstract—This paper proposes a generalized Firefly Algorithm +(FA) to solve an optimization framework having objective func- +tion and constraints as multivariate functions of independent +optimization variables. Four representative examples of how +the proposed generalized FA can be adopted to solve down- +link beamforming problems are shown for a classic transmit +beamforming, cognitive beamforming, reconfigurable-intelligent- +surfaces-aided (RIS-aided) transmit beamforming, and RIS-aided +wireless power transfer (WPT). Complexity analyzes indicate that +in large-antenna regimes the proposed FA approaches require +less computational complexity than their corresponding interior +point methods (IPMs) do, yet demand a higher complexity than +the iterative and the successive convex approximation (SCA) +approaches do. Simulation results reveal that the proposed FA +attains the same global optimal solution as that of the IPM for +an optimization problem in cognitive beamforming. On the other +hand, the proposed FA approaches outperform the iterative, IPM +and SCA in terms of obtaining better solution for optimization +problems, respectively, for a classic transmit beamforming, RIS- +aided transmit beamforming and RIS-aided WPT. + +Index Terms—Firefly algorithm, nature-inspired optimization, + +transmit beamforming, reconfigurable intelligent surfaces. + +I. Introduction + +Transmit beamforming problems are normally cast as + +optimization problems where beamforming vectors are +optimization variables. Two fundamental optimization prob- +lems in transmit beamforming include: i) minimizing the total +transmit power subject +to signal-to-interference-plus-noise- +ratio (SINR) constraints [1]–[4]; ii) maximizing the weakest +SINR subject to a total power constraint [5], [6]. In fact, these +two problems are equivalent [7], [8]. A generalized version of +the second problem is introduced in [8] where the objective is +to maximize an arbitrary utility function of SINRs, which is +strictly increasing in every receiver’s SINR, subject to a power +constraint. The other variation of the second optimization +problem is the sum rate maximization [9], [10]. Furthermore, +additional constraints can be introduced to these fundamental +problems to capture other wireless communication applica- +tions. For instance, a soft-shaping interference constraint was +added for cognitive radio scenarios [11], [12] while a power +transfer constraint was included for simultaneous-wireless- +information-and-power-transfer scenarios [13]. In addition, +various metrics have been utilized to formulate downlink +beamforming optimization problems such as secrecy capacity + +T. A. Le and X.-S. Yang are with the Faculty of Science and +t.le; + +Technology, Middlesex University, London, NW4 4BT, UK. Email: +x.yang +} + +This paper has been presented in part at the IEEE Vehicular Technology + +@mdx.ac.uk. + +{ + +Conference (VTC 2023-Spring), Florence, Italy, June, 20-23, 2023. + +[14], energy efficiency [15], data transmission reliability, data +transmission security, and power transfer reliability [16]. + +Since the SINR is a non-convex quadratic function of +the beamforming vectors, the two fundamental beamforming +optimization problems are NP-hard and cannot be solved in +polynomial time. Fortunately, exploiting the hidden convexity +property of the SINR metric, an elegant framework was +proposed in [2] to convert these two optimization problems +into convex conic programming forms, which can be ef- +fectively solved by a standard interior point method (IPM). +Furthermore, uplink-downlink duality was utilized to derive +iterative algorithms to find optimal beamforming vectors for +some power minimization problems, e.g., [1], [4], [17], [18]. +An iterative algorithm was introduced in [9] to attain optimal +beamforming vectors for the sum rate maximization. + +Numerous transmit beamforming problems can be realized +in quadratically constrained quadratic programs (QCQPs) of +beamforming vectors, which are mostly non-convex [11], [19]. +To solve a QCQP problem, a semidefinite relaxation technique +[20] is adopted in which the original QCQP is converted to +a convex semidefinite programming (SDP) with new opti- +mization variables as beamforming matrices. If solving the +transformed SDP yields a rank-one optimal beamforming +matrix, then this optimal matrix is also the optimal solution +to the original QCQP. Otherwise, an approximated solution +to the original QCQP can be obtained by exploiting some +rank-one approximations or the Gaussian randomize procedure +[19]. Unfortunately, obtaining such solution requires further +computational resources yet results in a sub-optimal solution. +Optimization variables for downlink beamforming problems +may include different types of beamforming vectors. For exam- +ple, in a reconfigurable-intelligent-surface-aided (RIS-aided) +communication system, see e.g., [21], [22] and references +therein, the optimization variables are active beamforming +vectors for the base station (BS) and a passive beamformimg +vector for the RIS. The objective function and/or constraints +for a RIS-aided communication system are functions of both +active and passive beamforming vectors. These beamforming +vectors are independent variables yet need to be jointly op- +timized making their problems non-convex. Widely adopted +approaches for tackling such problems are to iteratively solve +two sub-optimization problems, a.k.a., alternative optimization +(AO) approach [21], or to approximate a non-convex using +first-order Taylor expansion, a.k.a., successive convex approx- +imation (SCA) [23]. In an AO approach, each of these two sub- +optimization problems, one variable is treated as a constant +while solving for the other. These sub-optimization problems +themselves are mostly in QCQP forms. Due to the inherent + + IEEE TRANSACTIONS ON WIRELESS COMMUNICATIONS, DOI: 10.1109/TWC.2023.3328713 + +2 + +non-convexity character of the original and sub-optimization +problems, the resulting active and passive beamforming vec- +tors may not be the global solutions. Whereas in a SCA +approach, a lower (or upper) bounded solution is normally +attained. + +IPMs, a.k.a., barrier methods, are gradient based algorithms +being good at exploitation,1 a.k.a., intensification, hence, they +are regarded as effective methods to solve convex optimization +problems [25]. Unfortunately, most of transmit beamforming +problems are non-convex. Solving non-convex optimization +problems requires algorithms having better exploration2 ability +than that of the IPMs to avoid getting trapped in a local +mode. Firefly algorithm (FA), +i.e., a nature-inspired algo- +rithm, possesses both exploitation and exploration abilities. +Consequently, FA is a good candidate for solving non-convex +downlink beamforming problems. FA is an easy-to-implement, +simple, and flexible algorithm based on the flashing char- +acters and behaviour of tropical fireflies [24]. FA was first +developed and published by Xin-She Yang, respectively, in +late 2007 and in 2008 [24], [26] for optimization problems +with objective and constrains being functions of a single +optimization variable. Although FA has been widely applied +to many applications [27], there has not been any significant +work investigating the application of FA in solving transmit +beamforming problems. There were only two attempts to +adopt FA for a throughput maximization problem in [28] +and for a power minimization problem in [29]. As these two +attempts only capture two fundamental transmit beamforming +problems, it is not clear how FA can be adopted to solve other +types of transmit beamforming problems. + +• + +This paper takes a further step on implementing FA to solve +a wider range of transmit beamforming optimization problems. +The contributions of the paper can be summarized as follows. +The paper proposes a generalized FA to find the optimal +solution of an optimization framework where its objective +function and constraints are multivariate functions of +multiple independent optimization variables. The prob- +lems in [28] and [29] are only two special cases of the +proposed generalized FA while the proposed generalized +FA is capable of handling a larger range of transmit +beamforming problems. +The paper shows four representative examples of how +the generalized FA can be adopted for solving transmit +beamforming problems, i.e., a classic transmit beamform- +ing approach, a cognitive beamforming approach, a RIS- +aided beamforming approach, and RIS-aided wireless +power transfer (WPT) approach. The applications of the +proposed generalized FA are beyond these four examples +which are only given to showcase how different types of +beamforming problems can be handled by the generalized +FA. +For the sake of completeness and comparison, the iterative +closed form or SDP forms of the under investigated beam- +forming approaches are represented. The paper analyzes + +• + +• + +1Exploitation is the ability of using any information from the problem of + +interest to form new solutions which are better than the current ones [24]. + +2Exploration is the ability of efficient exploring the search space to form + +new solutions with sufficient diversity and far from the existing ones [24]. + +• + +and compares the complexities of the iterative or SDP +and FA implementations of each beamforming approach. +Simulations are carried out to evaluate the performances +of the proposed FAs for the classic transmit beamforming, +cognitive beamforming, RIS-aided, and RIS-aided WPT +beamforming approaches. + +· + +(cid:23) + +k·k + +: the Euclidean norm; ( + +)H: the complex conjugate transpose operator; Tr ( +· +0: Y is positive semidefinite; Ix: an x +× + +Notation: Lower and upper case letter y and Y: a scalar; bold +lower case letter y: a column vector; bold upper case letter Y: +)T : the transpose operator; +a matrix; +): the trace +( +· +operator; Y +x identity +: the big O notation; CM +1 +matrix; +vectors with complex elements; HM +M +× +(0, σ2): y is a zero-mean circularly +Hermitian matrices; y +symmetric complex Gaussian random variable with variance +σ2; diag (y): a diagonal matrix whose diagonal elements are +the entries of vector y; and finally diag (Y): a vector whose +entries are the diagonal elements of matrix Y. + +1: the set of all M +× +M: the set of all M + +∼ CN + +× +× + +O + +II. Generalized Firefly Algorithm Framework + +A. Proposed Generalized Firefly Algorithm Framework + +The FA was developed based on the following three ide- +alized rules [24], [26]. First, any firefly attracts other fireflies +regardless of its sex. Second, the attractiveness of any firefly +to the other one is proportional to its brightness. Both attrac- +tiveness and brightness decrease as the distance between these +two fireflies increases. Given two flashing fireflies, the darker +firefly will move towards the brighter one. If a firefly does +not find any brighter one, it will make a random move. Third, +the brightness of a firefly depends on the landscape of the +objective function. + +In this section, we propose a generalized FA to find +optimal solution for an optimization framework containing +both objective and constraints as multivariate functions of +introduce the +independent variables. To that end, we first +following optimization framework. + +A,B, + +minimize +··· +subject to + +,Z + +f (A, B, + +, Z) , + +· · · + +(1) + +· · · +· · · + +gl (A, B, +hk (A, B, + +, Z) +0, l +≤ +, Z) = 0, k +CMb× +where A +i.e., +∈ +Ma, Na, Mb, Nb, +1, are decision variables, a.k.a., +optimization variables. Depending on the the values of +Ma, Na, Mb, Nb, +the decision variables can be +{ +matrices, vectors, scalars, or the combination of all. + +, +1, 2, . . . , L +} +1, 2, . . . K +, +} +CMz× +Nz , + +Na, B +∈ +, Mz, Nz +≥ + +∈ { +∈ { +, Z + +, Mz, Nz + +CMa× + +Nb , + +· · · + +· · · + +· · · + +, +} + +∈ + +We continue by using the penalty method [24], [26] to + +equivalently rewrite (1) as: + +minimize +··· + +A,B, + +,Z + +where P (A, B, + +· · · + +f (A, B, + +· · · + +, Z) + P (A, B, + +, Z) , + +· · · + +(2) + +, Z) is the penalty term defined as: + +P (A, B, + +, Z) = + +· · · + +L + +Xl=1 + +0, gl (A, B, +λlmax +{ + +· · · + +, Z) + +2 +} + +K + ++ + +Xk=1 + +ρk + +hk (A, B, +{ + +· · · + +, Z) + +2. +} + +(3) + + IEEE TRANSACTIONS ON WIRELESS COMMUNICATIONS, DOI: 10.1109/TWC.2023.3328713 + +3 + +} + +∀ + +In (3), λl > 0, +k, are penalty constants. Let +l, and ρk > 0, +∀ +, Zi +Ai, Bi, +be the i-th firefly amongst the population of +{ +· · · +N fireflies, i.e., i +. Following the second rule of +} +the FA, the brightest firefly is the most attractive one. Since +the proposed optimization framework is a minimization, we +define the brightness of firefly i as:3 + +1, 2, + +∈ { + +, N + +· · · + +Ii (Ai, Bi, + +, Zi) = + +· · · + +f (Ai, Bi, + +, Zi) + P (Ai, Bi, + +1 + +For +I j +will move towards firefly j at (n + 1)-th generation as: + +any +A j, B j, +(cid:16) + +1, 2, +{ +, Zi), + +fireflies +> + +two +, Z j + +∈ +· · · + +· · · + +· · · + +(cid:17) + +· · · +i, j +Ii (Ai, Bi, + +. + +, Zi) + +· · · + +(4) +, N +if +then firefly i + +, +} + +γx(r(n) + +, + +(cid:17) + +(cid:17) + +|| + +|| + +|| + +|| + +a,i j)2 + +· · · + +z,i j)2 + +(7) + +(6) + +b,i j)2 + +γz(r(n) + +γy(r(n) + +j − + +Z(n) + +j − + +j − + +j − + +j − + +, r(n) + +Z(n) +i + ++ α(n) + ++ α(n) + ++ α(n) + +a,i j = + +(cid:17) +B(n) +i + +a,i , (5) + += Z(n) + += B(n) + += A(n) + +(cid:16) +b,i j = + +j − +B(n) + +A(n) +i +B(n) +i + +A(n) +(cid:16) +B(n) +(cid:16) + +a ΛΛΛ(n) +b ΛΛΛ(n) +b,i , + +z ΛΛΛ(n) +z,i , +, r(n) + +i + βa,0e− +i + βb,0e− + +i + βz,0e− +A(n) +A(n) +i + +A(n+1) +i +B(n+1) +i +... +Z(n+1) +i +where r(n) +z,i j = +|| +Z(n) +Z(n) +are the Cartesian distances which are not necessary +i +|| +Euclidean distances yet they can be any measure effectively +characterized the quantities of interest in the optimization +, βz,0 are, respectively, the attractiveness +problem; βa,0, βb,0, +at r(n) +, r(n) +a,i j = 0, r(n) +b,i j = 0, +, γz present +the variations of the attractiveness. The second terms in (5), +(6), and (7) capture the attractions. The third terms in (5), +(6), and (7) are randomizations with randomization factors +α(n) +a , α(n) +CMa× +b , +z,i ∈ +CMz× +Nz being matrices of random numbers drawn from a +Gaussian or an uniform distribution. The proposed generalized +FA for solving the optimization framework (1) is summarized +in Algorithm 1, where T is the maximum generation of the +algorithm. For any particular optimization problem subsumed +under the framework, the corresponding FA will have the same +steps as those in Algorithm 1 except the input, step 3, step 16, +step 18, step 19, and the return value. + +z,i j = 0; finally γa, γb, + +z and ΛΛΛ(n) + +Na, ΛΛΛ(n) + +CMb× + +, ΛΛΛ(n) + +, α(n) + +a,i ∈ + +b,i ∈ + +Nb, + +· · · + +· · · + +· · · + +· · · + +· · · + +B. Asymptotic Convergence and Optimality + +Since the firefly algorithm, like quite a few other nature- +inspired algorithms, is a metaheuristic algorithm, there is no +rigorous proof of convergence so far in the current literature, +despite many applications of such metaheuristic algorithms. +In this section, we provide some intuitive discussions on the +optimality and convergence of the FA framework.4 + +· · · + +1) Asymptotic Optimality: Without loss of generality, let += γz = γ, we consider two special cases of the +. + +γa = γb = +variations of the attractiveness when γ +→ ∞ +γ(r(n) +b,i j)2 +When γ +it +→ +1, +, e− +1. Therefore the attractivenesses in (5), (6), +and (7) are constant and, respectively, equal to βa,0, βb,0, and +βz,0. Equivalently, it is an idealized sky scenario where the + +0 and γ +1, e− + +is clear that e− + +→ +γ(r(n) +z,i j)2 + +γ(r(n) + +a,i j)2 + +· · · + +→ + +→ + +→ + +0, + +3Note that if (1) is a maximization problem, then (2) can be expressed as: + +,Z − + +f (A, B, + +, Z) + P (Ai, Bi, + +minimize +A,B, +··· +4Mathematical analysis of the FA’s optimality and convergence deserves an +important research topic. Such analysis is postponed to future research due +to the space constraint. + +, Zi). + +· · · + +· · · + +Algorithm 1 Generalized Firefly Algorithm for solving (1) + +1: Input: + +parameters: +γa, γb, + +FA +βa,0, βb,0, +, βz,0, +· · · +the structures/parameters of +gl (A, B, +· · · +2: Randomly + +, Z), hk (A, B, + +· · · + +· · · + +, Z); + +N, + +T , + +λt, + +, γz; Optimization +f (A, B, +functions + +ρk, +data: +, Z), + +· · · + +generate +A2, B2, +{ + +· · · + +N + +populations + +, + +AN, BN, +{ + +· · · + +, ZN + +; +}} + +· · · + +A1, B1, + +{{ + +· · · + +3: Evaluate the light intensities of N population as (4); +order +in +4: Rank + +descending + +the + +, Z2} +, +a + +, Z1} +, +fireflies +, Zi); + +best +A⋆, B⋆, +{ + +solution: +, Z⋆ + +· · · + +} + +I⋆ + +of + +:= +:= + +Ii (Ai, Bi, + +· · · +the +A⋆, B⋆, +(cid:0) + +5: Define +I1 +A1, B1, +{ + +, Z⋆ +· · · +, Z1} +; +6: for n = 1 : T do +7: + +for i = 1 : N do + +· · · + +current +; +(cid:1) + +for j = 1 : N do +if Ii (Ai, Bi, +I⋆ +, Z⋆ +} +end if +if I j + +· · · + +:= + +A⋆, B⋆, +{ + +Ai, Bi, +{ + +· · · + +, Zi + +· · · + +, Zi) > I⋆ then +:= + +Ii (Ai, Bi, + +; +} +> I⋆ then +I j + +A j, B j, +(cid:16) + +, Zi); + +· · · + +· · · + +, Z j + +; + +(cid:17) + +· · · + +, Z j +:= +A j, B j, +{ + +· · · + +A j, B j, +(cid:16){ +I⋆ +, Z⋆ +} +end if +if I j + +:= + +· · · + +}(cid:17) +, Z j + +; +} + +· · · + +, Z j + +> Ii (Ai, Bi, + +, Zi) then +A j, B j, +Move firefly i towards firefly j as (5)-(7); +(cid:16) +end if +Attractiveness +γb +, e− +, + +varies with +r(n) +z,i j + +distances + +· · · + +γz + +(cid:17) + +; + +2 + +2 + +via + +Evaluate new solutions and update light inten- + +· · · + +(cid:16) + +(cid:17) + +, e− + +r(n) +b,i j +(cid:16) + +(cid:17) + +A⋆, B⋆, +{ + +γa + +e− + +r(n) +a,i j +(cid:16) + +2 + +(cid:17) + +sity as (4); + +end for + +end for +Rank +Ii (Ai, Bi, + +23: + +· · · +Update +A⋆, B⋆, +I1 +A1, B1, +(cid:0) +{ +24: end for +25: return + +, Zi); +the +, Z⋆ +· · · +, Z1} +; +· · · +A⋆, B⋆, +{ + +· · · + +, Z⋆ + +. +} + +the fireflies + +in + +a + +descending order of + +current +; +(cid:1) + +best +A⋆, B⋆, +{ + +solution: +, Z⋆ + +· · · + +} + +I⋆ + +:= +:= + +8: +9: + +10: + +11: + +12: + +13: + +14: + +15: +16: + +17: + +18: + +19: + +20: + +21: +22: + +brightness of each firefly does not change over the distance, +which can be seen everywhere. Consequently, a global opti- +mum can be obtained. + +it + +→ + +a,i j)2 + +b,i j)2 + +γ(r(n) + +0, e− + +, +→ ∞ +γ(r(n) +z,i j)2 +, e− + +On the other hand, when γ +γ(r(n) + +is obvious that +e− +0, +indicating +that the attractiveness of each firefly is zero. Equivalently, +each firefly is randomly in a heavily foggy region and cannot +be seen by the others. Each will randomly move and the +optimality is not always guaranteed. In this case, FA is +equivalent to a random search approach. + +· · · + +→ + +→ + +0, + +In fact, the attractiveness is in between these two extreme +0.5 defines the average +cases, i.e., 0 < γ < +. The value of γ− +distance of a herd of fireflies being seen by its adjacent herds. +Hence, the entire population can be separated into number of +herds. This automatic division property provides FA suitable + +∞ + + IEEE TRANSACTIONS ON WIRELESS COMMUNICATIONS, DOI: 10.1109/TWC.2023.3328713 + +4 + +→ + +· · · + +· · · + +ability of handling highly nonlinear and multimodal optimiza- +tion problems. By controlling the attractiveness γa, γb, +, γz +, αz, it has been shown +and the roaming randomness αa, αb, +in previous studies that FA can outperform both Particle +Swarm Optimization (PSO), see, e.g., [30]–[33], and random +search approaches, see e.g., [24], [26]. +2) Asymptotic Convergence: When γ + +0, the convergence +of FA is similar to that of PSO where the convergence was +analyzed by Clerc and Kennedy in 2002 in [34]. When γ +, +→ ∞ +the FA may act like a random search, though its behaviour +is similar to that of Simulated Annealing (SA) because the +FA’s solution is perturbed or modified in the similar way as +that in the SA in this limiting case. The SA was shown to +be convergent under the right-cooling conditions [35]. The +, αz, in +reduction of the roaming randomness, i.e., αa, αb, +the FA can be considered as a type of cooling schedule, and +thus it can be expected that FA can converge in this case. +Let us now investigate the case when 0 < γ < +. +∞ +Given a very large number of firefly population N, it can +be assumed that N is much greater than the number of local +optima. The initial locations of N fireflies should be uniformly +distributed over the whole search space. As the iterations of +Algorithm 1 progress, i.e., n increases, these initial N fireflies +should converge into all locally brighter ones, i.e., the local +optima including the global ones, in a stochastic manner due +to the third term in (5), (6), and (7). By comparing the +brightest fireflies amongst the locally brighter ones, i.e., the +best solutions amongst the local optima, the global optima +can be attained. Theoretically, these fireflies will reach the +global optimal when N +1. However, it has +been reported in the related literature that the FA converges +with less than 50 to 100 generations [24], [26]. + +and n + +→ ∞ + +· · · + +≫ + +In sections IV, V, and VI, we present how the proposed +FA can be adopted to solve optimization problems for trans- +mit beamforming designs.5 Hereafter, “min” and “s. t.” are, +respectively, used to represent “minimize” and “subject to”. + +III. Transmit Beamforming +In this section we consider a classic transmit beamforming +problem with a well-known iterative method based on uplink- +downlink duality. We then introduce our FA solution to the +problem. + +A. Problem Formulation + +∈ + +CM + +i ∈ + +C1 +× + +Mt , wi + +1) Problem Formulation: Consider an Mt-antenna BS serv- +ing U single-antenna mobile users. Let hH +1 +× +and si, respectively, be the channel between the i-th user +and the BS, the information-beamforming vector and the data +symbol for the ith user. The overall signal received by the ith +user is yi = +i w js j +ni where ni is a zero mean circularly +symmetric complex Gaussian noise with variance σ2, i.e., +(0, σ2), at the user. Let Ri = hihH +ni +represent the +i +instantaneous channel state information (CSI) or Ri = E +hihH +i +wi +be the set +denote the statistical CSI, +(cid:17) +(cid:16) +{ + +w1, w2, +{ + +U +j=1 hH + +∼ CN + +, wU + +· · · + +P + += + +} + +} + +of candidate information-beamforming vectors for all users. +Assuming that E + += 1, the SINR at the i-th user is + +2 + +si + +| + +(cid:16)| +(cid:17) +SINRi = + +i Riwi +j Riw j + σ2 +P +We design the set of beamforming vectors + +wH +U +j=1, j,i wH + +. + +such that +the BS’s total transmit power is minimized while maintaining +the SINR level at each user above the required threshold. To +that end, the problem is formulated as follows: + +wi +{ + +} + +(8) + +min +wi + +s. t. + +U + +Xi=1 + +wH + +t wt + +wH +U +j=1, j,i wH + +i Riwi +j Riw j + σ2 + +i ≥ + +(9) + +γi, + +i + +∀ + +1, + +∈ { + +, U + +, +} + +· · · + +P + +where γi is the required SINR level for the i-th user. Problem +(9) is known as non-convex due to the SINR constraint. + +2) Iterative Approach: An elegant approach to solve (9) +was introduced in [1] based on uplink-downlink duality where +the optimal solution of the downlink problem can be sought +via solving the following dual-uplink problem:6 + +min +pi + +subject to + +U + +pi + +Xi=1 +p + +Γt(p), + +(cid:23) +, Γ = diag +T + +T + +pU + +i +tU (p) + +, + +(10) + +γ1, γ2, + +· · · + +, γU + +, +(cid:3) + +(cid:2) + +where p = + +p1 + +p2 + +t(p) = + +t1 (p) +h + +h + +t2 (p) + +· · · + +· · · + +, + +(cid:16)P + +(11) + +ti (p) = arg min +ˆwi + +i +ˆwH +i Qi (p) ˆwi +ˆwH +i Ri ˆwi +U +, pi = λiσ2 +t=1,t,i ptRt + σ2 +Qi (p) = +i I +is the dual-uplink +i +power for i-th user, λi is the ith Lagrange multiplier associated +(cid:17) +with the ith constraint in (9), and ˆwi, i.e., ˆwH +i ˆwi = 1, is the dual- +uplink beamforming vector for i-th user. Starting from any +positive initial value of p (0), the solution for the dual-uplink +problem (10) can be found iteratively as p (n + 1) = Γt (p (n)). +The iterative downlink algorithm to find optimal solutions for +(9) is summarised in algorithm 2. + +B. Proposed Firefly Algorithm + +We rewrite (9) as + +f (W) + +min +W +s. t. + +di (W) + +0, + +i, + +(12) + +≤ +CMt× +U +i=1 wH +where W = +, wU +i wi, +i ∈ +U +j=1, j,i wH +di(W) = +i . Using the +penalty method, we recast (22) into an unconstrained problem +as: + +f (W) = +j Riw j + γiσ2 + +w1, w2, +· · · +i Riwi + γi + +∀ +U, + +wH +h + +P + +P + +− + +min +W + +f (W) + P(W), + +(13) + +5The original FA has been discretized to solve various discrete or combi- +natorial optimization problems [36]. For example, Osaba et al. [37] used a +discrete FA to solve rich vehicle routing problems. + +6This approach was also adopted for transmit beamforing problems in + +coordinated multi-point (CoMP) transmissions, see e.g., [38] and [39]. + + IEEE TRANSACTIONS ON WIRELESS COMMUNICATIONS, DOI: 10.1109/TWC.2023.3328713 + +5 + +Algorithm 2 Iterative algorithm for problem (9) +1: Input: Γ = diag + +, Ri, + +, γU + +γ1, γ2, +(cid:2) + +· · · + +(cid:3) + +i, number of + +∀ + +iterations T . +2: Initialize p (1) +(cid:23) +3: for n = 1 : T do +4: + +for i = 1 : U do + +0. + +Find ˆwi (n) as the dominant eigenvector of the + +5: + +6: + +matrix Gi(n) = pi (n) Q− +i + +1 + +(p (n)) Ri + +Calculate ti (p (n)) = ˆwH + +i (n)Qi(p(n)) ˆwi(n) +ˆwH +i (n)Ri ˆwi(n) + +. + +end for +Update p (n + 1) = Γt (p (n)). + +7: +8: +9: end for +10: p⋆ +11: Output: w⋆ + +i = p (n + 1) and ˆw⋆ +i = ˆwi (n + 1). +i ˆw⋆ +p⋆ +i . + +i = + +q + +where P(W) is the penalty term given as: + +U + +P(W) = + +λimax + +Xi=1 + +0, di(W) +{ + +2 , +} + +(14) + +with λi > 0 is the penalty constant. + +Let + +Wi +{ + +} + += + +wi + +1, wi +2, + +, wi +U + +· · · + +Wi +initialize a population of N fireflies +nh +io +{ +and define the light density of the firefly + +be the i-th firefly. We +, +} + +1, 2, + +, N + +· · · + +, i +} +Wi +{ + +∈ { +as: +} + +Ii (Wi) = + +1 +f (Wi) + P(Wi) + +. + +(15) + +For any two fireflies i and j +W j +I j +firefly j as: +(cid:17) +(cid:16) + +if +> Ii (Wi) then the firefly i will move toward the + +in the population, + +r(n) +i j + +γ + +(cid:16) + +2 + +(cid:17) + +W(n) +(cid:16) + +j − + +W(n) +i + +(cid:17) + ++ α(n)V, (16) + +W(n+1) +i + += W(n) + +i + β0e− +W(n) +i + +|| + +|| + +(W(n) + +where r(n) +i j = +is the Cartesian distance, β0 is +j − +the attractiveness at r(n) +i j = 0, γ presents the variation of of the +attractiveness. The second term of (16) represent the attraction. +The third term of (16) is a randomization comprised of a +randomization factor α(n) and a matrix of random numbers +U. The random factor α(n) and the elements of V are +V +drawn from either a Gaussian or an uniform distribution. + +CMt× + +∈ + +It can be seen that problem (12) is a special case of the +proposed framework (1) where the objective and constraints +are functions of optimization variable W. Hence, the proposed +FA has the same steps as those in Algorithm 1 except steps +3, 16, 18 and 19 given in Algorithm 3. + +Algorithm 3 Modified generalized FA for solving (12) + +i , γi; + +Input: FA parameters: N, T , λi, β0; Optimization data: Ri, +σ2 +Step 3: Evaluate the light intensities of N fireflies as (15); +Step 16: Move firefly i towards firefly j as (16); +Step 18: Attractiveness varies with distance via e− +(cid:17) +Step 19: Evaluate new solutions; update Ii(Wi) as (15); +return W⋆. + +r(n) +i j +(cid:16) + +; + +γ + +2 + +C. Complexity Analysis + +The complexity of algorithm 2 is described in the following + +lemma. + +Lemma 1: The computational complexity of algorithm 2 is + +. +i + +U(M3 +h + +on the order of T + +t + M2 + +t + Mt log Mt) + U + +Proof: The proof is based on the observation that com- +plexities of steps 5, 6 and 8 are, respectively, on the order of +M3 + +t + Mt log Mt, M2 +Lemma 2: The computational complexity of Algorithm 3 is + +t and U. + +on the order of: + +T N2 + +h + +M2 + +t + NU Mt(1 + U Mt) + ++ T N log N + N MtU ++NU Mt(1 + U Mt) + N log N. + +i + +(17) + +× + +t , while the complexity of evaluating + +Proof: Due to space limitation, we provide main obser- +vations to derive (17) as follows. The dominant terms of the +computational complexity of Algorithm 3 are at steps 2, 3, +4, 16, 19, and 22. The complexity of generating N matrices, +each matrix of size Mt +U, in step 2 is on the order of N MtU. +The complexity of evaluating each di(W) is on the order of +U M2 +t wt is on the +order of U Mt.7 Hence the complexity of calculating the light +density for N fireflies, i.e., steps 3 and 19, is on the order +of N(U Mt + U 2M2 +t ) = NU Mt(1 + U Mt). The complexity of +ranking N firefly in steps 4 and 22 is N log N. Finally, the +complexity of moving a firefly in step 16 is on the order of +M2 +t . Assuming a worst case when step 16 is executed in every +inner loop of the algorithm, after some manipulations, one can +arrive at (17). + +U +t=1 wH + +P + +IV. Cognitive Beamforming + +A. Problem Formulation + +1) Problem Formulation: Consider a cognitive wireless +communication system consisting of an Mt-antenna cognitive +base station (BS), U active single-antenna secondary users +(SUs) and K single-antenna primary users (PUs). The cog- +nitive BS is allowed to communicate with its SUs in the +same frequency band owned by the primary system if its +interference imposed on each PU is less than a predefined +tolerable threshold of Ito,k. The received signal at the t-th SU, +t + +, U + +1, + +∈ { + +· · · + +, is: +} +yt = hH + +s,twt st + + +U + +Xj=1, j,t + +hH +s,tw js j + nt, + +(18) + +C1 +× + +s,t ∈ + +∼ CN + +Mt is the channel coefficient of the wireless +where hH +1 and +link between the t-th SU and the cognitive BS; wt +(0, 1) are, respectively, the beamforming vector and +st +(0, σ2 +the data symbol associated to the t-th SU; and nt +t ) +is a zero mean circularly symmetric complex Gaussian noise +t , at the t-th SU. Let Rs,t = E +with variance σ2 +for the +statistical CSI and Rs,t = hs,thH +(cid:16) +s,t for the instantaneous CSI. +The SINR at the t-th SU can be expressed as: + +hs,thH +s,t + +CMt× + +∼ CN + +∈ + +(cid:17) + +SINRt = + +wH +U +j=1, j,t wH + +t Rs,twt +j Rs,tw j + σ2 +t + +. + +(19) + +7Here, we adopt the schoolbook iterative algorithm to evaluate complexity +p as the order + +of the multiplication of two matrices of sizes n +of nmp. + +m and m + +× + +× + +P + + IEEE TRANSACTIONS ON WIRELESS COMMUNICATIONS, DOI: 10.1109/TWC.2023.3328713 + +p,k ∈ + +Let hH + +C1 +Mt be the channel coefficient of the wireless +× +, K +link between the k-th PU, k +, and the cognitive BS, +} +Rp,k = E +for the statistical CSI and Rp,k = hp,khH +hp,khH +p,k for +p,k +the instantaneous CSI. The total interference power imposed +(cid:16) +on the k-th PU by the cognitive BS is + +∈ { + +· · · + +1, + +(cid:17) + +U +j=1 wH + +j Rp,kw j. + +P + +Our objective is to design downlink beamforming vectors +for the SUs that minimize the cognitive BS transmit power +while maintaining the required SINR level for every SU and +keeping the interference level imposed at each PU receiver +below the predefined tolerable threshold. The optimization +problem to design beamforming vectors is cast as: + +min +wt + +s. t. + +U + +Xt=1 + +wH + +t wt + +wH +U +j=1, j,t wH +P +U + +t Rs,twt +j Rs,tw j + σ2 + +t ≥ + +ηt, + +t + +∀ + +1, + +∈ { + +, U + +, +} + +· · · + +(20) + +wH + +j Rp,kw j + +Ito,k, + +k + +∀ + +1, + +∈ { + +, K + +, +} + +· · · + +≤ + +Xj=1 + +where ηt is the required SINR level for the t-th SU. Due to +the SINR constraint, problem (20) is non-convex. + +2) SDP Approach: For the sake of completeness, we +provide a review on a traditional approach to solve (20) +using semidefinite programming (SDP). We first form a new +optimization variable Ft = wtwH +Mt , +is a rank-one matrix.8 We then utilize the identity +and Ft +xHXx = Tr(XxxH) to rewrite (20) as: + +t where Ft + +HMt× + +0, Ft + +(cid:23) + +∈ + +U + +M + +Xt=1 + +Tr (Ft) + +min +HM + +× + +Ft∈ + +s. t. + +1 + + +Tr + +Rs,tFt + +(cid:0) + +U + +− + +Xj=1 + +(cid:1) + +1 +ηt ! + +U + +Tr + +Rs,tF j + +(cid:16) + +σ2 + +t ≥ + +0, + +t, + +∀ + +(cid:17) − + +Ito,k + +− + +Ft + +(cid:23) + +Xj=1 +0, + +∀ +, U + +Tr + +Rp,kF j + +(cid:16) + +t, + +0, + +k, + +∀ + +(cid:17) ≥ + +(21) + +where t + +1, + +∈ { + +· · · + +, k +} + +1, + +∈ { + +· · · + +, K + +. +} + +Problem (21) is in a standard SDP form. Hence, its optimal +solution can be obtained in a polynomial time by using a +general purpose IPM, e.g., CVX which is a Matlab based +modeling system for constructing and solving disciplined +convex programs [40]. In arriving at (21), we have relaxed +the rank-one constraint on Ft, +t. If the solution of (21) +does not have rank-one, then further computation resources are +required to derive a sub-optimal solution via some rank-one +approximations or the Gaussian randomize procedure [19]. + +∀ + +6 + +(22) + +f (W) + +min +W +s. t. + +φt(W) +ϕk(W) + +≤ + +0, +0, + +t + +∀ +k + +∈ { + +· · · + +1, +1, + +, U + +, K + +, +} +, +} + +∀ +∈ { +CMt× + +≤ +, wU +· · · +j Rs,tw j + ηtσ2 + +· · · +U, +f (W) = +t wt, +wH +t Rs,twt and ϕk(W) = +P +Ito,k. Using the penalty method, we first + +U +t=1 wH + +i ∈ + +t − + +where W = +φt(W) = ηt +U +j=1 wH + +w1, w2, +U +j=1, j,i wH +h +j Rp,kw j +P + +− + +transform (22) into an unconstrained problem as: +P + +min +W +where P(W) is the penalty term given as: + +f (W) + P(W), + +(23) + +U + +P(W) = + +λtmax + +Xt=1 + +0, φt(W) +{ + +2 + +} + +K + +Xk=1 + +ρkmax + +0, ϕk(W) +{ + +2 , +} + +(24) + +with λt > 0 and ρk > 0 are penalty constants. + +wi + +Let Wi = + +1, wi +2, +initialize a population of N fireflies Wi, i +∈ { +define the light density of the firefly Wi as: + +U be the firefly i. We +, and +} + +, wi +U + +i ∈ + +1, 2, + +, N + +CMt× + +· · · + +· · · + +h + +Ii (Wi) = + +1 +f (Wi) + P(Wi) + +. + +(25) + +For any two fireflies i and j +W j +I j +firefly j as: +(cid:17) + +if +> Ii (Wi) then the firefly i will move toward the + +in the population, + +(cid:16) + +W(n) + +j − + +W(n) +i + ++ α(n)V, + +(26) + +W(n+1) +i + += W(n) + +2 + +(cid:17) + +γ + +i + β0e− +(W(n) + +r(n) +i j +(cid:16) +W(n) +i + +(cid:17) + +|| + +|| + +(cid:16) +where r(n) +i j = +is the Cartesian distance, β0 is +j − +the attractiveness at r(n) +i j = 0, γ presents the variation of of the +attractiveness. The second term of (26) captures the attraction. +The third term of (26) is a randomization comprised of a +randomization factor α(n) and a matrix of random numbers +U. The random factor α(n) and the elements of V are +V +drawn from either a Gaussian or an uniform distribution. + +CMt× + +∈ + +It can be seen that problem (22) is a special case of the +proposed framework (1) where the objective and constraints +are functions of only one optimization variable W. Hence, the +proposed FA has the same steps as those in Algorithm 1 except +steps 3, 16, 18 and 19 given in Algorithm 4. + +Algorithm 4 Modified generalized FA for solving (20) + +t , ηt, Ito,k; + +Input: FA parameters: N, T , λt, ρk, β0, γ; Optimization +data: Rs,t, Rp,k, σ2 +Step 3: Evaluate the light intensities of N fireflies as (25); +Step 16: Move firefly i towards firefly j as (26); +Step 18: Attractiveness varies with distance via e− +(cid:17) +Step 19: Evaluate new solutions; update Ii(Wi) as (25); +return W⋆. + +r(n) +i j +(cid:16) + +; + +γ + +2 + +B. Proposed Firefly Algorithm + +Here, we adopt the generalized FA in Algorithm 1 to solve + +(20). Rearranging the constraint, we rewrite (20) as: + +C. Complexity Analysis + +8A matrix is rank-one if and only if it has only one linearly independent + +column/row. + +We investigate the complexity of solving (21) in a worst- +case runtime of the IPM followed by the complexity analysis +of the proposed FA. We start by the following definition. + + IEEE TRANSACTIONS ON WIRELESS COMMUNICATIONS, DOI: 10.1109/TWC.2023.3328713 + +7 + +Definition 1: At a given ε > 0, the set of + +is an ε- +solution to problem (21), i.e., an acceptable solution with the +accuracy of ε, if + +Fε +t } +{ + +U + +Xt=1 + +Tr + +Fε +t +(cid:0) + +(cid:1) + +U + +min +HM +× + +Ft∈ + +M + +≤ + +Xt=1 + +Tr (Ft) + ε. + +(27) + +The number of decision variables of (21) is M2 +plexity of (21) is described in the following lemma. + +t . The com- + +Lemma 3: The computational complexity to attain ε- + +solution to (21) is on the order of: + +1 + +i + +(cid:16) + +ln + +ε− + +(cid:17) p + +(28) + +t + 1)(U + K) +M2 +t . + +(M2 +U(Mt + 1) + K +h ++U M2 +t (M2 +t + Mt) + M4 +t +Proof: We sketch some main steps to arrive at the lemma +due to space limitation. It can be observed that (21) has (U+K) +linear-matrix-inequality (LMI) constraints of size 1 and U LMI +constraints of size Mt. One can follow the same steps as in +[41, Section V-A] to derive the following facts: (i) the itera- +√U(Mt + 1) + K, +ε− +tion complexity is on the order of ln +the per-iteration complexity is on the order of +and (ii) +(cid:16) +t + Mt) + M4 +(M2 +t +h +on the order of: + +t + 1)(U + K) + U M2 +Lemma 4: The computational complexity of Algorithm 4 is + +(cid:17) +M2 +t . + +t (M2 + +i + +1 + +T N2 + +M2 + +t + NU Mt(1 + U Mt + K Mt) + ++ T N log N + N MtU + +h + ++NU Mt(1 + U Mt + K Mt) + N log N. (29) + +i + +× + +t , while the complexity of evaluating + +Proof: Due to space limitation, we provide main obser- +vations to derive (29) as follows. The dominant terms of the +computational complexity of Algorithm 4 are at steps 2, 3, 4, +16, 19, and 22. The complexity of generating N matrices, each +U, in step 2 is on the order of N MtU. The +matrix of size Mt +complexity of evaluating each φt(W) or ϕk(W) is on the order +of U M2 +t wt is on +the order of U Mt. Hence the complexity of calculating the light +density for N fireflies, i.e., steps 3 and 19, is on the order of +N(U Mt + U 2M2 +t ) = NU Mt(1 + U Mt + K Mt). The +complexity of ranking N firefly in steps 4 and 22 is N log N. +Finally, the complexity of moving a firefly in step 16 is on the +order of M2 +t . Assuming a worst case when step 16 is executed +in every inner loop of the algorithm, after some manipulations, +one can arrive at (29). + +t + KU M2 + +U +t=1 wH + +P + +V. Reconfigurable Intelligent Surface-Aided Beamforming +A. Problem Formulation + +1) Problem Formulation: Consider a communication sys- +tem comprising of an Mt-antenna BS communicating with U +single-antenna mobile users in which the direct communica- +tion links between the BS and its mobile users are blocked, +e.g., because of high building etc., [42]. To circumvent the +problem, an Nt-reflective-element RIS is utilized to support +Nt represent +the communication. Let H = [h1, . . . , hNt ] +the channel coefficients between the BS and the RIS and +gi = [gi1, . . . , giNt ]T +1 be the channel coefficients +between the RIS and the i-th user. + +CMt× + +CNt× + +∈ + +∈ + +Let xi, i.e., E[ + +1, respectively, +represent the data symbol and the active beamforming vector + +2] = 1, and wi + +CMt × + +xi + +∈ + +| + +| + +for the i-th user. Each reflective element of the RIS generates +a phase shift to support the communication between the BS +and the mobile users. Let θk be the phase shift at the k-th +, θNt ]T denote the +reflective element and let θθθ = [θ1, θ2, +phase-shift coefficients generated by the RIS with +1 +k = 1, . . . , Nt. Vector θθθ is the passive +and arg(θk) +beamforming vector for the RIS. The signal arrived at the i-th +user is: + +π, π), + +| ≤ + +· · · + +θk + +− + +∀ + +∈ + +[ + +| + +yi = gH + +i diag(θθθ)HHHwixi + gH + +i diag(θθθ)HHH + +U + +Xj=1, j,i + +w jx j + ni, + += θθθHGH + +i wixi + θθθHGH +i + +U + +Xj=1, j,i + +w jx j + ni, + +(30) + +CNt× + +i = diag(g∗i )HH + +where GH +(0, σ2) +represents the additive noise measured at the i-th user. Fur- +denote the set of active +thermore, let +} +, θθθ) be the SINR at the +beamforming vectors, and SINRi( +} +i-th user. One can write: + +, wU +} +wi +{ + +w1, w2, +{ + +Mt and ni + +∼ CN + +wi +{ + +· · · + += + +∈ + +SINRi ( + +wi +{ + +, θθθ) = +} + +U + +2 + +. + +(31) + +θθθHGH +| + +θθθHGH + +i wi + +| +i w j + +2 + σ2 +i + +| + +j=1, j,i | +P + +The optimization is posed as follows: + +min +, θθθ +wi} +{ +s. t. + +U + +wH + +i wi + +Xi=1 +, θθθ) +SINRi ( +wi +} +{ +k, +1, +θk + +ηi, + +i, + +∀ + +≥ + +(32) + +| + +∀ +where ηi is the required SINR level measured at the i-th user. +Since the SINR constraint is a function of two optimization +variables wi and θθθ, problem (32) is non-convex. + +| ≤ + +2) Alternative Optimization Approach: For the sake of +completeness, the widely-adopted AO approach [21], [42]– +[44] is represented here as a baseline to solve (32). Let +i , and Θ = θθθθθθH, i.e., rank(Fi) = 1 and rank(Θ) = 1. +Fi = wiwH +As Fi and Θ are two independent variables, they can be +alternatively solved [21], [42]–[44]. To that end, relaxing the +rank-one constraint on Fi and beginning with any initial value +of the reflecting coefficient matrix Θ(0), the following sub- +problem will be solved at the p-th iteration: + +U + +Tr + +min +Fi} +{ + +s. + +t. + +Tr + +Fi + +Xi=1 + + +GiΘ(p +1)GH +− +ηiσ2 +i + +i Fi + +− + +Fi + +0, + +i + +∀ + +1, + +∈ { + +· · · + +(cid:23) + +U + +Xj=1, j,i +. +, U +} + +Tr + +GiΘ(p +1)GH +− +σ2 +i + +i F j + +1 + +0, + +i, + +∀ + +≥ + +− + +(33) + +The reflecting coefficients Θ(p) is then updated from the +, by solving +} + +optimal solution of (33) at p-th iteration, i.e., + +F(p) +i +{ + + IEEE TRANSACTIONS ON WIRELESS COMMUNICATIONS, DOI: 10.1109/TWC.2023.3328713 + +the following sub-problem [42]: + +min +Θ + +Tr (Θ) + +It (Wt, θθθt) = + +1 +f (Wt) + P(Wt, θθθt) + +. + +8 + +(39) + +ΘGH + +j Gi + +i F(p) +σ2 +i + +1 + +0, + +i, + +∀ + +≥ + +− + +For any fireflies t and l amongst + +if +It (Wt, θθθt) > Il (Wl, θθθl) then the firefly l will move toward the +firefly t as: + +the population, + +s. + +t. + +Tr + +ΘGH + +i Gi + +i F(p) +ηiσ2 +i +diag (Θ) +(cid:1) +(cid:0) +0. + +Tr + +U + +− + +(cid:22) + +Xj=1, j,i +INt , + +diag +Θ + +(cid:23) + +(34) +The AO approach repetitively solves two SDPs (33) and + +(34) in n0 iterations to obtain the solution for (32). + +Remark 1: It is worth noticing that the AO approach ap- +proximates the originally non-convex optimization (32) by +two sub-problems (33) and (34). Although (33) and (34) are +convex, the solutions to these sub-problems can be regarded +as the upper bounds of the original problem (32) as these +solutions may not be the global solution. Furthermore, the +AO approach adopts the so-called semidefinite relaxation +technique [20] in which the rank-one constraints on Fi and +Θ are relaxed. If solving (33) and/or (34) does not return +rank-one matrices Fi and/or Θ, then a rank-one approximation +or a Gaussian randomize procedure [19] is required to extract +approximated rank-one solutions. Extracting the approximated +solutions requires further computational resources yet only +results in sub-optimal solutions. + +Motivated by the above observations, we introduce a novel +FA approach to simultaneously solve wi and θθθ for the original +problem (32) in the following section. + +B. Proposed Firefly Algorithm + +The optimization (32) can be expressed as + +f (W) + +min +W, θθθ +{ +} +s. t. + +where W = + +w1, w2, +h + +· · · + +φi (W, θθθ) = ηi P + +W, θθθ +φi ( +{ +ϕk (θk) + +) +} +0, +≤ +CMt× + +i, + +0, +k, + +∀ + +≤ +∀ +U, f (W) = + +, wU + +i ∈ +U +j GiθθθθθθHGH +j=1 wH + +i w j + ++ ηi + +(35) + +U +i=1 wH + +i wi, + +P + +σ2 +i +wH +i GiθθθθθθHGH +σ2 +i +1. Adopting the penalty method, (35) can + +(1 + ηi) + +i wi + +(36) + +− + +, + +and ϕk (θk) = +be written as: + +| + +θk + +| − + +min +W, θθθ +} +{ +where P(W, θθθ) is the penalty term given as: + +f (W) + P(W, θθθ), + +P(W, θθθ) = + +U + +Xi=1 + +λimax + +0, φi( +{ + +W, θθθ +{ + +) +} + +} + +2 + + +Nt + +Xk=1 + +ρkmax + +0, ϕk(θk) +{ + +2 , (38) +} + += + +Let + +Wt, θθθt +{ + +with λi > 0 and ρk > 0 are penalty constants. +1, wt +2, + +wt +, wt +be the firefly t. We +U +{h +initialize a population of N fireflies +, N +1, 2, +i +} +and define the light density, i.e., the brightness, of the firefly +t + +, θθθt +} +Wt, θθθt +{ + +, t +} + +∈ { + +· · · + +· · · + +as: + +} + +Wt, θθθt +{ + +} + +W(n) +l + ++ α(n)V, (40) + +(cid:17) ++ α(n)v, + +2 + +γ + +2 + +(cid:17) + +r(n) +w,tl +(cid:16) +r(n) +θ,tl + +W(n) +(cid:16) +θθθ(n) +t − +(cid:17) +(cid:16) +and r(n) +θ,tl = + +t − +θθθ(n) +l + +W(n+1) +l +θθθ(n+1) +l + += W(n) + +l + β0e− +γ + += θθθ(n) + +(cid:16) + +|| + +|| + +(41) + +t − + +(W(n) + +(cid:17) +(θθθ(n) + +w,tl = + +l + β0e− +θθθ(n) +W(n) +where r(n) +are the +l +l +Cartesian distances, β0 is the attractiveness at r(n) +w,tl = 0 and +r(n) +θ,tl = 0, γ presents the variation of of the attractiveness. The +second terms of (40) and (41) capture the attractions while +the third terms of (40) and (41) are randomization comprised +of randomization factor α(n), V +1. The +factor α(n), the elements of V and v are drawn from either an +uniform or a Gaussian distribution. + +U and v + +CMt × + +CMt× + +t − + +∈ + +∈ + +|| + +|| + +It can be observed that problem (35) is a special case of the +proposed framework (1) where the objective and constraints +are functions of optimization variables W and θθθ. The proposed +FA for RIS has the same steps as those in Algorithm 1 except +steps 3, 16, 18 and 19 given in Algorithm 5. + +Algorithm 5 Modified generalized FA for solving (32) + +i , ηi, Ito; + +Input: FA parameters: N, T , λi, ρn, β0; γ; Optimization +data: H, gi, σ2 +Step 3: Evaluate the light intensities of N fireflies as (39); +Step 16: Move firefly i towards firefly j as (40) and (41); +r(n) +Step 18: Attractiveness varies with distances via e− +w, ji +(cid:16) +and e− +Step 19: Evaluate new solutions; update Ii (Wi, θθθi) as (39); +return W⋆, θθθ⋆. + +r(n) +θ, ji +(cid:16) + +; + +γ + +γ + +(cid:17) + +(cid:17) + +2 + +2 + +C. Complexity Analysis + +Here, we analyze the computational complexities of the AO + +and the proposed FA for RIS-aided beamforming problem. + +Lemma 5: The complexity of the AO approach is on the + +order of: + +where + +no (τ1 + τ2) , + +(42) + +τ1 = ln + +1 + +ε− + +U(Mt + 1) + +t + 1)U + U M2 + +t (M2 + +t + Mt) + +(M2 +h + +(43) + +N2 + +t .(44) + +i + +(N2 + +t + 1)(U + 2N2 + +t ) + N4 +t + +(cid:16) + +(cid:17) p + +h + +Proof: We first give some hints to derive the computa- +tional complexity of obtaining optimal solution to problems +(33) and (34). With the observation that (33) has U LMI +constraints of size 1 and U LMI constraints of size Mt, one +can follow the same steps as in [41, Section V-A] to derive +the complexity of solving (33) as τ1 given in (43). + +At a given ε > 0, Θε is called an ε-solution to problem (34) +Tr (Θ)+ε. The number of decision variables of + +if Tr (Θε) + +min +Θ + +≤ + +(37) + +(cid:16) ++M4 +t + +(cid:17) p +M2 +t , + +τ2 = ln + +i +1 +ε− + +U + 2Nt + + IEEE TRANSACTIONS ON WIRELESS COMMUNICATIONS, DOI: 10.1109/TWC.2023.3328713 + +9 + +(34) is N2 +t . Observing that (34) has U linear-matrix-inequality +(LMI) constraints of size 1 and 2 LMI constraints of size +Nt, one can derive the computational complexity to attain ε- +solution to (34) as the order of τ2 given in (44). + +Since the AO approach iteratively solves (33) and (34) in +no iterations, the complexity of AO approach is on the order +of no (τ1 + τ2). + +Lemma 6: The computational complexity of Algorithm 5 is + +on the order of + +the l-th iterations is calculated as +beamforming vector at +U +1)θθθ(l +i=1 αiGiθθθ(l +w(l) = √Peigmax +where eigmax (X) is +− +− +the maximum eigenvalue of matrix X. The k-th coefficient of +the RIS’s phase shift vector at the l-th iterations is calculated +if µk , 0, where +θθθ(l) +as +h +µk = + += 1 if µk = 0 and +i w(l)w(l)HGiθθθ(l +− + +k +U +i=1 αiGH + +θθθ(l) +k +i +h +1) +. + += µk +µk| +| + +1)HGH +i + +(cid:16)P + +(cid:17) + +k +i + +i +hP + +B. Proposed Firefly Algorithm + +The optimization (47) can be expressed as + +M2 + +t + Nt + N + +U Mt + U(N2 + +T N2 +(cid:16) ++T N log N + N MtU + NtN + N log N ++N + +h +U Mt + U(N2 + +t + MtNt) + Nt + +. + +t + MtNt) + Nt + +(cid:17)i + +(45) + +min +W, θθθ +{ +} +s. t. + +(cid:16) + +(cid:17) + +U +i=1 wH + +Proof: The proof is based on the following observa- +tions. The dominant terms of the computational complexity +of Algorithm 5 are at steps 2, 3, 4, 16, 19, and 22. The +complexity of generating N fireflies in step 2 is on the order of +N MtU + NtN. The complexities of evaluating φi(W, θθθ), ϕk(θk), +i wi are, respectively, on the order of U(N2 +t + MtNt), +and +Nt, and U Mt. Hence, the complexity of calculating the light +density for N fireflies, i.e., steps 3 and 19, is on the order of +. The complexity of ranking N +N +firefly in steps 4 and 22 is N log N. Finally, the complexity of +(cid:17) +moving a firefly in step 16 is on the order of M2 +t +Nt. Assuming +a worst case when step 16 is executed in every inner loop of +the algorithm, after some manipulations, one can arrive at (45). + +t + MtNt) + Nt + +U Mt + U(N2 + +P + +(cid:16) + +VI. RIS-Aided Wireless Power Transfer + +A. Problem Formulation + +1) Problem Formulation: Consider a similar communica- +tion system in V-A, however, the users are energy harvesting +receivers (EHRs) instead of information decoding receivers. +Using the same notations as in V-A, the power arrived at the i- +th user is: + +Ei = + +i diag(θθθ)HHH +gH + +U + +Xj=1 + +w j + +U + +2 + += + +Xj=1 + +wH + +j Giθθθ θθθHGH + +i w j,(46) + +where w j is the active energy beamforming vector for the j-th +user. we interested in maximizing a total weighted sum power +received at the EHRs obtained via the following optimization +problem: + +(cid:12)(cid:12)(cid:12)(cid:12) + +(cid:12)(cid:12)(cid:12)(cid:12) + +max +, θθθ +wi} +{ + +s. t. + +αiwH + +j Giθθθ θθθHGH + +i w j + +U + +U + +Xj=1 + +Xi=1 +U + +wH + +j w j + +P, + +θk + +| + +| + +≤ + += 1, + +k, + +∀ + +Xj=1 + +(47) + +where P is the maximum transmit power of the BS and αi +is the weighting factor for the i-th EHR. + +0 + +≥ + +2) Successive Convex Approximation: According to [23], +for any fix θθθ, only one common energy beam is sufficient. +Using a successive convex approximation (SCA) technique, +[23] proposed an iterative algorithm to find optimal active +and passive beamforming vectors for problem (47) as follows. +Starting with an initialized value θθθ(0), +the optimal active + +f (W, θθθ) + +− +W, θθθ +φ ( +≤ +{ +ϕk (θk) = 0, +∀ + +) +} + +0, +k, + +(48) + +where W = +U +j=1 αiwH +θk + +U +i=1 +ϕk (θk) = +P +P +| +written as: + +| − + +f (W, θθθ) = +w1, w2, +j GiθθθθθθHGH +h +P, and +1. Adopting the penalty method, (35) can be + +, wU +i w j, φ (W, θθθ) = + +CMt × +U, +U +j=1 wH + +j w j + +· · · + +− + +∈ + +i + +P + +min +W, θθθ +} +{ + +− + +f (W, θθθ) + P(W, θθθ) + +(49) + +Let + +Nt +k=1 ρk + +0, φ( +{ +1, wt +2, + +2 + +where P(W, θθθ) = λmax +} +λ > 0 and ρk > 0 are penalty constants. +, θθθt +} +Wt, θθθt +{ + +W, θθθ +) +} +{ +, wt +be the firefly t. We +U +{h +initialize a population of N fireflies +, N +1, 2, +i +} +and define the light density, i.e., the brightness, of the firefly +t + +2, with +} + +ϕk(θk) +{ + +Wt, θθθt +{ + +, t +} + +∈ { + +· · · + +· · · + +wt + +as: + +P + += + +} + +Wt, θθθt +{ + +} + +It (Wt, θθθt) = + +− + +1 +f (Wt) + P(Wt, θθθt) + +. + +(50) + +It can be observed that problem (48) is a special case of the +proposed framework (1) where the objective and constraints +are functions of optimization variables W and θθθ. Utilizing the +firefly movements define in (40) and (41) in SectionV-B, the +proposed FA for RIS has the same steps as those in Algo- +rithm 1 except steps 3, 16, 18 and 19 given in Algorithm 6. + +Algorithm 6 Modified generalized FA for solving (47) + +Input: FA parameters: N, T , λ, ρk, β0; γ; Optimization +data: H, gi, αi, P; +Step 3: Evaluate the light intensities of N fireflies as (50); +Step 16: Move firefly i towards firefly j as (40) and (41); +r(n) +Step 18: Attractiveness varies with distances via e− +w, ji +(cid:16) +and e− +Step 19: Evaluate new solutions; update Ii (Wi, θθθi) as (50); +return W⋆, θθθ⋆. + +r(n) +θ, ji +(cid:16) + +; + +γ + +γ + +(cid:17) + +(cid:17) + +2 + +2 + +C. Complexity Analysis + +Here, we analyze the complexities of the SCA approach +and the proposed FA for the RIS-aided WPT beamforming. +We start by introducing the following lemma. + +Lemma 7: The complexity of the SCA approach is on the + +order of: + +m0 + +U Mt (Mt + Nt) + M3 + +t + Mt log Mt + N3 + +t + N2 + +t Mt + +, + +(51) + +(cid:16) + +where m0 is the number of iterations of the SCA approach. + +(cid:17) + + IEEE TRANSACTIONS ON WIRELESS COMMUNICATIONS, DOI: 10.1109/TWC.2023.3328713 + +10 + +M2 + +1)θθθ(l +− + +1)θθθ(l +− + +1)H GH + +1)HGH +i + +t + MtNt + +is on the order of U + +Proof: At each iteration, the complexity of evaluating +αiGiθθθ(l +. The +− +complexities of finding a maximum eigenvalue of the Mt +Mt +(cid:17) +(cid:16) +matrix αiGiθθθ(l +i based on the SVD method is on the +− +t + Mt log Mt. Hence, the complexity of finding w(l) +order of M3 +is on the order of U Mt(Mt +Nt)+ M3 +t + Mt log Mt. Furthermore, +the complexity of calculating µk is on the order of N2 +t + MtNt. +Therefore, the complexity of finding θθθ(l) is on the order of +. Consequently, m0 iterations of evaluating w(l) +t + MtNt +Nt +and θθθ(l) lead to (51). + +(cid:16) +Lemma 8: The complexity of the Algorithm 6 is on the + +N2 + +× + +(cid:17) + +order of: + +(c) + +FA +Iterative + +35 + +30 + +25 + +20 + +15 + +10 + +] + +m +B +d +[ + +r +e +w +o +p + +t +i +s +m +n +a +r +T + +(a) + +FA +Iterative + +35 + +30 + +25 + +20 + +15 + +10 + +] + +m +B +d +[ + +r +e +w +o +p + +t +i +s +m +n +a +r +T + +(b) + +FA +Iterative + +35 + +30 + +25 + +20 + +15 + +10 + +] + +m +B +d +[ + +r +e +w +o +p + +t +i +s +m +n +a +r +T + +M2 + +t + Nt + N + +U Mt + U(N2 + +T N2 +(cid:16) ++T N log N + N MtU + NtN + N log N ++N + +h +U Mt + U(N2 + +t + MtNt) + Nt + +. + +t + MtNt) + Nt + +(cid:17)i + +(52) + +5 + +0 + +10 +SINR [dB] + +20 + +5 + +0 + +10 +SINR [dB] + +20 + +5 + +0 + +10 +SINR [dB] + +20 + +(cid:16) + +(cid:17) + +Proof: Noticing that + +the complexities of evaluating +φ(W, θθθ), ϕk(θk), and f (W, θθθ) are, respectively, on the order +Nt Mt + N2 +of U Mt, Nt, and U +. One can easily show that +t +the complexity of the Algorithm 6 is the same as that of the +Algorithm 5. + +(cid:16) + +(cid:17) + +VII. Numerical Results + +In this section, we perform simulations to evaluate the per- +formances of the proposed FA approaches, i.e., FA approaches +for transmit beamforming, cognitive cognitive beamforming, +RIS-aided transmit beamforming, and RIS-aided WPT, and +compare them with their iterative, SDP, and SCA counterparts. +CVX package [40] is utilized to obtain the solution for +the cognitive SPD approach, i.e., problem (21), and the AO +approach for the RIS-aided transmit beamforming. In the AO +approach, two SDPs (33) and (34) are alternatively solved +in n0 = 10 iterations. The setup parameters for FAs are as +follows. The variation of the attractiveness γ is set at 1. The +penalty constants are set equal but they dynamically vary +as λi = ρk = n2, +i, k where n is the generation index in +Algorithm 1. The attractiveness at zero distance is β0 = 1. +Finally, the initial randomization factor is α(0) = 0.9 and its +value at the n-th generation is α(n) = α(0)0.9n. + +∀ + +A. Evaluation on Transmit Beamforming + +− + +We simulate a scenario of two users, i.e., U = 2, randomly +distributed within 2 km from their BS. The array antenna +gain at the BS is 15dBi. The noise power spectral density, +noise figure at each user and the subcarrier bandwidth are, +174 dBm/Hz, 5 dB and 15 kHz wide. The path +respectively, +loss model is 35 + 34.5 log 10(l), where l is in kilometers. A +log-normal shadowing with a standard deviation of 8 dB is +assumed. Furthermore, a complex Gaussian distribution is set +with the variance of 1/2 on each of its real and imaginary +components for the downlink channel fading coefficients. +Monte Carlo simulations have been carried out over 1000 +channel realizations. + +Fig. 1 illustrates the total transmit power of the proposed +FA approach and its iterative counterpart versus the required +SINR level with different numbers of BS’s antennas. The + +Fig. 1: The total BS’s transmit power versus the required SINR +level with different numbers of BS’s antennas: (a) 4 antennas; (b) +6 antennas; (c) 8 antennas. The firefly population is N = 30. The +number of maximum generations T = 30. + +results on Fig. 1 clearly show that the proposed FA approach +outperforms the iterative method in obtaining lower required +transmit power, i.e., around 3 to 4 dB lower, for all simulated +setups. The results in Fig. 1 confirm the ability of the proposed +FA in handling highly nonlinear and multimodal optimization +problems. This power saving gain, however, comes at the price +of a higher complexity. Using the parameter setup for Fig. 1 in +Lemmas 1 and 2, i.e., U = 2, T = N = 30, Mt = 4, 6, 8, one +can find the complexities of the Iterative and FA approaches +. When the +are, respectively, in the order of +number of antennas elements are large, letting T = N = Mt, +(cid:17) +it can be shown that the dominant terms of the complexities +M4 +of the Iterative and FA approach are in the order of +t +(cid:17) +, respectively. The trade off between the power +and +saving gain and computational complexity of the proposed FA +approach in comparison with the Iterative method should be +considered by the network designer/operator. + +O (cid:16) + +O (cid:16) + +O (cid:16) + +O (cid:16) + +M6 +t + +104 + +108 + +and + +(cid:17) + +(cid:17) + +Fig. 2 shows the total BS’s transmit power of the Iterative +and proposed FA versus the number of iteration/generations +with different numbers of BS’s antennas. The results indi- +cate that the Iterative approach converges after just 5 iter- +ations/generations while the proposed FA requires about 20 +generations/iterations to level off. + +Fig. 3 shows the total BS’s transmit power of the proposed +FA approach versus the number of population N with different +BS’s antenna elements. It can be seen that the observed curves +converge after N = 30. Our simulations indicate that the +proposed FA approach performs well with at least 30 fireflies +to solve (12) under the investigated SINR range. + +B. Evaluations on Cognitive Transmit Beamforming + +We first reproduce the result of the experiment described in +Example 1 of [3] to compare the proposed FA approach with +the SDP approach. In that experiment, three SUs are located +5◦, 10◦, 25◦, and two PUs are located at 30◦ and 50◦, +at + +− + + IEEE TRANSACTIONS ON WIRELESS COMMUNICATIONS, DOI: 10.1109/TWC.2023.3328713 + +11 + +40 + +35 + +30 + +25 + +20 + +15 + +10 + +5 + +] + +m +B +d +[ + +r +e +w +o +p +t +i +s +m +n +a +r +T + +(a) + +FA +Iterative + +40 + +35 + +30 + +25 + +20 + +15 + +10 + +5 + +] + +m +B +d +[ + +r +e +w +o +p +t +i +s +m +n +a +r +T + +(b) + +FA +Iterative + +40 + +35 + +30 + +25 + +20 + +15 + +10 + +5 + +] + +m +B +d +[ + +r +e +w +o +p +t +i +s +m +n +a +r +T + +(c) + +FA +Iterative + +-5 + +-10 + +-20 + +B +d + +-30 + +(a): SDP Approach + +-40 + +-90 -80 -70 -60 -50 -40 -30 -20 -10 0 + +10 20 30 40 50 60 70 80 90 + +Azimuth in degrees +(b): FA Approach + +-5 + +-10 + +-20 + +B +d + +-30 + +-40 + +-90 -80 -70 -60 -50 -40 -30 -20 -10 0 + +10 20 30 40 50 60 70 80 90 + +Azimuth in degrees + +0 + +10 + +0 +30 +Generations/Iterations + +20 + +0 + +10 + +0 +30 +Generations/Iterations + +20 + +0 + +10 + +0 +30 +Generations/Iterations + +20 + +Fig. 2: The total BS’s transmit power versus the generations/iteration +with different numbers of BS’s antennas: (a) 4 antennas; (b) 6 +antennas; (c) 8 antennas. The firefly population is N = 30. The +required SINR level at each user is 10 dB. + +Fig. 4: The radiation pattern of the BS with 8 antennas: (a) The +reproduction of [3, Fig. 3]; (b) The proposed FA approach with the +number of population N = 100. + +19.6 + +19.55 + +19.5 + +] + +m +B +d +[ + +r +e +w +o +p +t +i +s +m +n +a +r +T + +10 + +20 + +17.96 + +17.94 + +17.92 + +10 + +20 + +16.9 + +16.85 + +10 + +20 + +(a) + +30 + +40 +Number of population [N] +(b) + +50 + +30 + +40 +Number of population [N] +(c) + +50 + +30 + +40 +Number of population [N] + +50 + +60 + +70 + +60 + +70 + +60 + +70 + +Fig. 3: The total BS’s transmit power versus the number of population +with different numbers of BS’s antennas: (a) 4 antennas; (b) 6 +antennas; (c) 8 antennas. The number of generation is T = 30. The +required SINR level at each user is 10 dB. + +relative to the BS’s array broadside. The tolerable interference +level two PUs are Ito,1 = 0.001 and Ito,2 = 0.0001. The noise +variance is set to 0.1 while the required SINR values are set +to 1 for the SUs. + +The channel covariance matrices from the secondary BS +, and to PU k, i.e., Rp,k = +to SU t , i.e., Rs,t = R +R +, are the function of the angle of departure, i.e., ζs,t +(cid:1) +or ζp,k, and the standard deviation of the angular spread, i.e., +(cid:17) +δa. The (m, n)th entry of R (ζ, δa) is, [20]: + +ζs,t, δa +(cid:0) + +ζp,k, δa +(cid:16) + +e + +j2π∆ +ψ [(n +− + +m)sinζ]e− + +2 +h +where ψ is the carrier wavelength, σa = 2◦, and the antenna +spacing at the BS is set as ∆ = ψ/2. + +(53) + +m)cosζ + +π∆δa +ψ { + +}i + +(n + +− + +, + +2 + +Fig. 4 (a) illustrates the radiation patterns at the BS of the +SDP approach as described in (21), which is the reproduction + +of Fig. 3 in [3], while Fig. 4 (b) shows the radiation patterns +the BS of the FA approach proposed in Algorithm 4. +at +The results clearly indicate that the FA obtains the same +radiation pattern as the SDP approach does. Both approaches +are able to form nulls to the locations/angles where the PUs are +located. In other words, the proposed FA can obtain the same +optimal solution as the IPM does for the SDP counterpart. This +confirms the ability of the proposed FA in handling highly +nonlinear and multimodal optimization problems. + +With the setup in Fig. 4, i.e., Mt = 8, U = 3, K = 2, N = 100 +and, T = 80, one can easily verify from Lemmas 3 and 4 +that the proposed FA approach requires higher computational +complexity than the SDP approach does when it returns rank- +one optimal solution. When the number of antennas is large, +one can show that the dominant term of (28) is M6 1 +. On the +other hand, assuming T = N = Mt, the dominant term of +(29) is M6 +t . Hence, the complexity of an IPM to solve (21) +is slightly higher than the complexity of the proposed FA in +Algorithm 4, i.e., + +in comparison with + +M6 1 + +t + +2 + +2 + +Fig. 5 shows the transmit power of the proposed FA ap- +proach versus the number of population with different numbers +of transmit antennas. The results indicate that the proposed +FA converges with all number of antenna setups as all the +observed curves level off after the maximum size of population +of N = 50. However, the higher of the antenna elements is, the +larger the size of the population is required for a converged +transmit power. For example, with M = 8, 16, and 32, the +proposed FA approach, respectively, obtains a stable transmit +power at N = 30, 40 and 50. This is due to the fact that +the size of the system increases with a higher number of +antenna elements, i.e., a higher degree of freedom. As a result, +it requires a larger size of the population to provide a sufficient +diversification for the exploration of the FA. The results also +show that the required transmit power decreases when the +number of antennas increase as the result of having higher +degree of freedom. + +M6 +t + +. +(cid:17) + +O (cid:16) + +O (cid:18) + +t + +(cid:19) + + IEEE TRANSACTIONS ON WIRELESS COMMUNICATIONS, DOI: 10.1109/TWC.2023.3328713 + +12 + +10 + +5 + +0 + +-5 + +-10 + +-15 + +] + +B +d +[ + +r +e +w +o +p + +t +i + +m +s +n +a +r +T + +-20 + +10 + +20 + +30 + +50 + +70 +40 +Number of population N + +60 + +M=8 +M=16 +M=32 + +80 + +90 + +100 + +40 + +30 + +20 + +10 + +] + +m +B +d +[ + +r +e +w +o +p + +t +i + +m +s +n +a +r +T + +0 + +0 + +30 + +20 + +10 + +] + +m +B +d +[ + +r +e +w +o +p + +t +i + +m +s +n +a +r +T + +0 + +0 + +Mt=3, Nt=30 + +FA +AO + +4 + +8 + +12 + +16 + +20 + +Required SINR level [dB] +Mt=8, Nt=30 + +FA +AO + +4 + +8 + +12 + +16 + +20 + +Required SINR level [dB] + +40 + +30 + +20 + +10 + +] + +m +B +d +[ + +r +e +w +o +p + +t +i + +m +s +n +a +r +T + +0 + +0 + +30 + +20 + +10 + +] + +m +B +d +[ + +r +e +w +o +p + +t +i + +m +s +n +a +r +T + +0 + +0 + +Mt=3, Nt=20 + +FA +AO + +4 + +8 + +12 + +16 + +20 + +Required SINR level [dB] +Mt=8, Nt=20 + +FA +AO + +4 + +8 + +12 + +16 + +20 + +Required SINR level [dB] + +Fig. 5: The total transmit power of the proposed FA approach versus +the number of population with different numbers of transmit antennas. +The number of maximum generation T = 150. + +Fig. 7: The total BS’s transmit power versus the required SINR +level with different numbers of BS’s antennas and RIS’s reflective +elements. The firefly population is N = 120. The number of maximum +generations T = 50. + +M=8 +M=16 +M=32 + +] + +B +d +[ + +r +e +w +o +p + +t +i + +m +s +n +a +r +T + +50 + +40 + +30 + +20 + +10 + +0 + +-10 + +-20 + +0 + +20 40 60 80 100 120 140 160 180 200 220 240 260 280 300 + +Number of generations + +Fig. 6: The total +transmit power of the proposed FA approach +versus the number of maximum generations with different numbers +of transmit antennas. The number of population N = 70. + +Fig. 6 depicts the transmit power of the proposed FA +approach versus the number of maximum generations with +different numbers of transmit antennas. A similar trend as in +Fig. 5 is also observed in this figure. The transmit power +attained by the proposed FA approach converges with all +numbers of antenna setups. The higher number of antennas +is, the higher number of generations is needed as a result of +higher exploitation required for the increase of the problem +the transmit power levels off at +dimension. For instance, +around 90, 100, and 120 generations, respectively, for M = 8, +16, and 32. + +C. Evaluations on RIS-aided Transmit Beamforming + +We simulate a RIS-aided communication system which +consists of one BS, one RIS, and two users, i.e., U = 2. +The distance between the BS and the RIS is 10 m. Users + +− + +− + +− + +30 + +are randomly distributed with a distance of 6 m from the RIS. +The pathloss exponents of both wireless links from the BS +to the RIS and from the RIS to users are set to be 2.2 with +the signal attenuation at the reference distance of 1 m being +30 dB [23], i.e., the large-scale fading coefficient is modeled +22 log10(d) dB where d is the distance between the +as +BS to RIS or RIS to a user. The noise variance at each user +is +124 dBm. Monte Carlo simulations are carried over 100 +channel realizations. Each channel realization is associated +with a random user location and a random fading coefficient. +Fig. 7 illustrates the total BS’s transmit power versus the +required SINR level with different numbers of BS’s antennas +and RIS’s reflective elements. The results indicate that the +proposed FA prevails the AO approach in terms of lower power +consumption. The superior performance of the FA approach +over its AO counterpart can be explained as follows. As the +AO approach approximates non-convex problem (32) by two +convex sub-problems (33) and (34), the solution obtained by +the AO approach is not necessary the global optimal solution +of the original problem (32). On the other hand, the proposed +FA possessing both exploitation and exploration abilities can +effectively handle such non-convex problem and obtain much +better solution than its counterpart. The results shown on +Fig. 7 verify the ability of the proposed FA in handling highly +nonlinear and multimodal optimization problems. + +It can be observed from Fig. 7 that at a given number of +RIS’s reflective elements, the performance gap between the +proposed FA and the AO decreases when the number of BS’s +antennas increases. For example, when Nt = 20, the gaps are, +respectively, around 7.5 dB and 3.5 dB with Mt = 3 and +Mt = 8. Fortunately, at a given number of BS’s antennas, the +performance gap improves when the number of RIS’s elements +increases. For instance, with Mt = 8, the performance gap +increases from around 3.5 dB to 4.5 dB when Nt increases +from 20 to 30. Interestingly, the FA performs especially well +with a relatively high ratio of Nt/Mt, i.e., the performance gap + + IEEE TRANSACTIONS ON WIRELESS COMMUNICATIONS, DOI: 10.1109/TWC.2023.3328713 + +13 + +26 + +24 + +22 + +20 + +18 + +16 + +14 + +12 + +] + +m +B +d +[ + +r +e +w +o +p + +t +i + +m +s +n +a +r +T + +10 + +0 + +20 + +40 + +Mt:8, Nt:30 +Mt:8, Nt:20 +Mt:3, Nt:30 +Mt:3, Nt:20 + +34 + +32 + +30 + +28 + +26 + +24 + +22 + +] + +m +B +d +[ + +r +e +w +o +p + +t +i + +m +s +n +a +r +T + +160 + +180 + +200 + +20 + +20 + +40 + +60 + +Mt:8, Nt:30 +Mt:8, Nt:20 +Mt:3, Nt:30 +Mt:3, Nt:20 + +160 + +180 + +200 + +100 + +140 +80 +Number of Population (N) + +120 + +60 + +100 +Maximum number of generations (T) + +120 + +140 + +80 + +Fig. 8: The total BS’s transmit power versus the number of maximum +generations with different numbers of BS’s antennas and RIS’s +reflective elements. The firefly population is N = 120. The required +SINR level is 10 dB. + +Fig. 9: The total transmit power versus the number of populations +with different numbers of BS’s antennas and RIS’s reflective ele- +ments. The number of maximum generations T = 50. The required +SINR level is 20 dB. + +is around 9.5 dB with the ration of 30/3 while it is around +3.5 with the ratio of 20/8. The results can be explained as +follows. A higher number of RIS’s reflective elements gives +more degree of freedom for the FA to perform. Moreover, the +channel between the RIS and these users plays a higher role +than that between the BS and the RIS does as the former is +closer to these users. Last but not least, the performance gaps +slightly decrease at relatively high SINR level especially when +the Nt/Mt ratio is relatively low. For example with the ratio of +20/8, the performance gap is around 1.8 dB at SINR of 20 dB +compared with around 3.5 dB at the other SINR levels, i.e., +see the bottom-right corner figure of Fig. 7. This is because +of a fact that the FA has reached its limit of exploration with +N = 120 fireflies, at a stricter constraint condition. + +We now compare the computational complexities of the AO +and FA approaches for the experiments presented on Fig. 7. +As Nt is larger than Mt, from Lemma 5 one can show that +the dominant term of the complexity of the AO approach +6 1 +is n0N +. Similarly, from Lemma 6 one can conclude that +2 +t +the dominant term of the complexity of the FA approach is +T N3N2 +t . Substituting for Nt = 30, n0 = 10, N = 120 and +T = 50, we can arrive at the fact that the computational +complexities of the AO and FA approaches are on the same +. When the numbers of antennas Mt and Nt +order of +are large, letting Nt = n0 = Mt in (42), one can show that the +(cid:17) +dominant term of the complexity to attain ε-solution to (32) +is M7 1 +. On the other hand, one can derive the dominant term +of (45) as M6 +t when assuming T = N = Nt = Mt. Hence, +the complexity of an IPM to solve (32) is higher than the +M7 1 +complexity of the proposed FA in Algorithm 5, i.e., + +1010 + +O (cid:16) + +t + +2 + +2 + +in comparison with + +M6 +t + +. + +O (cid:16) + +(cid:17) + +In Fig. 8, the total BS’s transmit power is plotted versus +the maximum of generation T used in the FA in Algorithm 5 +with different BS’s antennas and RIS’s reflective elements. The +results indicate that the proposed FA requires around 50 to 60 + +O (cid:18) + +t + +(cid:19) + +generations to attain the optimal solution for all setups. + +Fig. 9 illustrates the total transmit power versus the number +of population N with different BS’s antennas and RIS’s +elements. The results show that increasing the size of the +firefly population enables the FA to obtain better solution. +For example, the total transmit power decreases around 7 +dB, 5.4 dB, 5 dB, and 3 dB, respectively, for the setups of +(Mt = 8, Nt = 20), (Mt = 3, Nt = 30), (Mt = 8, Nt = 20), +and (Mt = 3, Nt = 20) when the firefly population increases +from 20 to 120. The performance gap at the 20 dB SINR level +observed in Fig. 7 for (Mt = 8, Nt = 20) can be improved 1 dB +further when the population size is enlarged from 120 to 200. +These total-transmit-power curves converge after N = 180 as +the reduction in the total transmit power is negligible when +the population increases to N = 200 for all setups. + +D. Evaluations on RIS-aided WPT + +Here, we use the same setup for the RIS-aided commu- +nication system as considered in the previous section, i.e., +Section VII-C. However, the EHRs are randomly placed with +the distance of 2 m from the RIS. We run m0 = 10 iterations +to obtain the solution for the SCA approach. + +Fig. 10 shows the sum-power received at EHRs versus +BS’s maximum transmit power with different numbers of BS’s +antennas and RIS’s reflective elements. It is clear from the +figure that the proposed FA approach outperforms the SCA +approach in [23] in offering higher sum-power at EHRs. The +performance gaps are, respectively, around 18 dB, 17 dB, +15 dB, and 14 dB for the setups of (Mt = 3, Nt = 30), +(Mt = 8, Nt = 30), (Mt = 3, Nt = 20), and (Mt = 8, Nt = 20). +The superior performance of the proposed FA over the SCA +is due to the advantage of having exploitation and exploration +abilities to handle non-convex optimization problems. On the +other hand, the SCA employs the first-oder Taylor expansion +to approximate the optimization problem resulting in a lower- +bounded solution. Furthermore, the FA approach allocates + + IEEE TRANSACTIONS ON WIRELESS COMMUNICATIONS, DOI: 10.1109/TWC.2023.3328713 + +14 + +] + +m +B +d +[ + +s +R +H +E + +t +a + +r +e +w +o +p +- +m +u +S + +] + +m +B +d +[ + +s +R +H +E + +t +a + +r +e +w +o +p +- +m +u +S + +Mt=3, Nt=30 + +FA +SCA + +20 + +30 + +40 + +] + +m +B +d +[ + +s +R +H +E + +t +a + +r +e +w +o +p +- +m +u +S + +-80 + +-90 + +-100 + +-110 + +-120 + +10 + +Mt=3, Nt=20 + +FA +SCA + +20 + +30 + +40 + +-80 + +-90 + +-100 + +-110 + +-120 + +10 + +Maximun transmit power at BS [dBm] +Mt=8, Nt=30 + +-80 + +-90 + +-100 + +-110 + +-120 + +10 + +FA +SCA + +20 + +30 + +40 + +Maximun transmit power at BS [dBm] +Mt=8, Nt=20 + +-80 + +-90 + +-100 + +-110 + +-120 + +10 + +FA +SCA + +20 + +30 + +40 + +] + +m +B +d +[ + +s +R +H +E + +t +a + +r +e +w +o +p +- +m +u +S + +Maximun transmit power at BS [dBm] + +Maximun transmit power at BS [dBm] + +] + +m +B +d +[ + +s +R +H +E + +t +a +r +e +w +o +p +- +m +u +S + +-80 + +-85 + +-90 + +-95 + +-100 + +-105 + +0 + +20 + +40 + +Mt:8, Nt:30 +Mt:8, Nt:20 +Mt:3, Nt:30 +Mt:3, Nt:20 + +60 + +100 +Maximum number of generations (T) + +140 + +120 + +80 + +160 + +180 + +200 + +Fig. 10: Sum-power received at EHRs versus BS’s maximum transmit +power with different numbers of BS’s antennas and RIS’s reflective +elements. The firefly population is N = 100. The number of maximum +generations T = 50. + +Fig. 11: Sum-power received at EHRs versus the number of maximum +generations with different numbers of BS’s antennas and RIS’s +reflective elements. The firefly population is N = 100. The required +SINR level is 10 dB. + +one active beamforming vector for each EHR whereas the +SCA only uses one active beamforming vector for all EHRs. +The results shown on Fig. 10 again verify the ability of the +proposed FA in handling highly nonlinear and multimodal +optimization problems. + +Comparing Figs. 7 and 10, it can be observed that the +FA behaves in a similar manner for both power minimization +problem (35) and sum-power maximization problem (48). For +instance, at the same value of Mt, the higher the value of +Nt, the larger the performance gap is. At the same value of +Nt, the lower the value of Mt, the bigger the performance +gap is. The results also recommend to maintain a relatively +high ratio of Nt/Mt to attain the best performance of the FA. +Slight declines in the performance gaps are also observed at +the stricter constraint of BS’s transmit power, i.e., 40 dBm, as +the FA’s population reach their limit of exploration. + +We proceed by comparing the computational complexities +of the SCA and FA approaches for the experiments shown on +Fig. 10. As Nt is larger than Mt, from Lemmas 7 and 8, it is +clear that the dominant terms of the complexities of the SCA +t and T N3N2 +and the FA approaches are, respectively, m0N3 +t . +Substituting for Nt = 30, m0 = 10, N = 100 and T = 50, we +can arrive at the fact that the computational complexities of +the SCA and FA approaches are, respectively, on the orders of +. When the numbers of antennas Mt and +O (cid:16) +Nt are large, letting Nt = m0 = Mt in (51), one can show that +(cid:17) +the dominant term of the complexity of the SCA is M4 +t . On the +other hand, the dominant term of (52) is M6 +t when assuming +T = N = Nt = Mt. Hence, the complexity of the SCA approach +is lower than that of the proposed FA in Algorithm 6, i.e., + +1010 + +O (cid:16) + +105 + +and + +(cid:17) + +(cid:17) + +. +(cid:17) + +in comparison with + +M6 +M4 +t +t +O (cid:16) +O (cid:16) +Sum-power received at EHRs are shown versus the number +of maximum generations with different numbers of BS’s +antennas and RIS’s reflective elements in Fig. 11. The figure +reveals that the proposed FA converges after around 50 to 60 +generations for all observed setups. + +] + +m +B +d +[ + +s +R +H +E + +t +a + +r +e +w +o +p +- +m +u +S + +-83 + +-84 + +-85 + +-86 + +-87 + +-88 + +-89 + +-90 + +-91 + +-92 + +-93 + +0 + +20 + +40 + +Mt:8, Nt:30 +Mt:8, Nt:20 +Mt:3, Nt:30 +Mt:3, Nt:20 + +80 + +120 +60 +Number of Population (N) + +100 + +140 + +160 + +180 + +Fig. 12: Sum-power received at EHRs versus the number of popu- +lations with different numbers of BS’s antennas and RIS’s reflective +elements. The number of maximum generations T = 50. The required +SINR level is 20 dB. + +The effect of the firefly population on the sum-power +received at EHRs is illustrated on Fig. 12. The figure shows +that all the curves converge after the population size of 80. +However the difference between the EHRs’ sum-power offered +by 80 fireflies and that offered by 40 fireflies is no more +than 0.7 dB for all observed setups. This indicates that the +complexity of the proposed FA for the RIS-aided WPT sum- +power maximization problem in (48) can be reduced with an +acceptable tradeoff in the optimality. + +VIII. Conclusion +We have proposed a generalized FA to find optimal solution +for an optimization framework containing objective function +and constraints as multivariate functions of independent opti- +mization variables. We have adopted the proposed generalized + + IEEE TRANSACTIONS ON WIRELESS COMMUNICATIONS, DOI: 10.1109/TWC.2023.3328713 + +15 + +FA to solve four representative examples of classic trans- +mit beamforming, cognitive beamforming, RIS-aided transmit +beamforming, and RIS-aided wireless power transfer. Our +analyzes have indicated that the computational complexities +of proposed FA approaches are less than those of their IPM +counterparts, i.e., the SDP and the AO approaches, yet higher +than that of the iterative and SCA approaches in large-antenna +scenarios. Simulation results have revealed the fact that the +proposed FA attains the same optimal solution as the IMP +does for the under-investigated cognitive beamforming prob- +lem. Interestingly, the proposed FA outperforms the iterative, +AO, and SCA approaches for the under-investigated classic +transmit beamforming, RIS-aided transmit beamforming, and +wireless power transfer problems, respectively. This confirms +the effectiveness of the proposed generalized FA in handling +multivariate and non-convex problems. + +References + +[11] Y. Huang and D. P. Palomar, “Rank-constrained separable semidefinite + +programming with applications to optimal beamforming,” IEEE Trans. + +Signal Process., vol. 58, no. 2, pp. 664– 678, Feb. 2010. + +[12] Y. Huang, Q. Li, W.-K. Ma, and S. Zhang, “Robust multicast beamform- + +ing for spectrum sharing-based cognitive radios,” IEEE Trans. Signal + +Process., vol. 60, no. 1, pp. 527– 533, Jan. 2012. + +[13] B. Clerckx, R. Zhang, R. Schober, D. W. K. Ng, D. I. Kim, and H. V. + +Poor, “Fundamentals of wireless information and power transfer: From + +RF energy harvester models to signal and system designs,” IEEE J. Sel. + +Areas in Commun., vol. 37, no. 1, pp. 4–33, Jan. 2019. + +[14] D. W. K. Ng, E. S. Lo, and R. Schober, “Robust beamforming for + +secure communication in systems with wireless information and power + +transfer,” IEEE Trans. Wireless Commun., vol. 13, no. 8, pp. 4599–4615, + +Aug. 2014. + +[15] ——, “Wireless information and power transfer: Energy efficiency opti- + +mization in OFDMA systems,” IEEE Trans.Wireless Commun., vol. 12, + +no. 12, pp. 6352–6370, Dec. 2013. + +[1] F. Rashid-Farrokhi, K. J. R. Liu, and L. Tassiulas, “Transmit beamform- + +[16] T. A. Le, Q.-T. Vien, H. X. Nguyen, D. W. K. Ng, and R. Schober, + +ing and power control for cellular wireless systems,” IEEE J. Sel. Areas + +“Robust chance-constrained optimization for power-efficient and secure + +Commun., vol. 16, no. 8, pp. 1437– 1450, Oct. 1998. + +SWIPT systems,” IEEE Trans. Green Commun. and Netw., vol. 1, no. 3, + +[2] A. Wiesel, Y. C. Eldar, and S. Shamai, “Linear precoding via Conic + +pp. 333–346, Sep. 2017. + +optimization for fixed MIMO receivers,” IEEE Trans. Signal Process., + +[17] W. Yu and T. Lan, “Transmitter optimization for the multi-antenna + +vol. 54, no. 1, pp. 161– 176, Jan. 2006. + +downlink with per-antenna power constraints,” IEEE Trans. Signal + +[3] Y. Huang and D. P. Palomar, “Rank-constrained separable Semidefinite + +Process., vol. 55, no. 6, pp. 2646–2660, Jun. 2007. + +programming with applications to optimal beamforming,” IEEE Trans. + +[18] T. A. Le and K. Navaie, “Downlink beamforming in underlay cognitive + +Signal Process., vol. 58, no. 2, pp. 644–678, Feb. 2010. + +cellular networks,” IEEE Trans. Commun., vol. 62, no. 7, pp. 2212–2223, + +[4] H. Dahrouj and W. Yu, “Multicell interference mitigation with joint + +Jul. 2014. + +beamforming and common message decoding,” IEEE Trans. Commun., + +[19] Z.-Q. Luo, W.-K. Ma, A. M.-C. So, Y. Ye, and S. Zhang, “Semidefinite + +vol. 59, no. 8, pp. 2264–2273, Aug. 2011. + +relaxation of quadratic optimization problems,” IEEE Signal Process. + +[5] W. Yang and G. Xu, “Optimal downlink power assignment for smart + +Mag., vol. 27, no. 3, pp. 20–34, May 2010. + +antenna systems,” in Proc. IEEE Int. Conf. Acoustics, Speech and Signal + +[20] M. Bengtsson and B. Ottersten, “Optimal downlink beamforming using + +Process., ICASSP ’98, vol. 6, 1998, pp. 3337–3340. + +Semidefinite optimization,” in Proc. 37th Annu. Allerton Conf. Com- + +[6] M. Schubert and H. Boche, “Solution of the multiuser downlink beam- + +mun., Control, and Comput., 1999, pp. 987 – 996. + +forming problem with individual SINR constraints,” IEEE Trans. Veh. + +[21] Z. Peng, Z. Chen, C. Pan, G. Zhou, and H. Ren, “Robust transmission + +Technol., vol. 53, no. 1, pp. 18–28, Jan. 2004. + +design for RIS-aided communications with both transceiver hardware + +[7] D. Hammarwall, M. Bengtsson, and B. Ottersten, “On downlink beam- + +impairments and imperfect CSI,” IEEE Wireless Commun. Lett., vol. 11, + +forming with indefinite shaping constraints,” IEEE Trans. Signal Pro- + +no. 3, pp. 528–532, Mar. 2022. + +cess., vol. 54, no. 9, pp. 3566–3580, Sep. 2006. + +[22] S. Gong, C. Xing, P. Yue, L. Zhao, and T. Q. S. Quek, “Hybrid analog + +[8] E. Bj¨ornson, M. Bengtsson, and B. Ottersten, “Optimal multiuser trans- + +and digital beamforming for RIS-assisted mmWave communications,” + +mit beamforming: A difficult problem with a simple solution structure + +IEEE Trans. Wireless Commun., vol. 22, no. 3, pp. 1537–1554, Mar. + +[lecture notes],” IEEE Signal Process. Mag., vol. 31, no. 4, pp. 142–148, + +2023. + +Jul. 2014. + +[23] Q. Wu and R. Zhang, “Weighted sum power maximization for intelligent + +[9] G. Zheng, K.-K. Wong, and T.-S. Ng, “Throughput maximization in + +reflecting surface aided SWIPT,” IEEE Wireless Commun. Letters, vol. 9, + +linear multiuser MIMO–OFDM downlink systems,” IEEE Trans. Veh. + +no. 5, pp. 586–590, May 2020. + +Technol., vol. 57, no. 3, pp. 1993–1998, May 2008. + +[24] X.-S. Yang, Nature-Inspired Metaheuristic Algorithms. Luniver Press, + +[10] R. Bhagavatula and R. W. Heath, “Adaptive limited feedback for sum- + +2008. + +rate maximizing beamforming in cooperative multicell systems,” IEEE + +[25] S. Boyd and L. Vandenberghe, Convex Optimization. + +Cambridge + +Trans. Signal Process., vol. 59, no. 2, pp. 800–811, Feb. 2011. + +University Press, 2004. + + IEEE TRANSACTIONS ON WIRELESS COMMUNICATIONS, DOI: 10.1109/TWC.2023.3328713 + +16 + +[26] X.-S. Yang, Engineering optimisation: an introduction with metaheuris- + +constrained optimization for IRS-Aided MISO communication systems,” + +tic applications. Wiley, 2009. + +IEEE Wireless Commun. Lett., vol. 10, no. 1, pp. 1–5, Jan. 2021. + +[27] ——, “Chapter 13: Firefly algorithm: Variants and applications,” in + +[43] H. Yu, H. D. Tuan, A. A. Nasir, T. Q. Duong, and H. V. Poor, “Joint + +Swarm Intelligence Algorithms. CRC Press, 2020, pp. 175–186. + +design of reconfigurable intelligent surfaces and transmit beamforming + +[28] T. Yamanaka and K. Higuchi, “Transmitter beamforming control based + +under proper and improper gaussian signaling,” IEEE J. Sel. Areas + +on firefly algorithm for massive MIMO systems with per-antenna power + +Commun., vol. 38, no. 11, pp. 2589–2603, Nov. 2020. + +constraint,” in 2017 23rd Asia-Pacific Conf. Commun. (APCC), 2017, + +[44] N. S. Perovi´c, L.-N. Tran, M. D. Renzo, and M. F. Flanagan, “Optimiza- + +pp. 1–6. + +[29] T. A. Le and X.-S. Yang, “Firefly algorithm for beamforming design in + +RIS-aided communications systems,” in Proc. IEEE Veh. Techno. Conf. + +(VTC 2023-Spring), Jun. 2023, pp. 1–5. + +[30] I. Fister, I. F. Jr., X.-S. Yang, and J. Brest, “A comprehensive review of + +firefly algorithms,” Swarm and Evolutionary Computation, vol. 13, pp. + +34–46, Dec. 2013. + +tion of RIS-aided MIMO systems via the cutoff rate,” IEEE Wireless + +Communications Letters, vol. 10, no. 8, pp. 1692–1696, Aug. 2021. + +[31] X.-S. Yang and X.-S. He, Why the Firefly Algorithm Works? Cham: + +Springer International Publishing, 2018, pp. 245–259. + +Tuan Anh Le (S’10-M’13-SM’19) + +received the + +Ph.D. degree in telecommunications research from + +[32] W. Windarto and E. Eridani, “Comparison of particle swarm optimiza- + +King’s College London, The University of London, + +tion and firefly algorithm in parameter estimation of Lotka-Volterra,” + +U.K., in 2012. He was a Post-Doctoral Research + +AIP Conference Proceedings, vol. 2268, no. 1, p. 050008, 09 2020. + +Fellow with the School of Electronic and Electri- + +[33] R. Ezzeldin, M. Zelenakova, H. F. Abd-Elhamid, K. Pietrucha-Urbanik, + +and S. Elabd, “Hybrid optimization algorithms of firefly with ga and pso + +cal Engineering, University of Leeds, Leeds, U.K. + +He is a Senior Lecturer at Middlesex University, + +for the optimal design of water distribution networks,” Water, vol. 15, + +London, U.K. His current research interests include integrated sensing and + +no. 10, 2023. + +communication (ISAC), RIS-aided communication, RF energy harvesting and + +[34] M. Clerc and J. Kennedy, “The particle swarm - explosion, stability, + +wireless power transfer, physical-layer security, nature-inspired optimization, + +and convergence in a multidimensional complex space,” IEEE Trans. + +and applied machine learning for wireless communications. He severed as a + +Evolutionary Computation, vol. 6, no. 1, pp. 58–73, 2002. + +Technical Program Chair for 26th International Conference on Telecommuni- + +[35] D. Bertsimas and J. Tsitsiklis, “Simulated annealing,” Statistical Science, + +cations (ICT 2019). He was an Exemplary Reviewer of IEEE Communications + +vol. 8, no. 1, pp. 10–15, 1993. + +Letters in 2019. + +[36] X.-S. Yang, Cuckoo Search and Firefly Algorithm: Theory and Applica- + +tions. Studies in Computational Intelligence, 2014. + +[37] E. Osaba, X.-S. Yang, F. Diaz, E. Onieva, A. D. Masegosa, and A. Peral- + +los, “A discrete firefly algorithm to solve a rich vehicle routing problem + +modelling a newspaper distribution system with recycling policy,” Soft + +Computing, vol. 21, pp. 5295–5308, 2017. + +[38] H. Dahrouj and W. Yu, “Coordinated beamforming for the multicell + +multi-antenna wireless system,” IEEE Trans. Wireless Commun., vol. 9, + +no. 5, pp. 1748–1759, May 2010. + +[39] T. A. Le and M. R. Nakhai, “Downlink optimization with interference + +pricing and statistical CSI,” IEEE Trans. Commun., vol. 61, no. 6, pp. + +2339–2349, Jun 2013. + +[40] CVX Research Inc., “CVX: Matlab software for disciplined convex + +programming, academic users,” http://cvxr.com/cvx, 2015. + +[41] K.-Y. Wang, A. M.-C. So, T.-H. Chang, W.-K. Ma, and C.-Y. Chi, + +“Outage constrained robust transmit optimization for multiuser MISO + +downlinks: Tractable approximations by conic optimization,” IEEE + +Trans. Signal Process., vol. 62, no. 21, pp. 5690–5705, Nov. 2014. + +[42] T. A. Le, T. V. Chien, and M. D. Renzo, “Robust probabilistic- + +Xin-She Yang obtained his DPhil in Applied Math- + +ematics from the University of Oxford. He then + +worked at Cambridge University and National Phys- + +ical Laboratory (UK) as a Senior Research Scientist. + +Now he is Reader at Middlesex University London, + +and a co-Editor of the Springer Tracts in Nature- + +Inspired Computing. He is also an elected Fellow + +of the Institute of Mathematics and its Applications. He was the IEEE + +Computational Intelligence Society (CIS) chair for the Task Force on Business + +Intelligence and Knowledge Management (2015 to 2020). He has published + +more than 300 peer-reviewed research papers with more than 84,000 citations, + +and he has been on the prestigious list of highly-cited researchers (Web of + +Sciences) for eight consecutive years (2016-2023). + diff --git a/scratch/goodfile.md b/scratch/goodfile.md new file mode 100644 index 0000000..4f89cbb --- /dev/null +++ b/scratch/goodfile.md @@ -0,0 +1,31 @@ +arXiv:2310.18460v1 [cs.IT] 27 Oct 2023IEEETRANSACTIONSONWIRELESSCOMMUNICATIONS,DOI:10.1109/TWC.2023.33287131GeneralizedFireflyAlgorithmforOptimalTransmitBeamformingTuanAnhLeandXin-SheYangAbstract—ThispaperproposesageneralizedFireflyAlgorithm(FA)tosolveanoptimizationframeworkhavingobjectivefunc-tionandconstraintsasmultivariatefunctionsofindependentoptimizationvariables.FourrepresentativeexamplesofhowtheproposedgeneralizedFAcanbeadoptedtosolvedown-linkbeamformingproblemsareshownforaclassictransmitbeamforming,cognitivebeamforming,reconfigurable-intelligent-surfaces-aided(RIS-aided)transmitbeamforming,andRIS-aidedwirelesspowertransfer(WPT).Complexityanalyzesindicatethatinlarge-antennaregimestheproposedFAapproachesrequirelesscomputationalcomplexitythantheircorrespondinginteriorpointmethods(IPMs)do,yetdemandahighercomplexitythantheiterativeandthesuccessiveconvexapproximation(SCA)approachesdo.SimulationresultsrevealthattheproposedFAattainsthesameglobaloptimalsolutionasthatoftheIPMforanoptimizationproblemincognitivebeamforming.Ontheotherhand,theproposedFAapproachesoutperformtheiterative,IPMandSCAintermsofobtainingbettersolutionforoptimizationproblems,respectively,foraclassictransmitbeamforming,RIS-aidedtransmitbeamformingandRIS-aidedWPT.IndexTerms—Fireflyalgorithm,nature-inspiredoptimization,transmitbeamforming,reconfigurableintelligentsurfaces.I.IntroductionTransmitbeamformingproblemsarenormallycastasoptimizationproblemswherebeamformingvectorsareoptimizationvariables.Twofundamentaloptimizationprob-lemsintransmitbeamforminginclude:i)minimizingthetotaltransmitpowersubjecttosignal-to-interference-plus-noise-ratio(SINR)constraints[1]–[4];ii)maximizingtheweakestSINRsubjecttoatotalpowerconstraint[5],[6].Infact,thesetwoproblemsareequivalent[7],[8].Ageneralizedversionofthesecondproblemisintroducedin[8]wheretheobjectiveistomaximizeanarbitraryutilityfunctionofSINRs,whichisstrictlyincreasingineveryreceiver’sSINR,subjecttoapowerconstraint.Theothervariationofthesecondoptimizationproblemisthesumratemaximization[9],[10].Furthermore,additionalconstraintscanbeintroducedtothesefundamentalproblemstocaptureotherwirelesscommunicationapplica-tions.Forinstance,asoft-shapinginterferenceconstraintwasaddedforcognitiveradioscenarios[11],[12]whileapowertransferconstraintwasincludedforsimultaneous-wireless-information-and-power-transferscenarios[13].Inaddition,variousmetricshavebeenutilizedtoformulatedownlinkbeamformingoptimizationproblemssuchassecrecycapacityT.A.LeandX.-S.YangarewiththeFacultyofScienceandTechnology,MiddlesexUniversity,London,NW44BT,UK.Email:{t.le;x.yang}@mdx.ac.uk.ThispaperhasbeenpresentedinpartattheIEEEVehicularTechnologyConference(VTC2023-Spring),Florence,Italy,June,20-23,2023.[14],energyefficiency[15],datatransmissionreliability,datatransmissionsecurity,andpowertransferreliability[16].SincetheSINRisanon-convexquadraticfunctionofthebeamformingvectors,thetwofundamentalbeamformingoptimizationproblemsareNP-hardandcannotbesolvedinpolynomialtime.Fortunately,exploitingthehiddenconvexitypropertyoftheSINRmetric,anelegantframeworkwasproposedin[2]toconvertthesetwooptimizationproblemsintoconvexconicprogrammingforms,whichcanbeef-fectivelysolvedbyastandardinteriorpointmethod(IPM).Furthermore,uplink-downlinkdualitywasutilizedtoderiveiterativealgorithmstofindoptimalbeamformingvectorsforsomepowerminimizationproblems,e.g.,[1],[4],[17],[18].Aniterativealgorithmwasintroducedin[9]toattainoptimalbeamformingvectorsforthesumratemaximization.Numeroustransmitbeamformingproblemscanberealizedinquadraticallyconstrainedquadraticprograms(QCQPs)ofbeamformingvectors,whicharemostlynon-convex[11],[19].TosolveaQCQPproblem,asemidefiniterelaxationtechnique[20]isadoptedinwhichtheoriginalQCQPisconvertedtoaconvexsemidefiniteprogramming(SDP)withnewopti-mizationvariablesasbeamformingmatrices.IfsolvingthetransformedSDPyieldsarank-oneoptimalbeamformingmatrix,thenthisoptimalmatrixisalsotheoptimalsolutiontotheoriginalQCQP.Otherwise,anapproximatedsolutiontotheoriginalQCQPcanbeobtainedbyexploitingsomerank-oneapproximationsortheGaussianrandomizeprocedure[19].Unfortunately,obtainingsuchsolutionrequiresfurthercomputationalresourcesyetresultsinasub-optimalsolution.Optimizationvariablesfordownlinkbeamformingproblemsmayincludedifferenttypesofbeamformingvectors.Forexam-ple,inareconfigurable-intelligent-surface-aided(RIS-aided)communicationsystem,seee.g.,[21],[22]andreferencestherein,theoptimizationvariablesareactivebeamformingvectorsforthebasestation(BS)andapassivebeamformimgvectorfortheRIS.Theobjectivefunctionand/orconstraintsforaRIS-aidedcommunicationsystemarefunctionsofbothactiveandpassivebeamformingvectors.Thesebeamformingvectorsareindependentvariablesyetneedtobejointlyop-timizedmakingtheirproblemsnon-convex.Widelyadoptedapproachesfortacklingsuchproblemsaretoiterativelysolvetwosub-optimizationproblems,a.k.a.,alternativeoptimization(AO)approach[21],ortoapproximateanon-convexusingfirst-orderTaylorexpansion,a.k.a.,successiveconvexapprox-imation(SCA)[23].InanAOapproach,eachofthesetwosub-optimizationproblems,onevariableistreatedasaconstantwhilesolvingfortheother.Thesesub-optimizationproblemsthemselvesaremostlyinQCQPforms.Duetotheinherent + +IEEETRANSACTIONSONWIRELESSCOMMUNICATIONS,DOI:10.1109/TWC.2023.33287132non-convexitycharacteroftheoriginalandsub-optimizationproblems,theresultingactiveandpassivebeamformingvec-torsmaynotbetheglobalsolutions.WhereasinaSCAapproach,alower(orupper)boundedsolutionisnormallyattained.IPMs,a.k.a.,barriermethods,aregradientbasedalgorithmsbeinggoodatexploitation,1a.k.a.,intensification,hence,theyareregardedaseffectivemethodstosolveconvexoptimizationproblems[25].Unfortunately,mostoftransmitbeamformingproblemsarenon-convex.Solvingnon-convexoptimizationproblemsrequiresalgorithmshavingbetterexploration2abilitythanthatoftheIPMstoavoidgettingtrappedinalocalmode.Fireflyalgorithm(FA),i.e.,anature-inspiredalgo-rithm,possessesbothexploitationandexplorationabilities.Consequently,FAisagoodcandidateforsolvingnon-convexdownlinkbeamformingproblems.FAisaneasy-to-implement,simple,andflexiblealgorithmbasedontheflashingchar-actersandbehaviouroftropicalfireflies[24].FAwasfirstdevelopedandpublishedbyXin-SheYang,respectively,inlate2007andin2008[24],[26]foroptimizationproblemswithobjectiveandconstrainsbeingfunctionsofasingleoptimizationvariable.AlthoughFAhasbeenwidelyappliedtomanyapplications[27],therehasnotbeenanysignificantworkinvestigatingtheapplicationofFAinsolvingtransmitbeamformingproblems.TherewereonlytwoattemptstoadoptFAforathroughputmaximizationproblemin[28]andforapowerminimizationproblemin[29].Asthesetwoattemptsonlycapturetwofundamentaltransmitbeamformingproblems,itisnotclearhowFAcanbeadoptedtosolveothertypesoftransmitbeamformingproblems.ThispapertakesafurthersteponimplementingFAtosolveawiderrangeoftransmitbeamformingoptimizationproblems.Thecontributionsofthepapercanbesummarizedasfollows.•ThepaperproposesageneralizedFAtofindtheoptimalsolutionofanoptimizationframeworkwhereitsobjectivefunctionandconstraintsaremultivariatefunctionsofmultipleindependentoptimizationvariables.Theprob-lemsin[28]and[29]areonlytwospecialcasesoftheproposedgeneralizedFAwhiletheproposedgeneralizedFAiscapableofhandlingalargerrangeoftransmitbeamformingproblems.•ThepapershowsfourrepresentativeexamplesofhowthegeneralizedFAcanbeadoptedforsolvingtransmitbeamformingproblems,i.e.,aclassictransmitbeamform-ingapproach,acognitivebeamformingapproach,aRIS-aidedbeamformingapproach,andRIS-aidedwirelesspowertransfer(WPT)approach.TheapplicationsoftheproposedgeneralizedFAarebeyondthesefourexampleswhichareonlygiventoshowcasehowdifferenttypesofbeamformingproblemscanbehandledbythegeneralizedFA.•Forthesakeofcompletenessandcomparison,theiterativeclosedformorSDPformsoftheunderinvestigatedbeam-formingapproachesarerepresented.Thepaperanalyzes1Exploitationistheabilityofusinganyinformationfromtheproblemofinteresttoformnewsolutionswhicharebetterthanthecurrentones[24].2Explorationistheabilityofefficientexploringthesearchspacetoformnewsolutionswithsufficientdiversityandfarfromtheexistingones[24].andcomparesthecomplexitiesoftheiterativeorSDPandFAimplementationsofeachbeamformingapproach.•SimulationsarecarriedouttoevaluatetheperformancesoftheproposedFAsfortheclassictransmitbeamforming,cognitivebeamforming,RIS-aided,andRIS-aidedWPTbeamformingapproaches.Notation:LoweranduppercaseletteryandY:ascalar;boldlowercaselettery:acolumnvector;bolduppercaseletterY:amatrix;‖·‖:theEuclideannorm;(·)T:thetransposeoperator;(·)H:thecomplexconjugatetransposeoperator;Tr(·):thetraceoperator;Y0:Yispositivesemidefinite;Ix:anx×xidentitymatrix;O:thebigOnotation;CM×1:thesetofallM×1vectorswithcomplexelements;HM×M:thesetofallM×MHermitianmatrices;y∼CN(0,σ2):yisazero-meancircularlysymmetriccomplexGaussianrandomvariablewithvarianceσ2;diag(y):adiagonalmatrixwhosediagonalelementsaretheentriesofvectory;andfinallydiag(Y):avectorwhoseentriesarethediagonalelementsofmatrixY.II.GeneralizedFireflyAlgorithmFrameworkA.ProposedGeneralizedFireflyAlgorithmFrameworkTheFAwasdevelopedbasedonthefollowingthreeide-alizedrules[24],[26].First,anyfireflyattractsotherfirefliesregardlessofitssex.Second,theattractivenessofanyfireflytotheotheroneisproportionaltoitsbrightness.Bothattrac-tivenessandbrightnessdecreaseasthedistancebetweenthesetwofirefliesincreases.Giventwoflashingfireflies,thedarkerfireflywillmovetowardsthebrighterone.Ifafireflydoesnotfindanybrighterone,itwillmakearandommove.Third,thebrightnessofafireflydependsonthelandscapeoftheobjectivefunction.Inthissection,weproposeageneralizedFAtofindoptimalsolutionforanoptimizationframeworkcontainingbothobjectiveandconstraintsasmultivariatefunctionsofindependentvariables.Tothatend,wefirstintroducethefollowingoptimizationframework.minimizeA,B,···,Zf(A,B,···,Z),subjecttogl(A,B,···,Z)≤0,l∈{1,2,...,L},hk(A,B,···,Z)=0,k∈{1,2,...K},(1)whereA∈CMa×Na,B∈CMb×Nb,···,Z∈CMz×Nz,i.e.,Ma,Na,Mb,Nb,···,Mz,Nz≥1,aredecisionvariables,a.k.a.,optimizationvariables.Dependingonthethevaluesof{Ma,Na,Mb,Nb,···,Mz,Nz},thedecisionvariablescanbematrices,vectors,scalars,orthecombinationofall.Wecontinuebyusingthepenaltymethod[24],[26]toequivalentlyrewrite(1)as:minimizeA,B,···,Zf(A,B,···,Z)+P(A,B,···,Z),(2)whereP(A,B,···,Z)isthepenaltytermdefinedas:P(A,B,···,Z)=L∑l=1λlmax{0,gl(A,B,···,Z)}2+K∑k=1ρk{hk(A,B,···,Z)}2.(3) + +IEEETRANSACTIONSONWIRELESSCOMMUNICATIONS,DOI:10.1109/TWC.2023.33287133In(3),λl>0,∀l,andρk>0,∀k,arepenaltyconstants.Let{Ai,Bi,···,Zi}bethei-thfireflyamongstthepopulationofNfireflies,i.e.,i∈{1,2,···,N}.FollowingthesecondruleoftheFA,thebrightestfireflyisthemostattractiveone.Sincetheproposedoptimizationframeworkisaminimization,wedefinethebrightnessoffireflyias:3Ii(Ai,Bi,···,Zi)=1f(Ai,Bi,···,Zi)+P(Ai,Bi,···,Zi).(4)Foranytwofirefliesi,j∈{1,2,···,N},ifIj(Aj,Bj,···,Zj)>Ii(Ai,Bi,···,Zi),thenfireflyiwillmovetowardsfireflyjat(n+1)-thgenerationas:A(n+1)i=A(n)i+βa,0e−γx(r(n)a,ij)2(A(n)j−A(n)i)+α(n)aΛΛΛ(n)a,i,(5)B(n+1)i=B(n)i+βb,0e−γy(r(n)b,ij)2(B(n)j−B(n)i)+α(n)bΛΛΛ(n)b,i,(6)...Z(n+1)i=Z(n)i+βz,0e−γz(r(n)z,ij)2(Z(n)j−Z(n)i)+α(n)zΛΛΛ(n)z,i,(7)wherer(n)a,ij=||A(n)j−A(n)i||,r(n)b,ij=||B(n)j−B(n)i||,···,r(n)z,ij=||Z(n)j−Z(n)i||aretheCartesiandistanceswhicharenotnecessaryEuclideandistancesyettheycanbeanymeasureeffectivelycharacterizedthequantitiesofinterestintheoptimizationproblem;βa,0,βb,0,···,βz,0are,respectively,theattractivenessatr(n)a,ij=0,r(n)b,ij=0,···,r(n)z,ij=0;finallyγa,γb,···,γzpresentthevariationsoftheattractiveness.Thesecondtermsin(5),(6),and(7)capturetheattractions.Thethirdtermsin(5),(6),and(7)arerandomizationswithrandomizationfactorsα(n)a,α(n)b,···,α(n)zandΛΛΛ(n)a,i∈CMa×Na,ΛΛΛ(n)b,i∈CMb×Nb,···,ΛΛΛ(n)z,i∈CMz×NzbeingmatricesofrandomnumbersdrawnfromaGaussianoranuniformdistribution.TheproposedgeneralizedFAforsolvingtheoptimizationframework(1)issummarizedinAlgorithm1,whereTisthemaximumgenerationofthealgorithm.Foranyparticularoptimizationproblemsubsumedundertheframework,thecorrespondingFAwillhavethesamestepsasthoseinAlgorithm1excepttheinput,step3,step16,step18,step19,andthereturnvalue.B.AsymptoticConvergenceandOptimalitySincethefireflyalgorithm,likequiteafewothernature-inspiredalgorithms,isametaheuristicalgorithm,thereisnorigorousproofofconvergencesofarinthecurrentliterature,despitemanyapplicationsofsuchmetaheuristicalgorithms.Inthissection,weprovidesomeintuitivediscussionsontheoptimalityandconvergenceoftheFAframework.41)AsymptoticOptimality:Withoutlossofgenerality,letγa=γb=···=γz=γ,weconsidertwospecialcasesofthevariationsoftheattractivenesswhenγ→0andγ→∞.Whenγ→0,itisclearthate−γ(r(n)a,ij)2→1,e−γ(r(n)b,ij)2→1,···,e−γ(r(n)z,ij)2→1.Thereforetheattractivenessesin(5),(6),and(7)areconstantand,respectively,equaltoβa,0,βb,0,andβz,0.Equivalently,itisanidealizedskyscenariowherethe3Notethatif(1)isamaximizationproblem,then(2)canbeexpressedas:minimizeA,B,···,Z−f(A,B,···,Z)+P(Ai,Bi,···,Zi).4MathematicalanalysisoftheFA’soptimalityandconvergencedeservesanimportantresearchtopic.Suchanalysisispostponedtofutureresearchduetothespaceconstraint.Algorithm1GeneralizedFireflyAlgorithmforsolving(1)1:Input:FAparameters:N,T,λt,ρk,βa,0,βb,0,···,βz,0,γa,γb,···,γz;Optimizationdata:thestructures/parametersoffunctionsf(A,B,···,Z),gl(A,B,···,Z),hk(A,B,···,Z);2:RandomlygenerateNpopulations{{A1,B1,···,Z1},{A2,B2,···,Z2},···,{AN,BN,···,ZN}};3:EvaluatethelightintensitiesofNpopulationas(4);4:RankthefirefliesinadescendingorderofIi(Ai,Bi,···,Zi);5:Definethecurrentbestsolution:I⋆:=I1(A⋆,B⋆,···,Z⋆);{A⋆,B⋆,···,Z⋆}:={A1,B1,···,Z1};6:forn=1:Tdo7:fori=1:Ndo8:forj=1:Ndo9:ifIi(Ai,Bi,···,Zi)>I⋆then10:I⋆:=Ii(Ai,Bi,···,Zi);{A⋆,B⋆,···,Z⋆}:={Ai,Bi,···,Zi};11:endif12:ifIj({Aj,Bj,···,Zj})>I⋆then13:I⋆:=Ij(Aj,Bj,···,Zj);{A⋆,B⋆,···,Z⋆}:={Aj,Bj,···,Zj};14:endif15:ifIj(Aj,Bj,···,Zj)>Ii(Ai,Bi,···,Zi)then16:Movefireflyitowardsfireflyjas(5)-(7);17:endif18:Attractivenessvarieswithdistancesviae−γa(r(n)a,ij)2,e−γb(r(n)b,ij)2,···,e−γz(r(n)z,ij)2;19:Evaluatenewsolutionsandupdatelightinten-sityas(4);20:endfor21:endfor22:RankthefirefliesinadescendingorderofIi(Ai,Bi,···,Zi);23:Updatethecurrentbestsolution:I⋆:=I1(A⋆,B⋆,···,Z⋆);{A⋆,B⋆,···,Z⋆}:={A1,B1,···,Z1};24:endfor25:return{A⋆,B⋆,···,Z⋆}.brightnessofeachfireflydoesnotchangeoverthedistance,whichcanbeseeneverywhere.Consequently,aglobalopti-mumcanbeobtained.Ontheotherhand,whenγ→∞,itisobviousthate−γ(r(n)a,ij)2→0,e−γ(r(n)b,ij)2→0,···,e−γ(r(n)z,ij)2→0,indicatingthattheattractivenessofeachfireflyiszero.Equivalently,eachfireflyisrandomlyinaheavilyfoggyregionandcannotbeseenbytheothers.Eachwillrandomlymoveandtheoptimalityisnotalwaysguaranteed.Inthiscase,FAisequivalenttoarandomsearchapproach.Infact,theattractivenessisinbetweenthesetwoextremecases,i.e.,0<γ<∞.Thevalueofγ−0.5definestheaveragedistanceofaherdoffirefliesbeingseenbyitsadjacentherds.Hence,theentirepopulationcanbeseparatedintonumberofherds.ThisautomaticdivisionpropertyprovidesFAsuitable + +IEEETRANSACTIONSONWIRELESSCOMMUNICATIONS,DOI:10.1109/TWC.2023.33287134abilityofhandlinghighlynonlinearandmultimodaloptimiza-tionproblems.Bycontrollingtheattractivenessγa,γb,···,γzandtheroamingrandomnessαa,αb,···,αz,ithasbeenshowninpreviousstudiesthatFAcanoutperformbothParticleSwarmOptimization(PSO),see,e.g.,[30]–[33],andrandomsearchapproaches,seee.g.,[24],[26].2)AsymptoticConvergence:Whenγ→0,theconvergenceofFAissimilartothatofPSOwheretheconvergencewasanalyzedbyClercandKennedyin2002in[34].Whenγ→∞,theFAmayactlikearandomsearch,thoughitsbehaviourissimilartothatofSimulatedAnnealing(SA)becausetheFA’ssolutionisperturbedormodifiedinthesimilarwayasthatintheSAinthislimitingcase.TheSAwasshowntobeconvergentundertheright-coolingconditions[35].Thereductionoftheroamingrandomness,i.e.,αa,αb,···,αz,intheFAcanbeconsideredasatypeofcoolingschedule,andthusitcanbeexpectedthatFAcanconvergeinthiscase.Letusnowinvestigatethecasewhen0<γ<∞.GivenaverylargenumberoffireflypopulationN,itcanbeassumedthatNismuchgreaterthanthenumberoflocaloptima.TheinitiallocationsofNfirefliesshouldbeuniformlydistributedoverthewholesearchspace.AstheiterationsofAlgorithm1progress,i.e.,nincreases,theseinitialNfirefliesshouldconvergeintoalllocallybrighterones,i.e.,thelocaloptimaincludingtheglobalones,inastochasticmannerduetothethirdtermin(5),(6),and(7).Bycomparingthebrightestfirefliesamongstthelocallybrighterones,i.e.,thebestsolutionsamongstthelocaloptima,theglobaloptimacanbeattained.Theoretically,thesefireflieswillreachtheglobaloptimalwhenN→∞andn≫1.However,ithasbeenreportedintherelatedliteraturethattheFAconvergeswithlessthan50to100generations[24],[26].InsectionsIV,V,andVI,wepresenthowtheproposedFAcanbeadoptedtosolveoptimizationproblemsfortrans-mitbeamformingdesigns.5Hereafter,“min”and“s.t.”are,respectively,usedtorepresent“minimize”and“subjectto”.III.TransmitBeamformingInthissectionweconsideraclassictransmitbeamformingproblemwithawell-knowniterativemethodbasedonuplink-downlinkduality.WethenintroduceourFAsolutiontotheproblem.A.ProblemFormulation1)ProblemFormulation:ConsideranMt-antennaBSserv-ingUsingle-antennamobileusers.LethHi∈C1×Mt,wi∈CM×1andsi,respectively,bethechannelbetweenthei-thuserandtheBS,theinformation-beamformingvectorandthedatasymbolfortheithuser.Theoverallsignalreceivedbytheithuserisyi=∑Uj=1hHiwjsj+niwhereniisazeromeancircularlysymmetriccomplexGaussiannoisewithvarianceσ2,i.e.,ni∼CN(0,σ2),attheuser.LetRi=hihHirepresenttheinstantaneouschannelstateinformation(CSI)orRi=E(hihHi)denotethestatisticalCSI,{wi}={w1,w2,···,wU}betheset5TheoriginalFAhasbeendiscretizedtosolvevariousdiscreteorcombi-natorialoptimizationproblems[36].Forexample,Osabaetal.[37]usedadiscreteFAtosolverichvehicleroutingproblems.ofcandidateinformation-beamformingvectorsforallusers.AssumingthatE(|si|2)=1,theSINRatthei-thuserisSINRi=wHiRiwi∑Uj=1,j,iwHjRiwj+σ2.(8)Wedesignthesetofbeamformingvectors{wi}suchthattheBS’stotaltransmitpowerisminimizedwhilemaintainingtheSINRlevelateachuserabovetherequiredthreshold.Tothatend,theproblemisformulatedasfollows:minwiU∑i=1wHtwts.t.wHiRiwi∑Uj=1,j,iwHjRiwj+σ2i≥γi,∀i∈{1,···,U},(9)whereγiistherequiredSINRlevelforthei-thuser.Problem(9)isknownasnon-convexduetotheSINRconstraint.2)IterativeApproach:Anelegantapproachtosolve(9)wasintroducedin[1]basedonuplink-downlinkdualitywheretheoptimalsolutionofthedownlinkproblemcanbesoughtviasolvingthefollowingdual-uplinkproblem:6minpiU∑i=1pisubjecttopΓt(p),(10)wherep=[p1p2···pU]T,Γ=diag[γ1,γ2,···,γU],t(p)=[t1(p)t2(p)···tU(p)]T,ti(p)=argminˆwiˆwHiQi(p)ˆwiˆwHiRiˆwi,(11)Qi(p)=(∑Ut=1,t,iptRt+σ2iI),pi=λiσ2iisthedual-uplinkpowerfori-thuser,λiistheithLagrangemultiplierassociatedwiththeithconstraintin(9),andˆwi,i.e.,ˆwHiˆwi=1,isthedual-uplinkbeamformingvectorfori-thuser.Startingfromanypositiveinitialvalueofp(0),thesolutionforthedual-uplinkproblem(10)canbefounditerativelyasp(n+1)=Γt(p(n)).Theiterativedownlinkalgorithmtofindoptimalsolutionsfor(9)issummarisedinalgorithm2.B.ProposedFireflyAlgorithmWerewrite(9)asminWf(W)s.t.di(W)≤0,∀i,(12)whereW=[w1,w2,···,wU]∈CMt×U,f(W)=∑Ui=1wHiwi,di(W)=−wHiRiwi+γi∑Uj=1,j,iwHjRiwj+γiσ2i.Usingthepenaltymethod,werecast(22)intoanunconstrainedproblemas:minWf(W)+P(W),(13)6Thisapproachwasalsoadoptedfortransmitbeamforingproblemsincoordinatedmulti-point(CoMP)transmissions,seee.g.,[38]and[39]. + +IEEETRANSACTIONSONWIRELESSCOMMUNICATIONS,DOI:10.1109/TWC.2023.33287135Algorithm2Iterativealgorithmforproblem(9)1:Input:Γ=diag[γ1,γ2,···,γU],Ri,∀i,numberofiterationsT.2:Initializep(1)0.3:forn=1:Tdo4:fori=1:Udo5:Findˆwi(n)asthedominanteigenvectorofthematrixGi(n)=pi(n)Q−1i(p(n))Ri6:Calculateti(p(n))=ˆwHi(n)Qi(p(n))ˆwi(n)ˆwHi(n)Riˆwi(n).7:endfor8:Updatep(n+1)=Γt(p(n)).9:endfor10:p⋆i=p(n+1)andˆw⋆i=ˆwi(n+1).11:Output:w⋆i=√p⋆iˆw⋆i.whereP(W)isthepenaltytermgivenas:P(W)=U∑i=1λimax{0,di(W)}2,(14)withλi>0isthepenaltyconstant.Let{Wi}={[wi1,wi2,···,wiU]}bethei-thfirefly.WeinitializeapopulationofNfireflies{Wi},i∈{1,2,···,N},anddefinethelightdensityofthefirefly{Wi}as:Ii(Wi)=1f(Wi)+P(Wi).(15)Foranytwofirefliesiandjinthepopulation,ifIj(Wj)>Ii(Wi)thenthefireflyiwillmovetowardthefireflyjas:W(n+1)i=W(n)i+β0e−γ(r(n)ij)2(W(n)j−W(n)i)+α(n)V,(16)wherer(n)ij=||(W(n)j−W(n)i||istheCartesiandistance,β0istheattractivenessatr(n)ij=0,γpresentsthevariationofoftheattractiveness.Thesecondtermof(16)representtheattraction.Thethirdtermof(16)isarandomizationcomprisedofarandomizationfactorα(n)andamatrixofrandomnumbersV∈CMt×U.Therandomfactorα(n)andtheelementsofVaredrawnfromeitheraGaussianoranuniformdistribution.Itcanbeseenthatproblem(12)isaspecialcaseoftheproposedframework(1)wheretheobjectiveandconstraintsarefunctionsofoptimizationvariableW.Hence,theproposedFAhasthesamestepsasthoseinAlgorithm1exceptsteps3,16,18and19giveninAlgorithm3.Algorithm3ModifiedgeneralizedFAforsolving(12)Input:FAparameters:N,T,λi,β0;Optimizationdata:Ri,σ2i,γi;Step3:EvaluatethelightintensitiesofNfirefliesas(15);Step16:Movefireflyitowardsfireflyjas(16);Step18:Attractivenessvarieswithdistanceviae−γ(r(n)ij)2;Step19:Evaluatenewsolutions;updateIi(Wi)as(15);returnW⋆.C.ComplexityAnalysisThecomplexityofalgorithm2isdescribedinthefollowinglemma.Lemma1:Thecomputationalcomplexityofalgorithm2isontheorderofT[U(M3t+M2t+MtlogMt)+U].Proof:Theproofisbasedontheobservationthatcom-plexitiesofsteps5,6and8are,respectively,ontheorderofM3t+MtlogMt,M2tandU.Lemma2:ThecomputationalcomplexityofAlgorithm3isontheorderof:TN2[M2t+NUMt(1+UMt)]+TNlogN+NMtU+NUMt(1+UMt)+NlogN.(17)Proof:Duetospacelimitation,weprovidemainobser-vationstoderive(17)asfollows.ThedominanttermsofthecomputationalcomplexityofAlgorithm3areatsteps2,3,4,16,19,and22.ThecomplexityofgeneratingNmatrices,eachmatrixofsizeMt×U,instep2isontheorderofNMtU.Thecomplexityofevaluatingeachdi(W)isontheorderofUM2t,whilethecomplexityofevaluating∑Ut=1wHtwtisontheorderofUMt.7HencethecomplexityofcalculatingthelightdensityforNfireflies,i.e.,steps3and19,isontheorderofN(UMt+U2M2t)=NUMt(1+UMt).ThecomplexityofrankingNfireflyinsteps4and22isNlogN.Finally,thecomplexityofmovingafireflyinstep16isontheorderofM2t.Assumingaworstcasewhenstep16isexecutedineveryinnerloopofthealgorithm,aftersomemanipulations,onecanarriveat(17).IV.CognitiveBeamformingA.ProblemFormulation1)ProblemFormulation:ConsideracognitivewirelesscommunicationsystemconsistingofanMt-antennacognitivebasestation(BS),Uactivesingle-antennasecondaryusers(SUs)andKsingle-antennaprimaryusers(PUs).Thecog-nitiveBSisallowedtocommunicatewithitsSUsinthesamefrequencybandownedbytheprimarysystemifitsinterferenceimposedoneachPUislessthanapredefinedtolerablethresholdofIto,k.Thereceivedsignalatthet-thSU,t∈{1,···,U},is:yt=hHs,twtst+U∑j=1,j,thHs,twjsj+nt,(18)wherehHs,t∈C1×Mtisthechannelcoefficientofthewirelesslinkbetweenthet-thSUandthecognitiveBS;wt∈CMt×1andst∼CN(0,1)are,respectively,thebeamformingvectorandthedatasymbolassociatedtothet-thSU;andnt∼CN(0,σ2t)isazeromeancircularlysymmetriccomplexGaussiannoisewithvarianceσ2t,atthet-thSU.LetRs,t=E(hs,thHs,t)forthestatisticalCSIandRs,t=hs,thHs,tfortheinstantaneousCSI.TheSINRatthet-thSUcanbeexpressedas:SINRt=wHtRs,twt∑Uj=1,j,twHjRs,twj+σ2t.(19)7Here,weadopttheschoolbookiterativealgorithmtoevaluatecomplexityofthemultiplicationoftwomatricesofsizesn×mandm×pastheorderofnmp. + +IEEETRANSACTIONSONWIRELESSCOMMUNICATIONS,DOI:10.1109/TWC.2023.33287136LethHp,k∈C1×Mtbethechannelcoefficientofthewirelesslinkbetweenthek-thPU,k∈{1,···,K},andthecognitiveBS,Rp,k=E(hp,khHp,k)forthestatisticalCSIandRp,k=hp,khHp,kfortheinstantaneousCSI.Thetotalinterferencepowerimposedonthek-thPUbythecognitiveBSis∑Uj=1wHjRp,kwj.OurobjectiveistodesigndownlinkbeamformingvectorsfortheSUsthatminimizethecognitiveBStransmitpowerwhilemaintainingtherequiredSINRlevelforeverySUandkeepingtheinterferencelevelimposedateachPUreceiverbelowthepredefinedtolerablethreshold.Theoptimizationproblemtodesignbeamformingvectorsiscastas:minwtU∑t=1wHtwts.t.wHtRs,twt∑Uj=1,j,twHjRs,twj+σ2t≥ηt,∀t∈{1,···,U},U∑j=1wHjRp,kwj≤Ito,k,∀k∈{1,···,K},(20)whereηtistherequiredSINRlevelforthet-thSU.DuetotheSINRconstraint,problem(20)isnon-convex.2)SDPApproach:Forthesakeofcompleteness,weprovideareviewonatraditionalapproachtosolve(20)usingsemidefiniteprogramming(SDP).WefirstformanewoptimizationvariableFt=wtwHtwhereFt0,Ft∈HMt×Mt,andFtisarank-onematrix.8WethenutilizetheidentityxHXx=Tr(XxxH)torewrite(20)as:minFt∈HM×MU∑t=1Tr(Ft)s.t.(1+1ηt)Tr(Rs,tFt)−U∑j=1Tr(Rs,tFj)−σ2t≥0,∀t,Ito,k−U∑j=1Tr(Rp,kFj)≥0,∀k,Ft0,∀t,(21)wheret∈{1,···,U},k∈{1,···,K}.Problem(21)isinastandardSDPform.Hence,itsoptimalsolutioncanbeobtainedinapolynomialtimebyusingageneralpurposeIPM,e.g.,CVXwhichisaMatlabbasedmodelingsystemforconstructingandsolvingdisciplinedconvexprograms[40].Inarrivingat(21),wehaverelaxedtherank-oneconstraintonFt,∀t.Ifthesolutionof(21)doesnothaverank-one,thenfurthercomputationresourcesarerequiredtoderiveasub-optimalsolutionviasomerank-oneapproximationsortheGaussianrandomizeprocedure[19].B.ProposedFireflyAlgorithmHere,weadoptthegeneralizedFAinAlgorithm1tosolve(20).Rearrangingtheconstraint,werewrite(20)as:8Amatrixisrank-oneifandonlyifithasonlyonelinearlyindependentcolumn/row.minWf(W)s.t.φt(W)≤0,∀t∈{1,···,U},ϕk(W)≤0,∀k∈{1,···,K},(22)whereW=[w1,w2,···,wU]∈CMt×U,f(W)=∑Ut=1wHtwt,φt(W)=ηt∑Uj=1,j,iwHjRs,twj+ηtσ2t−wHtRs,twtandϕk(W)=∑Uj=1wHjRp,kwj−Ito,k.Usingthepenaltymethod,wefirsttransform(22)intoanunconstrainedproblemas:minWf(W)+P(W),(23)whereP(W)isthepenaltytermgivenas:P(W)=U∑t=1λtmax{0,φt(W)}2+K∑k=1ρkmax{0,ϕk(W)}2,(24)withλt>0andρk>0arepenaltyconstants.LetWi=[wi1,wi2,···,wiU]∈CMt×Ubethefireflyi.WeinitializeapopulationofNfirefliesWi,i∈{1,2,···,N},anddefinethelightdensityofthefireflyWias:Ii(Wi)=1f(Wi)+P(Wi).(25)Foranytwofirefliesiandjinthepopulation,ifIj(Wj)>Ii(Wi)thenthefireflyiwillmovetowardthefireflyjas:W(n+1)i=W(n)i+β0e−γ(r(n)ij)2(W(n)j−W(n)i)+α(n)V,(26)wherer(n)ij=||(W(n)j−W(n)i||istheCartesiandistance,β0istheattractivenessatr(n)ij=0,γpresentsthevariationofoftheattractiveness.Thesecondtermof(26)capturestheattraction.Thethirdtermof(26)isarandomizationcomprisedofarandomizationfactorα(n)andamatrixofrandomnumbersV∈CMt×U.Therandomfactorα(n)andtheelementsofVaredrawnfromeitheraGaussianoranuniformdistribution.Itcanbeseenthatproblem(22)isaspecialcaseoftheproposedframework(1)wheretheobjectiveandconstraintsarefunctionsofonlyoneoptimizationvariableW.Hence,theproposedFAhasthesamestepsasthoseinAlgorithm1exceptsteps3,16,18and19giveninAlgorithm4.Algorithm4ModifiedgeneralizedFAforsolving(20)Input:FAparameters:N,T,λt,ρk,β0,γ;Optimizationdata:Rs,t,Rp,k,σ2t,ηt,Ito,k;Step3:EvaluatethelightintensitiesofNfirefliesas(25);Step16:Movefireflyitowardsfireflyjas(26);Step18:Attractivenessvarieswithdistanceviae−γ(r(n)ij)2;Step19:Evaluatenewsolutions;updateIi(Wi)as(25);returnW⋆.C.ComplexityAnalysisWeinvestigatethecomplexityofsolving(21)inaworst-caseruntimeoftheIPMfollowedbythecomplexityanalysisoftheproposedFA.Westartbythefollowingdefinition. + +IEEETRANSACTIONSONWIRELESSCOMMUNICATIONS,DOI:10.1109/TWC.2023.33287137Definition1:Atagivenε>0,thesetof{Fεt}isanε-solutiontoproblem(21),i.e.,anacceptablesolutionwiththeaccuracyofε,ifU∑t=1Tr(Fεt)≤minFt∈HM×MU∑t=1Tr(Ft)+ε.(27)Thenumberofdecisionvariablesof(21)isM2t.Thecom-plexityof(21)isdescribedinthefollowinglemma.Lemma3:Thecomputationalcomplexitytoattainε-solutionto(21)isontheorderof:ln(ε−1)√U(Mt+1)+K[(M2t+1)(U+K)+UM2t(M2t+Mt)+M4t]M2t.(28)Proof:Wesketchsomemainstepstoarriveatthelemmaduetospacelimitation.Itcanbeobservedthat(21)has(U+K)linear-matrix-inequality(LMI)constraintsofsize1andULMIconstraintsofsizeMt.Onecanfollowthesamestepsasin[41,SectionV-A]toderivethefollowingfacts:(i)theitera-tioncomplexityisontheorderofln(ε−1)√U(Mt+1)+K,and(ii)theper-iterationcomplexityisontheorderof[(M2t+1)(U+K)+UM2t(M2t+Mt)+M4t]M2t.Lemma4:ThecomputationalcomplexityofAlgorithm4isontheorderof:TN2[M2t+NUMt(1+UMt+KMt)]+TNlogN+NMtU+NUMt(1+UMt+KMt)+NlogN.(29)Proof:Duetospacelimitation,weprovidemainobser-vationstoderive(29)asfollows.ThedominanttermsofthecomputationalcomplexityofAlgorithm4areatsteps2,3,4,16,19,and22.ThecomplexityofgeneratingNmatrices,eachmatrixofsizeMt×U,instep2isontheorderofNMtU.Thecomplexityofevaluatingeachφt(W)orϕk(W)isontheorderofUM2t,whilethecomplexityofevaluating∑Ut=1wHtwtisontheorderofUMt.HencethecomplexityofcalculatingthelightdensityforNfireflies,i.e.,steps3and19,isontheorderofN(UMt+U2M2t+KUM2t)=NUMt(1+UMt+KMt).ThecomplexityofrankingNfireflyinsteps4and22isNlogN.Finally,thecomplexityofmovingafireflyinstep16isontheorderofM2t.Assumingaworstcasewhenstep16isexecutedineveryinnerloopofthealgorithm,aftersomemanipulations,onecanarriveat(29).V.ReconfigurableIntelligentSurface-AidedBeamformingA.ProblemFormulation1)ProblemFormulation:Consideracommunicationsys-temcomprisingofanMt-antennaBScommunicatingwithUsingle-antennamobileusersinwhichthedirectcommunica-tionlinksbetweentheBSanditsmobileusersareblocked,e.g.,becauseofhighbuildingetc.,[42].Tocircumventtheproblem,anNt-reflective-elementRISisutilizedtosupportthecommunication.LetH=[h1,...,hNt]∈CMt×NtrepresentthechannelcoefficientsbetweentheBSandtheRISandgi=[gi1,...,giNt]T∈CNt×1bethechannelcoefficientsbetweentheRISandthei-thuser.Letxi,i.e.,E[|xi|2]=1,andwi∈CMt×1,respectively,representthedatasymbolandtheactivebeamformingvectorforthei-thuser.EachreflectiveelementoftheRISgeneratesaphaseshifttosupportthecommunicationbetweentheBSandthemobileusers.Letθkbethephaseshiftatthek-threflectiveelementandletθθθ=[θ1,θ2,···,θNt]Tdenotethephase-shiftcoefficientsgeneratedbytheRISwith|θk|≤1andarg(θk)∈[−π,π),∀k=1,...,Nt.VectorθθθisthepassivebeamformingvectorfortheRIS.Thesignalarrivedatthei-thuseris:yi=gHidiag(θθθ)HHHwixi+gHidiag(θθθ)HHHU∑j=1,j,iwjxj+ni,=θθθHGHiwixi+θθθHGHiU∑j=1,j,iwjxj+ni,(30)whereGHi=diag(g∗i)HH∈CNt×Mtandni∼CN(0,σ2)representstheadditivenoisemeasuredatthei-thuser.Fur-thermore,let{wi}={w1,w2,···,wU}denotethesetofactivebeamformingvectors,andSINRi({wi},θθθ)betheSINRatthei-thuser.Onecanwrite:SINRi({wi},θθθ)=|θθθHGHiwi|2U∑j=1,j,i|θθθHGHiwj|2+σ2i.(31)Theoptimizationisposedasfollows:min{wi},θθθU∑i=1wHiwis.t.SINRi({wi},θθθ)≥ηi,∀i,|θk|≤1,∀k,(32)whereηiistherequiredSINRlevelmeasuredatthei-thuser.SincetheSINRconstraintisafunctionoftwooptimizationvariableswiandθθθ,problem(32)isnon-convex.2)AlternativeOptimizationApproach:Forthesakeofcompleteness,thewidely-adoptedAOapproach[21],[42]–[44]isrepresentedhereasabaselinetosolve(32).LetFi=wiwHi,andΘ=θθθθθθH,i.e.,rank(Fi)=1andrank(Θ)=1.AsFiandΘaretwoindependentvariables,theycanbealternativelysolved[21],[42]–[44].Tothatend,relaxingtherank-oneconstraintonFiandbeginningwithanyinitialvalueofthereflectingcoefficientmatrixΘ(0),thefollowingsub-problemwillbesolvedatthep-thiteration:min{Fi}TrU∑i=1Fis.t.TrGiΘ(p−1)GHiFiηiσ2i−U∑j=1,j,iTrGiΘ(p−1)GHiFjσ2i−1≥0,∀i,Fi0,∀i∈{1,···,U}.(33)ThereflectingcoefficientsΘ(p)isthenupdatedfromtheoptimalsolutionof(33)atp-thiteration,i.e.,{F(p)i},bysolving + +IEEETRANSACTIONSONWIRELESSCOMMUNICATIONS,DOI:10.1109/TWC.2023.33287138thefollowingsub-problem[42]:minΘTr(Θ)s.t.TrΘGHiF(p)iGiηiσ2i−U∑j=1,j,iTrΘGHiF(p)jGiσ2i−1≥0,∀i,diag(diag(Θ))INt,Θ0.(34)TheAOapproachrepetitivelysolvestwoSDPs(33)and(34)inn0iterationstoobtainthesolutionfor(32).Remark1:ItisworthnoticingthattheAOapproachap-proximatestheoriginallynon-convexoptimization(32)bytwosub-problems(33)and(34).Although(33)and(34)areconvex,thesolutionstothesesub-problemscanberegardedastheupperboundsoftheoriginalproblem(32)asthesesolutionsmaynotbetheglobalsolution.Furthermore,theAOapproachadoptstheso-calledsemidefiniterelaxationtechnique[20]inwhichtherank-oneconstraintsonFiandΘarerelaxed.Ifsolving(33)and/or(34)doesnotreturnrank-onematricesFiand/orΘ,thenarank-oneapproximationoraGaussianrandomizeprocedure[19]isrequiredtoextractapproximatedrank-onesolutions.Extractingtheapproximatedsolutionsrequiresfurthercomputationalresourcesyetonlyresultsinsub-optimalsolutions.Motivatedbytheaboveobservations,weintroduceanovelFAapproachtosimultaneouslysolvewiandθθθfortheoriginalproblem(32)inthefollowingsection.B.ProposedFireflyAlgorithmTheoptimization(32)canbeexpressedasmin{W,θθθ}f(W)s.t.φi({W,θθθ})≤0,∀i,ϕk(θk)≤0,∀k,(35)whereW=[w1,w2,···,wU]∈CMt×U,f(W)=∑Ui=1wHiwi,φi(W,θθθ)=ηi∑Uj=1wHjGiθθθθθθHGHiwjσ2i+ηi−(1+ηi)wHiGiθθθθθθHGHiwiσ2i,(36)andϕk(θk)=|θk|−1.Adoptingthepenaltymethod,(35)canbewrittenas:min{W,θθθ}f(W)+P(W,θθθ),(37)whereP(W,θθθ)isthepenaltytermgivenas:P(W,θθθ)=U∑i=1λimax{0,φi({W,θθθ})}2+Nt∑k=1ρkmax{0,ϕk(θk)}2,(38)withλi>0andρk>0arepenaltyconstants.Let{Wt,θθθt}={[wt1,wt2,···,wtU],θθθt}bethefireflyt.WeinitializeapopulationofNfireflies{Wt,θθθt},t∈{1,2,···,N}anddefinethelightdensity,i.e.,thebrightness,ofthefireflyt{Wt,θθθt}as:It(Wt,θθθt)=1f(Wt)+P(Wt,θθθt).(39)Foranyfirefliestandlamongstthepopulation,ifIt(Wt,θθθt)>Il(Wl,θθθl)thenthefireflylwillmovetowardthefireflytas:W(n+1)l=W(n)l+β0e−γ(r(n)w,tl)2(W(n)t−W(n)l)+α(n)V,(40)θθθ(n+1)l=θθθ(n)l+β0e−γ(r(n)θ,tl)2(θθθ(n)t−θθθ(n)l)+α(n)v,(41)wherer(n)w,tl=||(W(n)t−W(n)l||andr(n)θ,tl=||(θθθ(n)t−θθθ(n)l||aretheCartesiandistances,β0istheattractivenessatr(n)w,tl=0andr(n)θ,tl=0,γpresentsthevariationofoftheattractiveness.Thesecondtermsof(40)and(41)capturetheattractionswhilethethirdtermsof(40)and(41)arerandomizationcomprisedofrandomizationfactorα(n),V∈CMt×Uandv∈CMt×1.Thefactorα(n),theelementsofVandvaredrawnfromeitheranuniformoraGaussiandistribution.Itcanbeobservedthatproblem(35)isaspecialcaseoftheproposedframework(1)wheretheobjectiveandconstraintsarefunctionsofoptimizationvariablesWandθθθ.TheproposedFAforRIShasthesamestepsasthoseinAlgorithm1exceptsteps3,16,18and19giveninAlgorithm5.Algorithm5ModifiedgeneralizedFAforsolving(32)Input:FAparameters:N,T,λi,ρn,β0;γ;Optimizationdata:H,gi,σ2i,ηi,Ito;Step3:EvaluatethelightintensitiesofNfirefliesas(39);Step16:Movefireflyitowardsfireflyjas(40)and(41);Step18:Attractivenessvarieswithdistancesviae−γ(r(n)w,ji)2ande−γ(r(n)θ,ji)2;Step19:Evaluatenewsolutions;updateIi(Wi,θθθi)as(39);returnW⋆,θθθ⋆.C.ComplexityAnalysisHere,weanalyzethecomputationalcomplexitiesoftheAOandtheproposedFAforRIS-aidedbeamformingproblem.Lemma5:ThecomplexityoftheAOapproachisontheorderof:no(τ1+τ2),(42)whereτ1=ln(ε−1)√U(Mt+1)[(M2t+1)U+UM2t(M2t+Mt)+M4t]M2t,(43)τ2=ln(ε−1)√U+2Nt[(N2t+1)(U+2N2t)+N4t]N2t.(44)Proof:Wefirstgivesomehintstoderivethecomputa-tionalcomplexityofobtainingoptimalsolutiontoproblems(33)and(34).Withtheobservationthat(33)hasULMIconstraintsofsize1andULMIconstraintsofsizeMt,onecanfollowthesamestepsasin[41,SectionV-A]toderivethecomplexityofsolving(33)asτ1givenin(43).Atagivenε>0,Θεiscalledanε-solutiontoproblem(34)ifTr(Θε)≤minΘTr(Θ)+ε.Thenumberofdecisionvariablesof + +IEEETRANSACTIONSONWIRELESSCOMMUNICATIONS,DOI:10.1109/TWC.2023.33287139(34)isN2t.Observingthat(34)hasUlinear-matrix-inequality(LMI)constraintsofsize1and2LMIconstraintsofsizeNt,onecanderivethecomputationalcomplexitytoattainε-solutionto(34)astheorderofτ2givenin(44).SincetheAOapproachiterativelysolves(33)and(34)innoiterations,thecomplexityofAOapproachisontheorderofno(τ1+τ2).Lemma6:ThecomputationalcomplexityofAlgorithm5isontheorderofTN2[M2t+Nt+N(UMt+U(N2t+MtNt)+Nt)]+TNlogN+NMtU+NtN+NlogN+N(UMt+U(N2t+MtNt)+Nt).(45)Proof:Theproofisbasedonthefollowingobserva-tions.ThedominanttermsofthecomputationalcomplexityofAlgorithm5areatsteps2,3,4,16,19,and22.ThecomplexityofgeneratingNfirefliesinstep2isontheorderofNMtU+NtN.Thecomplexitiesofevaluatingφi(W,θθθ),ϕk(θk),and∑Ui=1wHiwiare,respectively,ontheorderofU(N2t+MtNt),Nt,andUMt.Hence,thecomplexityofcalculatingthelightdensityforNfireflies,i.e.,steps3and19,isontheorderofN(UMt+U(N2t+MtNt)+Nt).ThecomplexityofrankingNfireflyinsteps4and22isNlogN.Finally,thecomplexityofmovingafireflyinstep16isontheorderofM2t+Nt.Assumingaworstcasewhenstep16isexecutedineveryinnerloopofthealgorithm,aftersomemanipulations,onecanarriveat(45).VI.RIS-AidedWirelessPowerTransferA.ProblemFormulation1)ProblemFormulation:Considerasimilarcommunica-tionsysteminV-A,however,theusersareenergyharvestingreceivers(EHRs)insteadofinformationdecodingreceivers.UsingthesamenotationsasinV-A,thepowerarrivedatthei-thuseris:Ei=∣∣∣∣gHidiag(θθθ)HHHU∑j=1wj∣∣∣∣2=U∑j=1wHjGiθθθθθθHGHiwj,(46)wherewjistheactiveenergybeamformingvectorforthej-thuser.weinterestedinmaximizingatotalweightedsumpowerreceivedattheEHRsobtainedviathefollowingoptimizationproblem:max{wi},θθθU∑i=1U∑j=1αiwHjGiθθθθθθHGHiwjs.t.U∑j=1wHjwj≤P,|θk|=1,∀k,(47)wherePisthemaximumtransmitpoweroftheBSandαi≥0istheweightingfactorforthei-thEHR.2)SuccessiveConvexApproximation:Accordingto[23],foranyfixθθθ,onlyonecommonenergybeamissufficient.Usingasuccessiveconvexapproximation(SCA)technique,[23]proposedaniterativealgorithmtofindoptimalactiveandpassivebeamformingvectorsforproblem(47)asfollows.Startingwithaninitializedvalueθθθ(0),theoptimalactivebeamformingvectoratthel-thiterationsiscalculatedasw(l)=√Peigmax(∑Ui=1αiGiθθθ(l−1)θθθ(l−1)HGHi)whereeigmax(X)isthemaximumeigenvalueofmatrixX.Thek-thcoefficientoftheRIS’sphaseshiftvectoratthel-thiterationsiscalculatedas[θθθ(l)]k=1ifµk=0and[θθθ(l)]k=µk|µk|ifµk,0,whereµk=[∑Ui=1αiGHiw(l)w(l)HGiθθθ(l−1)]k.B.ProposedFireflyAlgorithmTheoptimization(47)canbeexpressedasmin{W,θθθ}−f(W,θθθ)s.t.φ({W,θθθ})≤0,ϕk(θk)=0,∀k,(48)whereW=[w1,w2,···,wU]∈CMt×U,f(W,θθθ)=∑Ui=1∑Uj=1αiwHjGiθθθθθθHGHiwj,φ(W,θθθ)=∑Uj=1wHjwj−P,andϕk(θk)=|θk|−1.Adoptingthepenaltymethod,(35)canbewrittenas:min{W,θθθ}−f(W,θθθ)+P(W,θθθ)(49)whereP(W,θθθ)=λmax{0,φ({W,θθθ})}2+∑Ntk=1ρk{ϕk(θk)}2,withλ>0andρk>0arepenaltyconstants.Let{Wt,θθθt}={[wt1,wt2,···,wtU],θθθt}bethefireflyt.WeinitializeapopulationofNfireflies{Wt,θθθt},t∈{1,2,···,N}anddefinethelightdensity,i.e.,thebrightness,ofthefireflyt{Wt,θθθt}as:It(Wt,θθθt)=1−f(Wt)+P(Wt,θθθt).(50)Itcanbeobservedthatproblem(48)isaspecialcaseoftheproposedframework(1)wheretheobjectiveandconstraintsarefunctionsofoptimizationvariablesWandθθθ.Utilizingthefireflymovementsdefinein(40)and(41)inSectionV-B,theproposedFAforRIShasthesamestepsasthoseinAlgo-rithm1exceptsteps3,16,18and19giveninAlgorithm6.Algorithm6ModifiedgeneralizedFAforsolving(47)Input:FAparameters:N,T,λ,ρk,β0;γ;Optimizationdata:H,gi,αi,P;Step3:EvaluatethelightintensitiesofNfirefliesas(50);Step16:Movefireflyitowardsfireflyjas(40)and(41);Step18:Attractivenessvarieswithdistancesviae−γ(r(n)w,ji)2ande−γ(r(n)θ,ji)2;Step19:Evaluatenewsolutions;updateIi(Wi,θθθi)as(50);returnW⋆,θθθ⋆.C.ComplexityAnalysisHere,weanalyzethecomplexitiesoftheSCAapproachandtheproposedFAfortheRIS-aidedWPTbeamforming.Westartbyintroducingthefollowinglemma.Lemma7:ThecomplexityoftheSCAapproachisontheorderof:m0(UMt(Mt+Nt)+M3t+MtlogMt+N3t+N2tMt),(51)wherem0isthenumberofiterationsoftheSCAapproach. + +IEEETRANSACTIONSONWIRELESSCOMMUNICATIONS,DOI:10.1109/TWC.2023.332871310Proof:Ateachiteration,thecomplexityofevaluatingαiGiθθθ(l−1)θθθ(l−1)HGHiisontheorderofU(M2t+MtNt).ThecomplexitiesoffindingamaximumeigenvalueoftheMt×MtmatrixαiGiθθθ(l−1)θθθ(l−1)HGHibasedontheSVDmethodisontheorderofM3t+MtlogMt.Hence,thecomplexityoffindingw(l)isontheorderofUMt(Mt+Nt)+M3t+MtlogMt.Furthermore,thecomplexityofcalculatingµkisontheorderofN2t+MtNt.Therefore,thecomplexityoffindingθθθ(l)isontheorderofNt(N2t+MtNt).Consequently,m0iterationsofevaluatingw(l)andθθθ(l)leadto(51).Lemma8:ThecomplexityoftheAlgorithm6isontheorderof:TN2[M2t+Nt+N(UMt+U(N2t+MtNt)+Nt)]+TNlogN+NMtU+NtN+NlogN+N(UMt+U(N2t+MtNt)+Nt).(52)Proof:Noticingthatthecomplexitiesofevaluatingφ(W,θθθ),ϕk(θk),andf(W,θθθ)are,respectively,ontheorderofUMt,Nt,andU(NtMt+N2t).OnecaneasilyshowthatthecomplexityoftheAlgorithm6isthesameasthatoftheAlgorithm5.VII.NumericalResultsInthissection,weperformsimulationstoevaluatetheper-formancesoftheproposedFAapproaches,i.e.,FAapproachesfortransmitbeamforming,cognitivecognitivebeamforming,RIS-aidedtransmitbeamforming,andRIS-aidedWPT,andcomparethemwiththeiriterative,SDP,andSCAcounterparts.CVXpackage[40]isutilizedtoobtainthesolutionforthecognitiveSPDapproach,i.e.,problem(21),andtheAOapproachfortheRIS-aidedtransmitbeamforming.IntheAOapproach,twoSDPs(33)and(34)arealternativelysolvedinn0=10iterations.ThesetupparametersforFAsareasfollows.Thevariationoftheattractivenessγissetat1.Thepenaltyconstantsaresetequalbuttheydynamicallyvaryasλi=ρk=n2,∀i,kwherenisthegenerationindexinAlgorithm1.Theattractivenessatzerodistanceisβ0=1.Finally,theinitialrandomizationfactorisα(0)=0.9anditsvalueatthen-thgenerationisα(n)=α(0)0.9n.A.EvaluationonTransmitBeamformingWesimulateascenariooftwousers,i.e.,U=2,randomlydistributedwithin2kmfromtheirBS.ThearrayantennagainattheBSis15dBi.Thenoisepowerspectraldensity,noisefigureateachuserandthesubcarrierbandwidthare,respectively,−174dBm/Hz,5dBand15kHzwide.Thepathlossmodelis35+34.5log10(l),wherelisinkilometers.Alog-normalshadowingwithastandarddeviationof8dBisassumed.Furthermore,acomplexGaussiandistributionissetwiththevarianceof1/2oneachofitsrealandimaginarycomponentsforthedownlinkchannelfadingcoefficients.MonteCarlosimulationshavebeencarriedoutover1000channelrealizations.Fig.1illustratesthetotaltransmitpoweroftheproposedFAapproachanditsiterativecounterpartversustherequiredSINRlevelwithdifferentnumbersofBS’santennas.The01020SINR [dB]5101520253035Tranmsit power [dBm](a)FAIterative01020SINR [dB]5101520253035Tranmsit power [dBm](b)FAIterative01020SINR [dB]5101520253035Tranmsit power [dBm](c)FAIterativeFig.1:ThetotalBS’stransmitpowerversustherequiredSINRlevelwithdifferentnumbersofBS’santennas:(a)4antennas;(b)6antennas;(c)8antennas.ThefireflypopulationisN=30.ThenumberofmaximumgenerationsT=30.resultsonFig.1clearlyshowthattheproposedFAapproachoutperformstheiterativemethodinobtaininglowerrequiredtransmitpower,i.e.,around3to4dBlower,forallsimulatedsetups.TheresultsinFig.1confirmtheabilityoftheproposedFAinhandlinghighlynonlinearandmultimodaloptimizationproblems.Thispowersavinggain,however,comesatthepriceofahighercomplexity.UsingtheparametersetupforFig.1inLemmas1and2,i.e.,U=2,T=N=30,Mt=4,6,8,onecanfindthecomplexitiesoftheIterativeandFAapproachesare,respectively,intheorderofO(104)andO(108).Whenthenumberofantennaselementsarelarge,lettingT=N=Mt,itcanbeshownthatthedominanttermsofthecomplexitiesoftheIterativeandFAapproachareintheorderofO(M4t)andO(M6t),respectively.ThetradeoffbetweenthepowersavinggainandcomputationalcomplexityoftheproposedFAapproachincomparisonwiththeIterativemethodshouldbeconsideredbythenetworkdesigner/operator.Fig.2showsthetotalBS’stransmitpoweroftheIterativeandproposedFAversusthenumberofiteration/generationswithdifferentnumbersofBS’santennas.Theresultsindi-catethattheIterativeapproachconvergesafterjust5iter-ations/generationswhiletheproposedFArequiresabout20generations/iterationstoleveloff.Fig.3showsthetotalBS’stransmitpoweroftheproposedFAapproachversusthenumberofpopulationNwithdifferentBS’santennaelements.ItcanbeseenthattheobservedcurvesconvergeafterN=30.OursimulationsindicatethattheproposedFAapproachperformswellwithatleast30firefliestosolve(12)undertheinvestigatedSINRrange.B.EvaluationsonCognitiveTransmitBeamformingWefirstreproducetheresultoftheexperimentdescribedinExample1of[3]tocomparetheproposedFAapproachwiththeSDPapproach.Inthatexperiment,threeSUsarelocatedat−5◦,10◦,25◦,andtwoPUsarelocatedat30◦and50◦, + +IEEETRANSACTIONSONWIRELESSCOMMUNICATIONS,DOI:10.1109/TWC.2023.3328713110102030Generations/Iterations0510152025303540Tranmsit power [dBm](a)FAIterative0102030Generations/Iterations0510152025303540Tranmsit power [dBm](b)FAIterative0102030Generations/Iterations0510152025303540Tranmsit power [dBm](c)FAIterativeFig.2:ThetotalBS’stransmitpowerversusthegenerations/iterationwithdifferentnumbersofBS’santennas:(a)4antennas;(b)6antennas;(c)8antennas.ThefireflypopulationisN=30.TherequiredSINRlevelateachuseris10dB.10203040506070Number of population [N]19.519.5519.6(a)10203040506070Number of population [N]17.9217.9417.96Tranmsit power [dBm](b)10203040506070Number of population [N]16.8516.9(c)Fig.3:ThetotalBS’stransmitpowerversusthenumberofpopulationwithdifferentnumbersofBS’santennas:(a)4antennas;(b)6antennas;(c)8antennas.ThenumberofgenerationisT=30.TherequiredSINRlevelateachuseris10dB.relativetotheBS’sarraybroadside.ThetolerableinterferenceleveltwoPUsareIto,1=0.001andIto,2=0.0001.Thenoisevarianceissetto0.1whiletherequiredSINRvaluesaresetto1fortheSUs.ThechannelcovariancematricesfromthesecondaryBStoSUt,i.e.,Rs,t=R(ζs,t,δa),andtoPUk,i.e.,Rp,k=R(ζp,k,δa),arethefunctionoftheangleofdeparture,i.e.,ζs,torζp,k,andthestandarddeviationoftheangularspread,i.e.,δa.The(m,n)thentryofR(ζ,δa)is,[20]:ej2π∆ψ[(n−m)sinζ]e−2[π∆δaψ{(n−m)cosζ}]2,(53)whereψisthecarrierwavelength,σa=2◦,andtheantennaspacingattheBSissetas∆=ψ/2.Fig.4(a)illustratestheradiationpatternsattheBSoftheSDPapproachasdescribedin(21),whichisthereproduction-90-80-70-60-50-40-30-20-100102030405060708090Azimuth in degrees-40-30-20-10-5dB(b): FA Approach-90-80-70-60-50-40-30-20-100102030405060708090Azimuth in degrees-40-30-20-10-5dB(a): SDP ApproachFig.4:TheradiationpatternoftheBSwith8antennas:(a)Thereproductionof[3,Fig.3];(b)TheproposedFAapproachwiththenumberofpopulationN=100.ofFig.3in[3],whileFig.4(b)showstheradiationpatternsattheBSoftheFAapproachproposedinAlgorithm4.TheresultsclearlyindicatethattheFAobtainsthesameradiationpatternastheSDPapproachdoes.Bothapproachesareabletoformnullstothelocations/angleswherethePUsarelocated.Inotherwords,theproposedFAcanobtainthesameoptimalsolutionastheIPMdoesfortheSDPcounterpart.ThisconfirmstheabilityoftheproposedFAinhandlinghighlynonlinearandmultimodaloptimizationproblems.WiththesetupinFig.4,i.e.,Mt=8,U=3,K=2,N=100and,T=80,onecaneasilyverifyfromLemmas3and4thattheproposedFAapproachrequireshighercomputationalcomplexitythantheSDPapproachdoeswhenitreturnsrank-oneoptimalsolution.Whenthenumberofantennasislarge,onecanshowthatthedominanttermof(28)isM612t.Ontheotherhand,assumingT=N=Mt,thedominanttermof(29)isM6t.Hence,thecomplexityofanIPMtosolve(21)isslightlyhigherthanthecomplexityoftheproposedFAinAlgorithm4,i.e.,O(M612t)incomparisonwithO(M6t).Fig.5showsthetransmitpoweroftheproposedFAap-proachversusthenumberofpopulationwithdifferentnumbersoftransmitantennas.TheresultsindicatethattheproposedFAconvergeswithallnumberofantennasetupsasalltheobservedcurvesleveloffafterthemaximumsizeofpopulationofN=50.However,thehigheroftheantennaelementsis,thelargerthesizeofthepopulationisrequiredforaconvergedtransmitpower.Forexample,withM=8,16,and32,theproposedFAapproach,respectively,obtainsastabletransmitpoweratN=30,40and50.Thisisduetothefactthatthesizeofthesystemincreaseswithahighernumberofantennaelements,i.e.,ahigherdegreeoffreedom.Asaresult,itrequiresalargersizeofthepopulationtoprovideasufficientdiversificationfortheexplorationoftheFA.Theresultsalsoshowthattherequiredtransmitpowerdecreaseswhenthenumberofantennasincreaseastheresultofhavinghigherdegreeoffreedom. + +IEEETRANSACTIONSONWIRELESSCOMMUNICATIONS,DOI:10.1109/TWC.2023.332871312102030405060708090100Number of population N-20-15-10-50510Transmit power [dB]M=8M=16M=32Fig.5:ThetotaltransmitpoweroftheproposedFAapproachversusthenumberofpopulationwithdifferentnumbersoftransmitantennas.ThenumberofmaximumgenerationT=150.020406080100120140160180200220240260280300Number of generations-20-1001020304050Transmit power [dB]M=8M=16M=32Fig.6:ThetotaltransmitpoweroftheproposedFAapproachversusthenumberofmaximumgenerationswithdifferentnumbersoftransmitantennas.ThenumberofpopulationN=70.Fig.6depictsthetransmitpoweroftheproposedFAapproachversusthenumberofmaximumgenerationswithdifferentnumbersoftransmitantennas.AsimilartrendasinFig.5isalsoobservedinthisfigure.ThetransmitpowerattainedbytheproposedFAapproachconvergeswithallnumbersofantennasetups.Thehighernumberofantennasis,thehighernumberofgenerationsisneededasaresultofhigherexploitationrequiredfortheincreaseoftheproblemdimension.Forinstance,thetransmitpowerlevelsoffataround90,100,and120generations,respectively,forM=8,16,and32.C.EvaluationsonRIS-aidedTransmitBeamformingWesimulateaRIS-aidedcommunicationsystemwhichconsistsofoneBS,oneRIS,andtwousers,i.e.,U=2.ThedistancebetweentheBSandtheRISis10m.Users048121620Required SINR level [dB]010203040Transmit power [dBm]Mt=3, Nt=30FAAO048121620Required SINR level [dB]0102030Transmit power [dBm]Mt=8, Nt=30FAAO048121620Required SINR level [dB]010203040Transmit power [dBm]Mt=3, Nt=20FAAO048121620Required SINR level [dB]0102030Transmit power [dBm]Mt=8, Nt=20FAAOFig.7:ThetotalBS’stransmitpowerversustherequiredSINRlevelwithdifferentnumbersofBS’santennasandRIS’sreflectiveelements.ThefireflypopulationisN=120.ThenumberofmaximumgenerationsT=50.arerandomlydistributedwithadistanceof6mfromtheRIS.ThepathlossexponentsofbothwirelesslinksfromtheBStotheRISandfromtheRIStousersaresettobe2.2withthesignalattenuationatthereferencedistanceof1mbeing30dB[23],i.e.,thelarge-scalefadingcoefficientismodeledas−30−22log10(d)dBwheredisthedistancebetweentheBStoRISorRIStoauser.Thenoisevarianceateachuseris−124dBm.MonteCarlosimulationsarecarriedover100channelrealizations.Eachchannelrealizationisassociatedwitharandomuserlocationandarandomfadingcoefficient.Fig.7illustratesthetotalBS’stransmitpowerversustherequiredSINRlevelwithdifferentnumbersofBS’santennasandRIS’sreflectiveelements.TheresultsindicatethattheproposedFAprevailstheAOapproachintermsoflowerpowerconsumption.ThesuperiorperformanceoftheFAapproachoveritsAOcounterpartcanbeexplainedasfollows.AstheAOapproachapproximatesnon-convexproblem(32)bytwoconvexsub-problems(33)and(34),thesolutionobtainedbytheAOapproachisnotnecessarytheglobaloptimalsolutionoftheoriginalproblem(32).Ontheotherhand,theproposedFApossessingbothexploitationandexplorationabilitiescaneffectivelyhandlesuchnon-convexproblemandobtainmuchbettersolutionthanitscounterpart.TheresultsshownonFig.7verifytheabilityoftheproposedFAinhandlinghighlynonlinearandmultimodaloptimizationproblems.ItcanbeobservedfromFig.7thatatagivennumberofRIS’sreflectiveelements,theperformancegapbetweentheproposedFAandtheAOdecreaseswhenthenumberofBS’santennasincreases.Forexample,whenNt=20,thegapsare,respectively,around7.5dBand3.5dBwithMt=3andMt=8.Fortunately,atagivennumberofBS’santennas,theperformancegapimproveswhenthenumberofRIS’selementsincreases.Forinstance,withMt=8,theperformancegapincreasesfromaround3.5dBto4.5dBwhenNtincreasesfrom20to30.Interestingly,theFAperformsespeciallywellwitharelativelyhighratioofNt/Mt,i.e.,theperformancegap + +IEEETRANSACTIONSONWIRELESSCOMMUNICATIONS,DOI:10.1109/TWC.2023.332871313020406080100120140160180200Maximum number of generations (T)101214161820222426Transmit power [dBm]Mt:8, Nt:30Mt:8, Nt:20Mt:3, Nt:30Mt:3, Nt:20Fig.8:ThetotalBS’stransmitpowerversusthenumberofmaximumgenerationswithdifferentnumbersofBS’santennasandRIS’sreflectiveelements.ThefireflypopulationisN=120.TherequiredSINRlevelis10dB.isaround9.5dBwiththerationof30/3whileitisaround3.5withtheratioof20/8.Theresultscanbeexplainedasfollows.AhighernumberofRIS’sreflectiveelementsgivesmoredegreeoffreedomfortheFAtoperform.Moreover,thechannelbetweentheRISandtheseusersplaysahigherrolethanthatbetweentheBSandtheRISdoesastheformerisclosertotheseusers.Lastbutnotleast,theperformancegapsslightlydecreaseatrelativelyhighSINRlevelespeciallywhentheNt/Mtratioisrelativelylow.Forexamplewiththeratioof20/8,theperformancegapisaround1.8dBatSINRof20dBcomparedwitharound3.5dBattheotherSINRlevels,i.e.,seethebottom-rightcornerfigureofFig.7.ThisisbecauseofafactthattheFAhasreacheditslimitofexplorationwithN=120fireflies,atastricterconstraintcondition.WenowcomparethecomputationalcomplexitiesoftheAOandFAapproachesfortheexperimentspresentedonFig.7.AsNtislargerthanMt,fromLemma5onecanshowthatthedominanttermofthecomplexityoftheAOapproachisn0N612t.Similarly,fromLemma6onecanconcludethatthedominanttermofthecomplexityoftheFAapproachisTN3N2t.SubstitutingforNt=30,n0=10,N=120andT=50,wecanarriveatthefactthatthecomputationalcomplexitiesoftheAOandFAapproachesareonthesameorderofO(1010).WhenthenumbersofantennasMtandNtarelarge,lettingNt=n0=Mtin(42),onecanshowthatthedominanttermofthecomplexitytoattainε-solutionto(32)isM712t.Ontheotherhand,onecanderivethedominanttermof(45)asM6twhenassumingT=N=Nt=Mt.Hence,thecomplexityofanIPMtosolve(32)ishigherthanthecomplexityoftheproposedFAinAlgorithm5,i.e.,O(M712t)incomparisonwithO(M6t).InFig.8,thetotalBS’stransmitpowerisplottedversusthemaximumofgenerationTusedintheFAinAlgorithm5withdifferentBS’santennasandRIS’sreflectiveelements.TheresultsindicatethattheproposedFArequiresaround50to6020406080100120140160180200Number of Population (N)2022242628303234Transmit power [dBm]Mt:8, Nt:30Mt:8, Nt:20Mt:3, Nt:30Mt:3, Nt:20Fig.9:ThetotaltransmitpowerversusthenumberofpopulationswithdifferentnumbersofBS’santennasandRIS’sreflectiveele-ments.ThenumberofmaximumgenerationsT=50.TherequiredSINRlevelis20dB.generationstoattaintheoptimalsolutionforallsetups.Fig.9illustratesthetotaltransmitpowerversusthenumberofpopulationNwithdifferentBS’santennasandRIS’selements.TheresultsshowthatincreasingthesizeofthefireflypopulationenablestheFAtoobtainbettersolution.Forexample,thetotaltransmitpowerdecreasesaround7dB,5.4dB,5dB,and3dB,respectively,forthesetupsof(Mt=8,Nt=20),(Mt=3,Nt=30),(Mt=8,Nt=20),and(Mt=3,Nt=20)whenthefireflypopulationincreasesfrom20to120.Theperformancegapatthe20dBSINRlevelobservedinFig.7for(Mt=8,Nt=20)canbeimproved1dBfurtherwhenthepopulationsizeisenlargedfrom120to200.Thesetotal-transmit-powercurvesconvergeafterN=180asthereductioninthetotaltransmitpowerisnegligiblewhenthepopulationincreasestoN=200forallsetups.D.EvaluationsonRIS-aidedWPTHere,weusethesamesetupfortheRIS-aidedcommu-nicationsystemasconsideredintheprevioussection,i.e.,SectionVII-C.However,theEHRsarerandomlyplacedwiththedistanceof2mfromtheRIS.Werunm0=10iterationstoobtainthesolutionfortheSCAapproach.Fig.10showsthesum-powerreceivedatEHRsversusBS’smaximumtransmitpowerwithdifferentnumbersofBS’santennasandRIS’sreflectiveelements.ItisclearfromthefigurethattheproposedFAapproachoutperformstheSCAapproachin[23]inofferinghighersum-poweratEHRs.Theperformancegapsare,respectively,around18dB,17dB,15dB,and14dBforthesetupsof(Mt=3,Nt=30),(Mt=8,Nt=30),(Mt=3,Nt=20),and(Mt=8,Nt=20).ThesuperiorperformanceoftheproposedFAovertheSCAisduetotheadvantageofhavingexploitationandexplorationabilitiestohandlenon-convexoptimizationproblems.Ontheotherhand,theSCAemploysthefirst-oderTaylorexpansiontoapproximatetheoptimizationproblemresultinginalower-boundedsolution.Furthermore,theFAapproachallocates + +IEEETRANSACTIONSONWIRELESSCOMMUNICATIONS,DOI:10.1109/TWC.2023.33287131410203040Maximun transmit power at BS [dBm]-120-110-100-90-80Sum-power at EHRs [dBm]Mt=3, Nt=30FASCA10203040Maximun transmit power at BS [dBm]-120-110-100-90-80Sum-power at EHRs [dBm]Mt=8, Nt=30FASCA10203040Maximun transmit power at BS [dBm]-120-110-100-90-80Sum-power at EHRs [dBm]Mt=3, Nt=20FASCA10203040Maximun transmit power at BS [dBm]-120-110-100-90-80Sum-power at EHRs [dBm]Mt=8, Nt=20FASCAFig.10:Sum-powerreceivedatEHRsversusBS’smaximumtransmitpowerwithdifferentnumbersofBS’santennasandRIS’sreflectiveelements.ThefireflypopulationisN=100.ThenumberofmaximumgenerationsT=50.oneactivebeamformingvectorforeachEHRwhereastheSCAonlyusesoneactivebeamformingvectorforallEHRs.TheresultsshownonFig.10againverifytheabilityoftheproposedFAinhandlinghighlynonlinearandmultimodaloptimizationproblems.ComparingFigs.7and10,itcanbeobservedthattheFAbehavesinasimilarmannerforbothpowerminimizationproblem(35)andsum-powermaximizationproblem(48).Forinstance,atthesamevalueofMt,thehigherthevalueofNt,thelargertheperformancegapis.AtthesamevalueofNt,thelowerthevalueofMt,thebiggertheperformancegapis.TheresultsalsorecommendtomaintainarelativelyhighratioofNt/MttoattainthebestperformanceoftheFA.SlightdeclinesintheperformancegapsarealsoobservedatthestricterconstraintofBS’stransmitpower,i.e.,40dBm,astheFA’spopulationreachtheirlimitofexploration.WeproceedbycomparingthecomputationalcomplexitiesoftheSCAandFAapproachesfortheexperimentsshownonFig.10.AsNtislargerthanMt,fromLemmas7and8,itisclearthatthedominanttermsofthecomplexitiesoftheSCAandtheFAapproachesare,respectively,m0N3tandTN3N2t.SubstitutingforNt=30,m0=10,N=100andT=50,wecanarriveatthefactthatthecomputationalcomplexitiesoftheSCAandFAapproachesare,respectively,ontheordersofO(105)andO(1010).WhenthenumbersofantennasMtandNtarelarge,lettingNt=m0=Mtin(51),onecanshowthatthedominanttermofthecomplexityoftheSCAisM4t.Ontheotherhand,thedominanttermof(52)isM6twhenassumingT=N=Nt=Mt.Hence,thecomplexityoftheSCAapproachislowerthanthatoftheproposedFAinAlgorithm6,i.e.,O(M4t)incomparisonwithO(M6t).Sum-powerreceivedatEHRsareshownversusthenumberofmaximumgenerationswithdifferentnumbersofBS’santennasandRIS’sreflectiveelementsinFig.11.ThefigurerevealsthattheproposedFAconvergesafteraround50to60generationsforallobservedsetups.020406080100120140160180200Maximum number of generations (T)-105-100-95-90-85-80Sum-power at EHRs [dBm]Mt:8, Nt:30Mt:8, Nt:20Mt:3, Nt:30Mt:3, Nt:20Fig.11:Sum-powerreceivedatEHRsversusthenumberofmaximumgenerationswithdifferentnumbersofBS’santennasandRIS’sreflectiveelements.ThefireflypopulationisN=100.TherequiredSINRlevelis10dB.020406080100120140160180Number of Population (N)-93-92-91-90-89-88-87-86-85-84-83Sum-power at EHRs [dBm]Mt:8, Nt:30Mt:8, Nt:20Mt:3, Nt:30Mt:3, Nt:20Fig.12:Sum-powerreceivedatEHRsversusthenumberofpopu-lationswithdifferentnumbersofBS’santennasandRIS’sreflectiveelements.ThenumberofmaximumgenerationsT=50.TherequiredSINRlevelis20dB.Theeffectofthefireflypopulationonthesum-powerreceivedatEHRsisillustratedonFig.12.Thefigureshowsthatallthecurvesconvergeafterthepopulationsizeof80.HoweverthedifferencebetweentheEHRs’sum-powerofferedby80firefliesandthatofferedby40firefliesisnomorethan0.7dBforallobservedsetups.ThisindicatesthatthecomplexityoftheproposedFAfortheRIS-aidedWPTsum-powermaximizationproblemin(48)canbereducedwithanacceptabletradeoffintheoptimality.VIII.ConclusionWehaveproposedageneralizedFAtofindoptimalsolutionforanoptimizationframeworkcontainingobjectivefunctionandconstraintsasmultivariatefunctionsofindependentopti-mizationvariables.Wehaveadoptedtheproposedgeneralized + +IEEETRANSACTIONSONWIRELESSCOMMUNICATIONS,DOI:10.1109/TWC.2023.332871315FAtosolvefourrepresentativeexamplesofclassictrans-mitbeamforming,cognitivebeamforming,RIS-aidedtransmitbeamforming,andRIS-aidedwirelesspowertransfer.OuranalyzeshaveindicatedthatthecomputationalcomplexitiesofproposedFAapproachesarelessthanthoseoftheirIPMcounterparts,i.e.,theSDPandtheAOapproaches,yethigherthanthatoftheiterativeandSCAapproachesinlarge-antennascenarios.SimulationresultshaverevealedthefactthattheproposedFAattainsthesameoptimalsolutionastheIMPdoesfortheunder-investigatedcognitivebeamformingprob-lem.Interestingly,theproposedFAoutperformstheiterative,AO,andSCAapproachesfortheunder-investigatedclassictransmitbeamforming,RIS-aidedtransmitbeamforming,andwirelesspowertransferproblems,respectively.ThisconfirmstheeffectivenessoftheproposedgeneralizedFAinhandlingmultivariateandnon-convexproblems.References[1]F.Rashid-Farrokhi,K.J.R.Liu,andL.Tassiulas,“Transmitbeamform-ingandpowercontrolforcellularwirelesssystems,”IEEEJ.Sel.AreasCommun.,vol.16,no.8,pp.1437–1450,Oct.1998.[2]A.Wiesel,Y.C.Eldar,andS.Shamai,“LinearprecodingviaConicoptimizationforfixedMIMOreceivers,”IEEETrans.SignalProcess.,vol.54,no.1,pp.161–176,Jan.2006.[3]Y.HuangandD.P.Palomar,“Rank-constrainedseparableSemidefiniteprogrammingwithapplicationstooptimalbeamforming,”IEEETrans.SignalProcess.,vol.58,no.2,pp.644–678,Feb.2010.[4]H.DahroujandW.Yu,“Multicellinterferencemitigationwithjointbeamformingandcommonmessagedecoding,”IEEETrans.Commun.,vol.59,no.8,pp.2264–2273,Aug.2011.[5]W.YangandG.Xu,“Optimaldownlinkpowerassignmentforsmartantennasystems,”inProc.IEEEInt.Conf.Acoustics,SpeechandSignalProcess.,ICASSP’98,vol.6,1998,pp.3337–3340.[6]M.SchubertandH.Boche,“Solutionofthemultiuserdownlinkbeam-formingproblemwithindividualSINRconstraints,”IEEETrans.Veh.Technol.,vol.53,no.1,pp.18–28,Jan.2004.[7]D.Hammarwall,M.Bengtsson,andB.Ottersten,“Ondownlinkbeam-formingwithindefiniteshapingconstraints,”IEEETrans.SignalPro-cess.,vol.54,no.9,pp.3566–3580,Sep.2006.[8]E.Bj¨ornson,M.Bengtsson,andB.Ottersten,“Optimalmultiusertrans-mitbeamforming:Adifficultproblemwithasimplesolutionstructure[lecturenotes],”IEEESignalProcess.Mag.,vol.31,no.4,pp.142–148,Jul.2014.[9]G.Zheng,K.-K.Wong,andT.-S.Ng,“ThroughputmaximizationinlinearmultiuserMIMO–OFDMdownlinksystems,”IEEETrans.Veh.Technol.,vol.57,no.3,pp.1993–1998,May2008.[10]R.BhagavatulaandR.W.Heath,“Adaptivelimitedfeedbackforsum-ratemaximizingbeamformingincooperativemulticellsystems,”IEEETrans.SignalProcess.,vol.59,no.2,pp.800–811,Feb.2011.[11]Y.HuangandD.P.Palomar,“Rank-constrainedseparablesemidefiniteprogrammingwithapplicationstooptimalbeamforming,”IEEETrans.SignalProcess.,vol.58,no.2,pp.664–678,Feb.2010.[12]Y.Huang,Q.Li,W.-K.Ma,andS.Zhang,“Robustmulticastbeamform-ingforspectrumsharing-basedcognitiveradios,”IEEETrans.SignalProcess.,vol.60,no.1,pp.527–533,Jan.2012.[13]B.Clerckx,R.Zhang,R.Schober,D.W.K.Ng,D.I.Kim,andH.V.Poor,“Fundamentalsofwirelessinformationandpowertransfer:FromRFenergyharvestermodelstosignalandsystemdesigns,”IEEEJ.Sel.AreasinCommun.,vol.37,no.1,pp.4–33,Jan.2019.[14]D.W.K.Ng,E.S.Lo,andR.Schober,“Robustbeamformingforsecurecommunicationinsystemswithwirelessinformationandpowertransfer,”IEEETrans.WirelessCommun.,vol.13,no.8,pp.4599–4615,Aug.2014.[15]——,“Wirelessinformationandpowertransfer:Energyefficiencyopti-mizationinOFDMAsystems,”IEEETrans.WirelessCommun.,vol.12,no.12,pp.6352–6370,Dec.2013.[16]T.A.Le,Q.-T.Vien,H.X.Nguyen,D.W.K.Ng,andR.Schober,“Robustchance-constrainedoptimizationforpower-efficientandsecureSWIPTsystems,”IEEETrans.GreenCommun.andNetw.,vol.1,no.3,pp.333–346,Sep.2017.[17]W.YuandT.Lan,“Transmitteroptimizationforthemulti-antennadownlinkwithper-antennapowerconstraints,”IEEETrans.SignalProcess.,vol.55,no.6,pp.2646–2660,Jun.2007.[18]T.A.LeandK.Navaie,“Downlinkbeamforminginunderlaycognitivecellularnetworks,”IEEETrans.Commun.,vol.62,no.7,pp.2212–2223,Jul.2014.[19]Z.-Q.Luo,W.-K.Ma,A.M.-C.So,Y.Ye,andS.Zhang,“Semidefiniterelaxationofquadraticoptimizationproblems,”IEEESignalProcess.Mag.,vol.27,no.3,pp.20–34,May2010.[20]M.BengtssonandB.Ottersten,“OptimaldownlinkbeamformingusingSemidefiniteoptimization,”inProc.37thAnnu.AllertonConf.Com-mun.,Control,andComput.,1999,pp.987–996.[21]Z.Peng,Z.Chen,C.Pan,G.Zhou,andH.Ren,“RobusttransmissiondesignforRIS-aidedcommunicationswithbothtransceiverhardwareimpairmentsandimperfectCSI,”IEEEWirelessCommun.Lett.,vol.11,no.3,pp.528–532,Mar.2022.[22]S.Gong,C.Xing,P.Yue,L.Zhao,andT.Q.S.Quek,“HybridanaloganddigitalbeamformingforRIS-assistedmmWavecommunications,”IEEETrans.WirelessCommun.,vol.22,no.3,pp.1537–1554,Mar.2023.[23]Q.WuandR.Zhang,“WeightedsumpowermaximizationforintelligentreflectingsurfaceaidedSWIPT,”IEEEWirelessCommun.Letters,vol.9,no.5,pp.586–590,May2020.[24]X.-S.Yang,Nature-InspiredMetaheuristicAlgorithms.LuniverPress,2008.[25]S.BoydandL.Vandenberghe,ConvexOptimization.CambridgeUniversityPress,2004. + +IEEETRANSACTIONSONWIRELESSCOMMUNICATIONS,DOI:10.1109/TWC.2023.332871316[26]X.-S.Yang,Engineeringoptimisation:anintroductionwithmetaheuris-ticapplications.Wiley,2009.[27]——,“Chapter13:Fireflyalgorithm:Variantsandapplications,”inSwarmIntelligenceAlgorithms.CRCPress,2020,pp.175–186.[28]T.YamanakaandK.Higuchi,“TransmitterbeamformingcontrolbasedonfireflyalgorithmformassiveMIMOsystemswithper-antennapowerconstraint,”in201723rdAsia-PacificConf.Commun.(APCC),2017,pp.1–6.[29]T.A.LeandX.-S.Yang,“FireflyalgorithmforbeamformingdesigninRIS-aidedcommunicationssystems,”inProc.IEEEVeh.Techno.Conf.(VTC2023-Spring),Jun.2023,pp.1–5.[30]I.Fister,I.F.Jr.,X.-S.Yang,andJ.Brest,“Acomprehensivereviewoffireflyalgorithms,”SwarmandEvolutionaryComputation,vol.13,pp.34–46,Dec.2013.[31]X.-S.YangandX.-S.He,WhytheFireflyAlgorithmWorks?Cham:SpringerInternationalPublishing,2018,pp.245–259.[32]W.WindartoandE.Eridani,“Comparisonofparticleswarmoptimiza-tionandfireflyalgorithminparameterestimationofLotka-Volterra,”AIPConferenceProceedings,vol.2268,no.1,p.050008,092020.[33]R.Ezzeldin,M.Zelenakova,H.F.Abd-Elhamid,K.Pietrucha-Urbanik,andS.Elabd,“Hybridoptimizationalgorithmsoffireflywithgaandpsofortheoptimaldesignofwaterdistributionnetworks,”Water,vol.15,no.10,2023.[34]M.ClercandJ.Kennedy,“Theparticleswarm-explosion,stability,andconvergenceinamultidimensionalcomplexspace,”IEEETrans.EvolutionaryComputation,vol.6,no.1,pp.58–73,2002.[35]D.BertsimasandJ.Tsitsiklis,“Simulatedannealing,”StatisticalScience,vol.8,no.1,pp.10–15,1993.[36]X.-S.Yang,CuckooSearchandFireflyAlgorithm:TheoryandApplica-tions.StudiesinComputationalIntelligence,2014.[37]E.Osaba,X.-S.Yang,F.Diaz,E.Onieva,A.D.Masegosa,andA.Peral-los,“Adiscretefireflyalgorithmtosolvearichvehicleroutingproblemmodellinganewspaperdistributionsystemwithrecyclingpolicy,”SoftComputing,vol.21,pp.5295–5308,2017.[38]H.DahroujandW.Yu,“Coordinatedbeamformingforthemulticellmulti-antennawirelesssystem,”IEEETrans.WirelessCommun.,vol.9,no.5,pp.1748–1759,May2010.[39]T.A.LeandM.R.Nakhai,“DownlinkoptimizationwithinterferencepricingandstatisticalCSI,”IEEETrans.Commun.,vol.61,no.6,pp.2339–2349,Jun2013.[40]CVXResearchInc.,“CVX:Matlabsoftwarefordisciplinedconvexprogramming,academicusers,”http://cvxr.com/cvx,2015.[41]K.-Y.Wang,A.M.-C.So,T.-H.Chang,W.-K.Ma,andC.-Y.Chi,“OutageconstrainedrobusttransmitoptimizationformultiuserMISOdownlinks:Tractableapproximationsbyconicoptimization,”IEEETrans.SignalProcess.,vol.62,no.21,pp.5690–5705,Nov.2014.[42]T.A.Le,T.V.Chien,andM.D.Renzo,“Robustprobabilistic-constrainedoptimizationforIRS-AidedMISOcommunicationsystems,”IEEEWirelessCommun.Lett.,vol.10,no.1,pp.1–5,Jan.2021.[43]H.Yu,H.D.Tuan,A.A.Nasir,T.Q.Duong,andH.V.Poor,“Jointdesignofreconfigurableintelligentsurfacesandtransmitbeamformingunderproperandimpropergaussiansignaling,”IEEEJ.Sel.AreasCommun.,vol.38,no.11,pp.2589–2603,Nov.2020.[44]N.S.Perovi´c,L.-N.Tran,M.D.Renzo,andM.F.Flanagan,“Optimiza-tionofRIS-aidedMIMOsystemsviathecutoffrate,”IEEEWirelessCommunicationsLetters,vol.10,no.8,pp.1692–1696,Aug.2021.TuanAnhLe(S’10-M’13-SM’19)receivedthePh.D.degreeintelecommunicationsresearchfromKing’sCollegeLondon,TheUniversityofLondon,U.K.,in2012.HewasaPost-DoctoralResearchFellowwiththeSchoolofElectronicandElectri-calEngineering,UniversityofLeeds,Leeds,U.K.HeisaSeniorLectureratMiddlesexUniversity,London,U.K.Hiscurrentresearchinterestsincludeintegratedsensingandcommunication(ISAC),RIS-aidedcommunication,RFenergyharvestingandwirelesspowertransfer,physical-layersecurity,nature-inspiredoptimization,andappliedmachinelearningforwirelesscommunications.HeseveredasaTechnicalProgramChairfor26thInternationalConferenceonTelecommuni-cations(ICT2019).HewasanExemplaryReviewerofIEEECommunicationsLettersin2019.Xin-SheYangobtainedhisDPhilinAppliedMath-ematicsfromtheUniversityofOxford.HethenworkedatCambridgeUniversityandNationalPhys-icalLaboratory(UK)asaSeniorResearchScientist.NowheisReaderatMiddlesexUniversityLondon,andaco-EditoroftheSpringerTractsinNature-InspiredComputing.HeisalsoanelectedFellowoftheInstituteofMathematicsanditsApplications.HewastheIEEEComputationalIntelligenceSociety(CIS)chairfortheTaskForceonBusinessIntelligenceandKnowledgeManagement(2015to2020).Hehaspublishedmorethan300peer-reviewedresearchpaperswithmorethan84,000citations,andhehasbeenontheprestigiouslistofhighly-citedresearchers(WebofSciences)foreightconsecutiveyears(2016-2023). \ No newline at end of file diff --git a/scratch/ingestion-markdown-error-repro-in-mem.cs b/scratch/ingestion-markdown-error-repro-in-mem.cs new file mode 100644 index 0000000..a07107c --- /dev/null +++ b/scratch/ingestion-markdown-error-repro-in-mem.cs @@ -0,0 +1,98 @@ +/* +To run with the default "./badfile.md": +dotnet run .\ingestion-markdown-error-repro-in-mem.cs +*/ + +#pragma warning disable S3903 + +#:package Microsoft.Extensions.DataIngestion +#:package Microsoft.Extensions.DataIngestion.Markdig + +using Microsoft.Extensions.DataIngestion; +using System.Text; + +var filePath = args.Length > 0 ? args[0] : "./badfile.md"; +if (!File.Exists(filePath)) +{ + Console.WriteLine($"File '{filePath}' does not exist."); + return; +} + +await ReadMarkdown( + """ + arXiv:2310.18460v1 [cs.IT] 27 Oct 2023 + # Good markdown + + Content + """, + "good-markdown"); + +await ReadMarkdown( + """ + arXiv:2310.18460v1 [cs.IT] 27 Oct 2023 + # Bad markdown + 3 + 2 + 0 + 2 + + t + c + O + 7 + 2 + + ] + T + I + . + s + c + [ + + 1 + v + 0 + 6 + 4 + 8 + 1 + . + 0 + 1 + 3 + 2 + : + v + i + X + r + a + + Content + """, + "bad-markdown"); + +async static Task ReadMarkdown(string content, string identifier, string mediaType = "text/markdown") +{ + Console.WriteLine($"Reading document with identifier '{identifier}' and media type '{mediaType}'"); + + using var stream = new MemoryStream(Encoding.UTF8.GetBytes(content)); + + var reader = new DocumentReader(); + var result = await reader.ReadAsync(stream, identifier, mediaType); + foreach (var contentItem in result.EnumerateContent()) + { + Console.WriteLine($"Content item: page {contentItem.PageNumber} - {contentItem.Text}"); + } +} + +internal sealed class DocumentReader : IngestionDocumentReader +{ + private readonly MarkdownReader _markdownReader = new(); + + public async override Task ReadAsync(Stream source, string identifier, string mediaType, CancellationToken cancellationToken = default) + { + return await _markdownReader.ReadAsync(source, identifier, mediaType, cancellationToken); + } +} diff --git a/scratch/ingestion-markdown-error-repro.cs b/scratch/ingestion-markdown-error-repro.cs new file mode 100644 index 0000000..a0b3db1 --- /dev/null +++ b/scratch/ingestion-markdown-error-repro.cs @@ -0,0 +1,40 @@ +/* +To run with the default "./badfile.md": +dotnet run .\ingestion-markdown-error-repro.cs + +Optionally, you can provide a path to a markdown file as the first argument: +dotnet run .\ingestion-markdown-error-repro.cs "./goodfile.md" +*/ + +#pragma warning disable S3903 + +#:package Microsoft.Extensions.DataIngestion +#:package Microsoft.Extensions.DataIngestion.Markdig + +using Microsoft.Extensions.DataIngestion; + +var filePath = args.Length > 0 ? args[0] : "./badfile.md"; +if (!File.Exists(filePath)) +{ + Console.WriteLine($"File '{filePath}' does not exist."); + return; +} + +var reader = new DocumentReader(); + +var result = await reader.ReadAsync(new FileInfo(filePath), "test-doc"); +foreach (var contentItem in result.EnumerateContent()) +{ + Console.WriteLine($"Content item: page {contentItem.PageNumber} - {contentItem.Text}"); +} + +internal sealed class DocumentReader : IngestionDocumentReader +{ + private readonly MarkdownReader _markdownReader = new(); + + public async override Task ReadAsync(Stream source, string identifier, string mediaType, CancellationToken cancellationToken = default) + { + Console.WriteLine($"Reading document with identifier '{identifier}' and media type '{mediaType}'"); + return await _markdownReader.ReadAsync(source, identifier, mediaType, cancellationToken); + } +} diff --git a/scratch/small-badfile.md b/scratch/small-badfile.md new file mode 100644 index 0000000..9a9780e --- /dev/null +++ b/scratch/small-badfile.md @@ -0,0 +1,68 @@ +IEEE TRANSACTIONS ON WIRELESS COMMUNICATIONS, DOI: 10.1109/TWC.2023.3328713 + +1 + +Generalized Firefly Algorithm for Optimal Transmit +Beamforming + +Tuan Anh Le and Xin-She Yang + +3 +2 +0 +2 + +t +c +O +7 +2 + +] +T +I +. +s +c +[ + +1 +v +0 +6 +4 +8 +1 +. +0 +1 +3 +2 +: +v +i +X +r +a + +Abstract—This paper proposes a generalized Firefly Algorithm +(FA) to solve an optimization framework having objective func- +tion and constraints as multivariate functions of independent +optimization variables. Four representative examples of how +the proposed generalized FA can be adopted to solve down- +link beamforming problems are shown for a classic transmit +beamforming, cognitive beamforming, reconfigurable-intelligent- +surfaces-aided (RIS-aided) transmit beamforming, and RIS-aided +wireless power transfer (WPT). Complexity analyzes indicate that +in large-antenna regimes the proposed FA approaches require +less computational complexity than their corresponding interior +point methods (IPMs) do, yet demand a higher complexity than +the iterative and the successive convex approximation (SCA) +approaches do. Simulation results reveal that the proposed FA +attains the same global optimal solution as that of the IPM for +an optimization problem in cognitive beamforming. On the other +hand, the proposed FA approaches outperform the iterative, IPM +and SCA in terms of obtaining better solution for optimization +problems, respectively, for a classic transmit beamforming, RIS- +aided transmit beamforming and RIS-aided WPT. + diff --git a/scratch/small-goodfile.md b/scratch/small-goodfile.md new file mode 100644 index 0000000..795644f --- /dev/null +++ b/scratch/small-goodfile.md @@ -0,0 +1,30 @@ +arXiv:2310.18460v1 [cs.IT] 27 Oct 2023 +IEEE TRANSACTIONS ON WIRELESS COMMUNICATIONS, DOI: 10.1109/TWC.2023.3328713 + +1 + +Generalized Firefly Algorithm for Optimal Transmit +Beamforming + +Tuan Anh Le and Xin-She Yang + +Abstract—This paper proposes a generalized Firefly Algorithm +(FA) to solve an optimization framework having objective func- +tion and constraints as multivariate functions of independent +optimization variables. Four representative examples of how +the proposed generalized FA can be adopted to solve down- +link beamforming problems are shown for a classic transmit +beamforming, cognitive beamforming, reconfigurable-intelligent- +surfaces-aided (RIS-aided) transmit beamforming, and RIS-aided +wireless power transfer (WPT). Complexity analyzes indicate that +in large-antenna regimes the proposed FA approaches require +less computational complexity than their corresponding interior +point methods (IPMs) do, yet demand a higher complexity than +the iterative and the successive convex approximation (SCA) +approaches do. Simulation results reveal that the proposed FA +attains the same global optimal solution as that of the IPM for +an optimization problem in cognitive beamforming. On the other +hand, the proposed FA approaches outperform the iterative, IPM +and SCA in terms of obtaining better solution for optimization +problems, respectively, for a classic transmit beamforming, RIS- +aided transmit beamforming and RIS-aided WPT. \ No newline at end of file diff --git a/src/Microsoft.Extensions.DataIngestion.MarkItDown/CHANGELOG.md b/src/Microsoft.Extensions.DataIngestion.MarkItDown/CHANGELOG.md new file mode 100644 index 0000000..7cf926b --- /dev/null +++ b/src/Microsoft.Extensions.DataIngestion.MarkItDown/CHANGELOG.md @@ -0,0 +1,5 @@ +# Release History + +## 10.0.0-preview.1 + +- Initial preview release diff --git a/src/Microsoft.Extensions.DataIngestion.MarkItDown/MarkItDownMcpReader.cs b/src/Microsoft.Extensions.DataIngestion.MarkItDown/MarkItDownMcpReader.cs new file mode 100644 index 0000000..cbec488 --- /dev/null +++ b/src/Microsoft.Extensions.DataIngestion.MarkItDown/MarkItDownMcpReader.cs @@ -0,0 +1,109 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Collections.Generic; +using System.IO; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.AI; +using Microsoft.Shared.Diagnostics; +using ModelContextProtocol.Client; +using ModelContextProtocol.Protocol; + +namespace Microsoft.Extensions.DataIngestion; + +/// +/// Reads documents by converting them to Markdown using the MarkItDown MCP server. +/// +public class MarkItDownMcpReader : IngestionDocumentReader +{ + private readonly Uri _mcpServerUri; + private readonly McpClientOptions? _options; + + /// + /// Initializes a new instance of the class. + /// + /// The URI of the MarkItDown MCP server (e.g., http://localhost:3001/mcp). + /// Optional MCP client options for configuring the connection. + public MarkItDownMcpReader(Uri mcpServerUri, McpClientOptions? options = null) + { + _mcpServerUri = Throw.IfNull(mcpServerUri); + _options = options; + } + + /// + public override async Task ReadAsync(FileInfo source, string identifier, string? mediaType = null, CancellationToken cancellationToken = default) + { + _ = Throw.IfNull(source); + _ = Throw.IfNullOrEmpty(identifier); + + if (!source.Exists) + { + throw new FileNotFoundException("The specified file does not exist.", source.FullName); + } + + // Read file content and create DataContent (media type is inferred from extension if not provided) + DataContent dataContent = await DataContent.LoadFromAsync(source.FullName, mediaType, cancellationToken).ConfigureAwait(false); + + string markdown = await ConvertToMarkdownAsync(dataContent, cancellationToken).ConfigureAwait(false); + + return MarkdownParser.Parse(markdown, identifier); + } + + /// + public override async Task ReadAsync(Stream source, string identifier, string mediaType, CancellationToken cancellationToken = default) + { + _ = Throw.IfNull(source); + _ = Throw.IfNullOrEmpty(identifier); + + // Read stream content and create DataContent (media type is inferred from FileStream path if applicable) + DataContent dataContent = await DataContent.LoadFromAsync(source, mediaType, cancellationToken).ConfigureAwait(false); + + string markdown = await ConvertToMarkdownAsync(dataContent, cancellationToken).ConfigureAwait(false); + + return MarkdownParser.Parse(markdown, identifier); + } + + private async Task ConvertToMarkdownAsync(DataContent dataContent, CancellationToken cancellationToken) + { + // Create HTTP client transport for MCP + HttpClientTransport transport = new(new HttpClientTransportOptions + { + Endpoint = _mcpServerUri + }); + + await using (transport.ConfigureAwait(false)) + { + // Create MCP client + McpClient client = await McpClient.CreateAsync(transport, _options, cancellationToken: cancellationToken).ConfigureAwait(false); + + await using (client.ConfigureAwait(false)) + { + // Build parameters for convert_to_markdown tool + Dictionary parameters = new() + { + ["uri"] = dataContent.Uri + }; + + // Call the convert_to_markdown tool + var result = await client.CallToolAsync("convert_to_markdown", parameters, cancellationToken: cancellationToken).ConfigureAwait(false); + + // Extract markdown content from result + // The result is expected to be in the format: { "content": [{ "type": "text", "text": "markdown content" }] } + if (result.Content != null && result.Content.Count > 0) + { + foreach (var content in result.Content) + { + if (content.Type == "text" && content is TextContentBlock textBlock) + { + return textBlock.Text; + } + } + } + } + } + + throw new InvalidOperationException("Failed to convert document to markdown: unexpected response format from MCP server."); + } +} diff --git a/src/Microsoft.Extensions.DataIngestion.MarkItDown/MarkItDownReader.cs b/src/Microsoft.Extensions.DataIngestion.MarkItDown/MarkItDownReader.cs new file mode 100644 index 0000000..79b60f3 --- /dev/null +++ b/src/Microsoft.Extensions.DataIngestion.MarkItDown/MarkItDownReader.cs @@ -0,0 +1,133 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Diagnostics; +using System.IO; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Shared.Diagnostics; + +namespace Microsoft.Extensions.DataIngestion; + +/// +/// Reads documents by converting them to Markdown using the MarkItDown tool. +/// +public class MarkItDownReader : IngestionDocumentReader +{ + private readonly FileInfo? _exePath; + private readonly bool _extractImages; + + /// + /// Initializes a new instance of the class. + /// + /// The path to the MarkItDown executable. When not provided, "markitdown" needs to be added to PATH. + /// A value indicating whether to extract images. + public MarkItDownReader(FileInfo? exePath = null, bool extractImages = false) + { + _exePath = exePath; + _extractImages = extractImages; + } + + /// + public override async Task ReadAsync(FileInfo source, string identifier, string? mediaType = null, CancellationToken cancellationToken = default) + { + _ = Throw.IfNull(source); + _ = Throw.IfNullOrEmpty(identifier); + + if (!source.Exists) + { + throw new FileNotFoundException("The specified file does not exist.", source.FullName); + } + + // Manually set ProcessStartInfo.WorkingDirectory to a "safe location": + // - If exePath is provided, use its directory. + // - Otherwise, use AppContext.BaseDirectory (the directory of the running application). + string workingDirectory = _exePath?.Directory?.FullName ?? AppContext.BaseDirectory; + + ProcessStartInfo startInfo = new() + { + FileName = _exePath?.FullName ?? "markitdown", + WorkingDirectory = workingDirectory, + UseShellExecute = false, + CreateNoWindow = true, + RedirectStandardOutput = true, + StandardOutputEncoding = Encoding.UTF8, + }; + + // Force UTF-8 encoding in the environment (will produce garbage otherwise). + startInfo.Environment["PYTHONIOENCODING"] = "utf-8"; + startInfo.Environment["LC_ALL"] = "C.UTF-8"; + startInfo.Environment["LANG"] = "C.UTF-8"; + +#if NET + startInfo.ArgumentList.Add(source.FullName); + if (_extractImages) + { + startInfo.ArgumentList.Add("--keep-data-uris"); + } +#else + startInfo.Arguments = $"\"{source.FullName}\"" + (_extractImages ? " --keep-data-uris" : string.Empty); +#endif + + string outputContent = string.Empty; + using (Process process = new() { StartInfo = startInfo }) + { + process.Start(); + + outputContent = await process.StandardOutput.ReadToEndAsync(cancellationToken).ConfigureAwait(false); +#if NET + await process.WaitForExitAsync(cancellationToken).ConfigureAwait(false); +#else + process.WaitForExit(); +#endif + + if (process.ExitCode != 0) + { + throw new InvalidOperationException($"MarkItDown process failed with exit code {process.ExitCode}."); + } + } + + return MarkdownParser.Parse(outputContent, identifier); + } + + /// + /// The contents of are copied to a temporary file. + public override async Task ReadAsync(Stream source, string identifier, string mediaType, CancellationToken cancellationToken = default) + { + _ = Throw.IfNull(source); + _ = Throw.IfNullOrEmpty(identifier); + + // Instead of creating a temporary file, we could write to the StandardInput of the process. + // MarkItDown says it supports reading from stdin, but it does not work as expected. + // Even the sample command line does not work with stdin: "cat example.pdf | markitdown" + // I can be doing something wrong, but for now, let's write to a temporary file. + string inputFilePath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); + FileStream inputFile = new(inputFilePath, FileMode.CreateNew, FileAccess.Write, FileShare.None, bufferSize: 1, FileOptions.Asynchronous); + + try + { + await source +#if NET + .CopyToAsync(inputFile, cancellationToken) +#else + .CopyToAsync(inputFile) +#endif + .ConfigureAwait(false); + + inputFile.Close(); + + return await ReadAsync(new FileInfo(inputFilePath), identifier, mediaType, cancellationToken).ConfigureAwait(false); + } + finally + { +#if NET + await inputFile.DisposeAsync().ConfigureAwait(false); +#else + inputFile.Dispose(); +#endif + File.Delete(inputFilePath); + } + } +} diff --git a/src/Microsoft.Extensions.DataIngestion.MarkItDown/Microsoft.Extensions.DataIngestion.MarkItDown.csproj b/src/Microsoft.Extensions.DataIngestion.MarkItDown/Microsoft.Extensions.DataIngestion.MarkItDown.csproj new file mode 100644 index 0000000..7574763 --- /dev/null +++ b/src/Microsoft.Extensions.DataIngestion.MarkItDown/Microsoft.Extensions.DataIngestion.MarkItDown.csproj @@ -0,0 +1,37 @@ + + + + + Microsoft.Extensions.DataIngestion + Implementation of IngestionDocumentReader abstraction for MarkItDown. + RAG + RAG;ingestion;documents;markitdown + + preview + false + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Microsoft.Extensions.DataIngestion.MarkItDown/README.md b/src/Microsoft.Extensions.DataIngestion.MarkItDown/README.md new file mode 100644 index 0000000..095011b --- /dev/null +++ b/src/Microsoft.Extensions.DataIngestion.MarkItDown/README.md @@ -0,0 +1,91 @@ +# Microsoft.Extensions.DataIngestion.MarkItDown + +Provides an implementation of the `IngestionDocumentReader` class for the [MarkItDown](https://github.com/microsoft/markitdown/) utility. + +## Install the package + +From the command-line: + +```console +dotnet add package Microsoft.Extensions.DataIngestion.MarkItDown --prerelease +``` + +Or directly in the C# project file: + +```xml + + + +``` + +## Usage Examples + +### Creating a MarkItDownReader for Data Ingestion (Local Process) + +Use `MarkItDownReader` to convert documents using the MarkItDown executable installed locally: + +```csharp +using Microsoft.Extensions.DataIngestion; + +IngestionDocumentReader reader = + new MarkItDownReader(new FileInfo(@"pathToMarkItDown.exe"), extractImages: true); + +using IngestionPipeline pipeline = new(reader, CreateChunker(), CreateWriter()); +``` + +### Creating a MarkItDownMcpReader for Data Ingestion (MCP Server) + +Use `MarkItDownMcpReader` to convert documents using a MarkItDown MCP server: + +```csharp +using Microsoft.Extensions.DataIngestion; + +// Connect to a MarkItDown MCP server (e.g., running in Docker) +IngestionDocumentReader reader = + new MarkItDownMcpReader(new Uri("http://localhost:3001/mcp")); + +using IngestionPipeline pipeline = new(reader, CreateChunker(), CreateWriter()); +``` + +The MarkItDown MCP server can be run using Docker: + +```bash +docker run -p 3001:3001 mcp/markitdown --http --host 0.0.0.0 --port 3001 +``` + +Or installed via pip: + +```bash +pip install markitdown-mcp-server +markitdown-mcp --http --host 0.0.0.0 --port 3001 +``` + +### Integrating with Aspire + +Aspire can be used for seamless integration with [MarkItDown MCP](https://github.com/microsoft/markitdown/tree/main/packages/markitdown-mcp). Sample AppHost logic: + +```csharp +var builder = DistributedApplication.CreateBuilder(args); + +var markitdown = builder.AddContainer("markitdown", "mcp/markitdown") + .WithArgs("--http", "--host", "0.0.0.0", "--port", "3001") + .WithHttpEndpoint(targetPort: 3001, name: "http"); + +var webApp = builder.AddProject("name"); + +webApp.WithEnvironment("MARKITDOWN_MCP_URL", markitdown.GetEndpoint("http")); + +builder.Build().Run(); +``` + +Sample Ingestion Service: + +```csharp +string url = $"{Environment.GetEnvironmentVariable("MARKITDOWN_MCP_URL")}/mcp"; + +IngestionDocumentReader reader = new MarkItDownMcpReader(new Uri(url)); +``` + +## Feedback & Contributing + +We welcome feedback and contributions in [our GitHub repo](https://github.com/dotnet/extensions). diff --git a/src/Microsoft.Extensions.DataIngestion.Markdig/CHANGELOG.md b/src/Microsoft.Extensions.DataIngestion.Markdig/CHANGELOG.md new file mode 100644 index 0000000..7cf926b --- /dev/null +++ b/src/Microsoft.Extensions.DataIngestion.Markdig/CHANGELOG.md @@ -0,0 +1,5 @@ +# Release History + +## 10.0.0-preview.1 + +- Initial preview release diff --git a/src/Microsoft.Extensions.DataIngestion.Markdig/MarkdownParser.cs b/src/Microsoft.Extensions.DataIngestion.Markdig/MarkdownParser.cs new file mode 100644 index 0000000..5b02f91 --- /dev/null +++ b/src/Microsoft.Extensions.DataIngestion.Markdig/MarkdownParser.cs @@ -0,0 +1,325 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Diagnostics; +using System.Linq; +using System.Text; +using Markdig; +using Markdig.Extensions.Tables; +using Markdig.Syntax; +using Markdig.Syntax.Inlines; +using Microsoft.Shared.Diagnostics; + +namespace Microsoft.Extensions.DataIngestion; + +internal static class MarkdownParser +{ + internal static IngestionDocument Parse(string markdown, string identifier) + { + _ = Throw.IfNullOrEmpty(markdown); + _ = Throw.IfNullOrEmpty(identifier); + + // Markdig's "UseAdvancedExtensions" option includes many common extensions beyond + // CommonMark, such as citations, figures, footnotes, grid tables, mathematics + // task lists, diagrams, and more. + var pipeline = new MarkdownPipelineBuilder() + .UseAdvancedExtensions() + .Build(); + + MarkdownDocument markdownDocument = Markdown.Parse(markdown, pipeline); + return Map(markdownDocument, markdown, identifier); + } + +#if !NET + internal static System.Threading.Tasks.Task ReadToEndAsync(this System.IO.StreamReader reader, System.Threading.CancellationToken cancellationToken) + => cancellationToken.IsCancellationRequested ? System.Threading.Tasks.Task.FromCanceled(cancellationToken) : reader.ReadToEndAsync(); +#endif + + private static IngestionDocument Map(MarkdownDocument markdownDocument, string documentMarkdown, string identifier) + { + IngestionDocumentSection rootSection = new(documentMarkdown); + IngestionDocument result = new(identifier) + { + Sections = { rootSection } + }; + + bool previousWasBreak = false; + foreach (Block block in markdownDocument) + { + if (block is ThematicBreakBlock breakBlock) + { + // We have encountered a thematic break (horizontal rule): ----------- etc. + previousWasBreak = true; + continue; + } + + if (block is LinkReferenceDefinitionGroup linkReferenceGroup) + { + continue; // In the future, we might want to handle links differently. + } + + if (IsEmptyBlock(block)) + { + continue; + } + + rootSection.Elements.Add(MapBlock(documentMarkdown, previousWasBreak, block)); + previousWasBreak = false; + } + + return result; + } + + private static bool IsEmptyBlock(Block block) // Block with no text. Sample: QuoteBlock the next block is a quote. + => block is LeafBlock emptyLeafBlock && (emptyLeafBlock.Inline is null || emptyLeafBlock.Inline.FirstChild is null); + + private static IngestionDocumentElement MapBlock(string documentMarkdown, bool previousWasBreak, Block block) + { + string elementMarkdown = documentMarkdown.Substring(block.Span.Start, block.Span.Length); + + IngestionDocumentElement element = block switch + { + LeafBlock leafBlock => MapLeafBlockToElement(leafBlock, previousWasBreak, elementMarkdown), + ListBlock listBlock => MapListBlock(listBlock, previousWasBreak, documentMarkdown, elementMarkdown), + QuoteBlock quoteBlock => MapQuoteBlock(quoteBlock, previousWasBreak, documentMarkdown, elementMarkdown), + Table table => new IngestionDocumentTable(elementMarkdown, GetCells(table, documentMarkdown)), + _ => throw new NotSupportedException($"Block type '{block.GetType().Name}' is not supported.") + }; + + return element; + } + + private static IngestionDocumentElement MapLeafBlockToElement(LeafBlock block, bool previousWasBreak, string elementMarkdown) + => block switch + { + HeadingBlock heading => new IngestionDocumentHeader(elementMarkdown) + { + Text = GetText(heading.Inline), + Level = heading.Level + }, + ParagraphBlock footer when previousWasBreak => new IngestionDocumentFooter(elementMarkdown) + { + Text = GetText(footer.Inline), + }, + ParagraphBlock image when image.Inline!.Descendants().FirstOrDefault() is LinkInline link && link.IsImage => MapImage(elementMarkdown, link), + ParagraphBlock paragraph => new IngestionDocumentParagraph(elementMarkdown) + { + Text = GetText(paragraph.Inline), + }, + CodeBlock codeBlock => new IngestionDocumentParagraph(elementMarkdown) + { + Text = GetText(codeBlock.Inline), + }, + _ => throw new NotSupportedException($"Block type '{block.GetType().Name}' is not supported.") + }; + + private static IngestionDocumentImage MapImage(string elementMarkdown, LinkInline link) + { + IngestionDocumentImage result = new(elementMarkdown); + + // ![Alt text](data:image/type;base64,...) + if (link.FirstChild is LiteralInline literal) + { + result.AlternativeText = literal.Content.ToString(); + } + + if (link.Url is not null && link.Url.StartsWith("data:image/", StringComparison.Ordinal)) + { + // Parse the data URL format: data:image/{type};base64,{data} + ReadOnlySpan url = link.Url.AsSpan("data:".Length); + + // Find the semicolon that separates media type from encoding + int semicolonIndex = url.IndexOf(';'); + if (semicolonIndex > 0) + { + ReadOnlySpan mediaType = url.Slice(0, semicolonIndex); + + // Find the comma that separates encoding from data + int commaIndex = url.IndexOf(','); + if (commaIndex > semicolonIndex) + { + // Check if it's base64 encoded + ReadOnlySpan encoding = url.Slice(semicolonIndex + 1, commaIndex - semicolonIndex - 1); + if (encoding.SequenceEqual("base64".AsSpan())) + { + result.Content = Convert.FromBase64String(url.Slice(commaIndex + 1).ToString()); + result.MediaType = mediaType.ToString(); + } + } + } + } + + return result; + } + + private static IngestionDocumentSection MapListBlock(ListBlock listBlock, bool previousWasBreak, string documentMarkdown, string listMarkdown) + { + IngestionDocumentSection list = new(listMarkdown); + foreach (Block? item in listBlock) + { + if (item is not ListItemBlock listItemBlock) + { + continue; + } + + foreach (Block? child in listItemBlock) + { + if (child is not LeafBlock leafBlock || IsEmptyBlock(leafBlock)) + { + continue; // Skip empty blocks in lists + } + + string childMarkdown = documentMarkdown.Substring(leafBlock.Span.Start, leafBlock.Span.Length); + IngestionDocumentElement element = MapLeafBlockToElement(leafBlock, previousWasBreak, childMarkdown); + list.Elements.Add(element); + } + } + + return list; + } + + private static IngestionDocumentSection MapQuoteBlock(QuoteBlock quoteBlock, bool previousWasBreak, string documentMarkdown, string elementMarkdown) + { + IngestionDocumentSection quote = new(elementMarkdown); + foreach (Block child in quoteBlock) + { + if (IsEmptyBlock(child)) + { + continue; // Skip empty blocks in quotes + } + + quote.Elements.Add(MapBlock(documentMarkdown, previousWasBreak, child)); + } + + return quote; + } + + private static string? GetText(ContainerInline? containerInline) + { + Debug.Assert(containerInline != null, "ContainerInline should not be null here."); + Debug.Assert(containerInline!.FirstChild != null, "FirstChild should not be null here."); + + if (ReferenceEquals(containerInline.FirstChild, containerInline.LastChild)) + { + // If there is only one child, return its text. + return containerInline.FirstChild!.ToString(); + } + + StringBuilder content = new(100); + foreach (Inline inline in containerInline) + { +#pragma warning disable IDE0058 // Expression value is never used + if (inline is LiteralInline literalInline) + { + content.Append(literalInline.Content); + } + else if (inline is LineBreakInline) + { + content.AppendLine(); // Append a new line for line breaks + } + else if (inline is ContainerInline another) + { + // EmphasisInline is also a ContainerInline, but it does not get any special treatment, + // as we use raw text here (instead of a markdown, where emphasis can be expressed). + content.Append(GetText(another)); + } + else if (inline is CodeInline codeInline) + { + content.Append(codeInline.Content); + } + else if (inline is HtmlInline htmlInline) + { + content.Append(htmlInline.Tag); + } + else + { + throw new NotSupportedException($"Inline type '{inline.GetType().Name}' is not supported."); + } +#pragma warning restore IDE0058 // Expression value is never used + } + + return content.ToString(); + } + +#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional +#pragma warning disable S3967 // Multidimensional arrays should not be used + private static IngestionDocumentElement?[,] GetCells(Table table, string outputContent) + { + int firstRowIndex = SkipFirstRow(table, outputContent) ? 1 : 0; + + // Calculate the actual number of columns by examining the rows. + // table.ColumnDefinitions.Count can vary: for tables WITH trailing pipes it's (columns + 1), + // but for tables WITHOUT trailing pipes it's equal to the actual column count. + int columnCount = GetColumnCount(table, firstRowIndex); + var cells = new IngestionDocumentElement?[table.Count - firstRowIndex, columnCount]; + + for (int rowIndex = firstRowIndex; rowIndex < table.Count; rowIndex++) + { + var tableRow = (TableRow)table[rowIndex]; + int columnIndex = 0; + for (int cellIndex = 0; cellIndex < tableRow.Count; cellIndex++) + { + var tableCell = (TableCell)tableRow[cellIndex]; + var content = tableCell.Count switch + { + 0 => null, + 1 => MapBlock(outputContent, previousWasBreak: false, tableCell[0]), + _ => throw new NotSupportedException($"Cells with {tableCell.Count} elements are not supported.") + }; + + for (int columnSpan = 0; columnSpan < tableCell.ColumnSpan; columnSpan++, columnIndex++) + { + // tableCell.ColumnIndex defaults to -1, so it's not used here. + cells[rowIndex - firstRowIndex, columnIndex] = content; + } + } + } + + return cells; + + static int GetColumnCount(Table table, int firstRowIndex) + { + int maxColumns = 0; + for (int rowIndex = firstRowIndex; rowIndex < table.Count; rowIndex++) + { + var tableRow = (TableRow)table[rowIndex]; + int columnCount = 0; + for (int cellIndex = 0; cellIndex < tableRow.Count; cellIndex++) + { + var tableCell = (TableCell)tableRow[cellIndex]; + columnCount += tableCell.ColumnSpan; + } + + maxColumns = Math.Max(maxColumns, columnCount); + } + + return maxColumns; + } + + // Some parsers like MarkItDown include a row with invalid markdown before the separator row: + // | | | | | + // | --- | --- | --- | --- | + static bool SkipFirstRow(Table table, string outputContent) + { + if (table.Count > 0) + { + var firstRow = (TableRow)table[0]; + for (int cellIndex = 0; cellIndex < firstRow.Count; cellIndex++) + { + var tableCell = (TableCell)firstRow[cellIndex]; + if (!string.IsNullOrWhiteSpace(MapBlock(outputContent, previousWasBreak: false, tableCell[0]).Text)) + { + return false; + } + } + + return true; + } + + return false; + } + } +#pragma warning restore CA1814 // Prefer jagged arrays over multidimensional +#pragma warning restore S3967 // Multidimensional arrays should not be used +} diff --git a/src/Microsoft.Extensions.DataIngestion.Markdig/MarkdownReader.cs b/src/Microsoft.Extensions.DataIngestion.Markdig/MarkdownReader.cs new file mode 100644 index 0000000..1afabd0 --- /dev/null +++ b/src/Microsoft.Extensions.DataIngestion.Markdig/MarkdownReader.cs @@ -0,0 +1,52 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.IO; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Shared.Diagnostics; + +namespace Microsoft.Extensions.DataIngestion; + +/// +/// Reads Markdown content and converts it to an . +/// +public sealed class MarkdownReader : IngestionDocumentReader +{ + /// + public override async Task ReadAsync(FileInfo source, string identifier, string? mediaType = null, CancellationToken cancellationToken = default) + { + _ = Throw.IfNull(source); + _ = Throw.IfNullOrEmpty(identifier); + +#if NET + string fileContent = await File.ReadAllTextAsync(source.FullName, cancellationToken).ConfigureAwait(false); +#else + using FileStream stream = new(source.FullName, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 1, FileOptions.Asynchronous); + string fileContent = await ReadToEndAsync(stream, cancellationToken).ConfigureAwait(false); +#endif + return MarkdownParser.Parse(fileContent, identifier); + } + + /// + public override async Task ReadAsync(Stream source, string identifier, string mediaType, CancellationToken cancellationToken = default) + { + _ = Throw.IfNull(source); + _ = Throw.IfNullOrEmpty(identifier); + + string fileContent = await ReadToEndAsync(source, cancellationToken).ConfigureAwait(false); + return MarkdownParser.Parse(fileContent, identifier); + } + + private static async Task ReadToEndAsync(Stream source, CancellationToken cancellationToken) + { + using StreamReader reader = +#if NET + new(source, leaveOpen: true); +#else + new(source, System.Text.Encoding.UTF8, detectEncodingFromByteOrderMarks: true, bufferSize: 1024, leaveOpen: true); +#endif + + return await reader.ReadToEndAsync(cancellationToken).ConfigureAwait(false); + } +} diff --git a/src/Microsoft.Extensions.DataIngestion.Markdig/Microsoft.Extensions.DataIngestion.Markdig.csproj b/src/Microsoft.Extensions.DataIngestion.Markdig/Microsoft.Extensions.DataIngestion.Markdig.csproj new file mode 100644 index 0000000..5dbe9c2 --- /dev/null +++ b/src/Microsoft.Extensions.DataIngestion.Markdig/Microsoft.Extensions.DataIngestion.Markdig.csproj @@ -0,0 +1,24 @@ + + + + $(TargetFrameworks);netstandard2.0 + Microsoft.Extensions.DataIngestion + Implementation of IngestionDocumentReader abstraction for Markdown. + RAG + RAG;ingestion;documents;markdown + true + preview + false + 75 + 75 + + + + + + + + + + + diff --git a/src/Microsoft.Extensions.DataIngestion.Markdig/README.md b/src/Microsoft.Extensions.DataIngestion.Markdig/README.md new file mode 100644 index 0000000..c6a2328 --- /dev/null +++ b/src/Microsoft.Extensions.DataIngestion.Markdig/README.md @@ -0,0 +1,35 @@ +# Microsoft.Extensions.DataIngestion.Markdig + +Provides an implementation of the `IngestionDocumentReader` class for the Markdown files using [MarkDig](https://github.com/xoofx/markdig) library. + +## Install the package + +From the command-line: + +```console +dotnet add package Microsoft.Extensions.DataIngestion.Markdig --prerelease +``` + +Or directly in the C# project file: + +```xml + + + +``` + +## Usage Examples + +### Creating a MarkdownReader for Data Ingestion + +```csharp +using Microsoft.Extensions.DataIngestion; + +IngestionDocumentReader reader = new MarkdownReader(); + +using IngestionPipeline pipeline = new(reader, CreateChunker(), CreateWriter()); +``` + +## Feedback & Contributing + +We welcome feedback and contributions in [our GitHub repo](https://github.com/dotnet/extensions). diff --git a/src/Shared/Throw/README.md b/src/Shared/Throw/README.md new file mode 100644 index 0000000..2c93998 --- /dev/null +++ b/src/Shared/Throw/README.md @@ -0,0 +1,11 @@ +# Throw + +Efficient exception throwing utilities. + +To use this in your project, add the following to your `.csproj` file: + +```xml + + true + +``` diff --git a/src/Shared/Throw/Throw.cs b/src/Shared/Throw/Throw.cs new file mode 100644 index 0000000..257a880 --- /dev/null +++ b/src/Shared/Throw/Throw.cs @@ -0,0 +1,988 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; + +#pragma warning disable CA1716 +namespace Microsoft.Shared.Diagnostics; +#pragma warning restore CA1716 + +/// +/// Defines static methods used to throw exceptions. +/// +/// +/// The main purpose is to reduce code size, improve performance, and standardize exception +/// messages. +/// +[SuppressMessage("Minor Code Smell", "S4136:Method overloads should be grouped together", Justification = "Doesn't work with the region layout")] +[SuppressMessage("Minor Code Smell", "S2333:Partial is gratuitous in this context", Justification = "Some projects add additional partial parts.")] +[SuppressMessage("Design", "CA1716", Justification = "Not part of an API")] + +#if !SHARED_PROJECT +[ExcludeFromCodeCoverage] +#endif + +internal static partial class Throw +{ + #region For Object + + /// + /// Throws an if the specified argument is . + /// + /// Argument type to be checked for . + /// Object to be checked for . + /// The name of the parameter being checked. + /// The original value of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [return: NotNull] + public static T IfNull([NotNull] T argument, [CallerArgumentExpression(nameof(argument))] string paramName = "") + { + if (argument is null) + { + ArgumentNullException(paramName); + } + + return argument; + } + + /// + /// Throws an if the specified argument is , + /// or if the specified member is . + /// + /// Argument type to be checked for . + /// Member type to be checked for . + /// Argument to be checked for . + /// Object member to be checked for . + /// The name of the parameter being checked. + /// The name of the member. + /// The original value of . + /// + /// + /// Throws.IfNullOrMemberNull(myObject, myObject?.MyProperty) + /// + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [return: NotNull] + public static TMember IfNullOrMemberNull( + [NotNull] TParameter argument, + [NotNull] TMember member, + [CallerArgumentExpression(nameof(argument))] string paramName = "", + [CallerArgumentExpression(nameof(member))] string memberName = "") + { + if (argument is null) + { + ArgumentNullException(paramName); + } + + if (member is null) + { + ArgumentException(paramName, $"Member {memberName} of {paramName} is null"); + } + + return member; + } + + /// + /// Throws an if the specified member is . + /// + /// Argument type. + /// Member type to be checked for . + /// Argument to which member belongs. + /// Object member to be checked for . + /// The name of the parameter being checked. + /// The name of the member. + /// The original value of . + /// + /// + /// Throws.IfMemberNull(myObject, myObject.MyProperty) + /// + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [return: NotNull] + [SuppressMessage("Style", "IDE0060:Remove unused parameter", Justification = "Analyzer isn't seeing the reference to 'argument' in the attribute")] + public static TMember IfMemberNull( + TParameter argument, + [NotNull] TMember member, + [CallerArgumentExpression(nameof(argument))] string paramName = "", + [CallerArgumentExpression(nameof(member))] string memberName = "") + where TParameter : notnull + { + if (member is null) + { + ArgumentException(paramName, $"Member {memberName} of {paramName} is null"); + } + + return member; + } + + #endregion + + #region For String + + /// + /// Throws either an or an + /// if the specified string is or whitespace respectively. + /// + /// String to be checked for or whitespace. + /// The name of the parameter being checked. + /// The original value of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [return: NotNull] + public static string IfNullOrWhitespace([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string paramName = "") + { +#if !NETCOREAPP3_1_OR_GREATER + if (argument == null) + { + ArgumentNullException(paramName); + } +#endif + + if (string.IsNullOrWhiteSpace(argument)) + { + if (argument == null) + { + ArgumentNullException(paramName); + } + else + { + ArgumentException(paramName, "Argument is whitespace"); + } + } + + return argument; + } + + /// + /// Throws an if the string is , + /// or if it is empty. + /// + /// String to be checked for or empty. + /// The name of the parameter being checked. + /// The original value of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [return: NotNull] + public static string IfNullOrEmpty([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string paramName = "") + { +#if !NETCOREAPP3_1_OR_GREATER + if (argument == null) + { + ArgumentNullException(paramName); + } +#endif + + if (string.IsNullOrEmpty(argument)) + { + if (argument == null) + { + ArgumentNullException(paramName); + } + else + { + ArgumentException(paramName, "Argument is an empty string"); + } + } + + return argument; + } + + #endregion + + #region For Buffer + + /// + /// Throws an if the argument's buffer size is less than the required buffer size. + /// + /// The actual buffer size. + /// The required buffer size. + /// The name of the parameter to be checked. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IfBufferTooSmall(int bufferSize, int requiredSize, string paramName = "") + { + if (bufferSize < requiredSize) + { + ArgumentException(paramName, $"Buffer too small, needed a size of {requiredSize} but got {bufferSize}"); + } + } + + #endregion + + #region For Enums + + /// + /// Throws an if the enum value is not valid. + /// + /// The argument to evaluate. + /// The name of the parameter being checked. + /// The type of the enumeration. + /// The original value of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static T IfOutOfRange(T argument, [CallerArgumentExpression(nameof(argument))] string paramName = "") + where T : struct, Enum + { +#if NET5_0_OR_GREATER + if (!Enum.IsDefined(argument)) +#else + if (!Enum.IsDefined(typeof(T), argument)) +#endif + { + ArgumentOutOfRangeException(paramName, $"{argument} is an invalid value for enum type {typeof(T)}"); + } + + return argument; + } + + #endregion + + #region For Collections + + /// + /// Throws an if the collection is , + /// or if it is empty. + /// + /// The collection to evaluate. + /// The name of the parameter being checked. + /// The type of objects in the collection. + /// The original value of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [return: NotNull] + + // The method has actually 100% coverage, but due to a bug in the code coverage tool, + // a lower number is reported. Therefore, we temporarily exclude this method + // from the coverage measurements. Once the bug in the code coverage tool is fixed, + // the exclusion attribute can be removed. + [ExcludeFromCodeCoverage] + public static IEnumerable IfNullOrEmpty([NotNull] IEnumerable? argument, [CallerArgumentExpression(nameof(argument))] string paramName = "") + { + if (argument == null) + { + ArgumentNullException(paramName); + } + else + { + switch (argument) + { + case ICollection collection: + if (collection.Count == 0) + { + ArgumentException(paramName, "Collection is empty"); + } + + break; + case IReadOnlyCollection readOnlyCollection: + if (readOnlyCollection.Count == 0) + { + ArgumentException(paramName, "Collection is empty"); + } + + break; + default: + using (IEnumerator enumerator = argument.GetEnumerator()) + { + if (!enumerator.MoveNext()) + { + ArgumentException(paramName, "Collection is empty"); + } + } + + break; + } + } + + return argument; + } + + #endregion + + #region Exceptions + + /// + /// Throws an . + /// + /// The name of the parameter that caused the exception. +#if !NET6_0_OR_GREATER + [MethodImpl(MethodImplOptions.NoInlining)] +#endif + [DoesNotReturn] + public static void ArgumentNullException(string paramName) + => throw new ArgumentNullException(paramName); + + /// + /// Throws an . + /// + /// The name of the parameter that caused the exception. + /// A message that describes the error. +#if !NET6_0_OR_GREATER + [MethodImpl(MethodImplOptions.NoInlining)] +#endif + [DoesNotReturn] + public static void ArgumentNullException(string paramName, string? message) + => throw new ArgumentNullException(paramName, message); + + /// + /// Throws an . + /// + /// The name of the parameter that caused the exception. +#if !NET6_0_OR_GREATER + [MethodImpl(MethodImplOptions.NoInlining)] +#endif + [DoesNotReturn] + public static void ArgumentOutOfRangeException(string paramName) + => throw new ArgumentOutOfRangeException(paramName); + + /// + /// Throws an . + /// + /// The name of the parameter that caused the exception. + /// A message that describes the error. +#if !NET6_0_OR_GREATER + [MethodImpl(MethodImplOptions.NoInlining)] +#endif + [DoesNotReturn] + public static void ArgumentOutOfRangeException(string paramName, string? message) + => throw new ArgumentOutOfRangeException(paramName, message); + + /// + /// Throws an . + /// + /// The name of the parameter that caused the exception. + /// The value of the argument that caused this exception. + /// A message that describes the error. +#if !NET6_0_OR_GREATER + [MethodImpl(MethodImplOptions.NoInlining)] +#endif + [DoesNotReturn] + public static void ArgumentOutOfRangeException(string paramName, object? actualValue, string? message) + => throw new ArgumentOutOfRangeException(paramName, actualValue, message); + + /// + /// Throws an . + /// + /// The name of the parameter that caused the exception. + /// A message that describes the error. +#if !NET6_0_OR_GREATER + [MethodImpl(MethodImplOptions.NoInlining)] +#endif + [DoesNotReturn] + public static void ArgumentException(string paramName, string? message) + => throw new ArgumentException(message, paramName); + + /// + /// Throws an . + /// + /// The name of the parameter that caused the exception. + /// A message that describes the error. + /// The exception that is the cause of the current exception. + /// + /// If the is not a , the current exception is raised in a catch + /// block that handles the inner exception. + /// +#if !NET6_0_OR_GREATER + [MethodImpl(MethodImplOptions.NoInlining)] +#endif + [DoesNotReturn] + public static void ArgumentException(string paramName, string? message, Exception? innerException) + => throw new ArgumentException(message, paramName, innerException); + + /// + /// Throws an . + /// + /// A message that describes the error. +#if !NET6_0_OR_GREATER + [MethodImpl(MethodImplOptions.NoInlining)] +#endif + [DoesNotReturn] + public static void InvalidOperationException(string message) + => throw new InvalidOperationException(message); + + /// + /// Throws an . + /// + /// A message that describes the error. + /// The exception that is the cause of the current exception. +#if !NET6_0_OR_GREATER + [MethodImpl(MethodImplOptions.NoInlining)] +#endif + [DoesNotReturn] + public static void InvalidOperationException(string message, Exception? innerException) + => throw new InvalidOperationException(message, innerException); + + #endregion + + #region For Integer + + /// + /// Throws an if the specified number is less than min. + /// + /// Number to be expected being less than min. + /// The number that must be less than the argument. + /// The name of the parameter being checked. + /// The original value of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static int IfLessThan(int argument, int min, [CallerArgumentExpression(nameof(argument))] string paramName = "") + { + if (argument < min) + { + ArgumentOutOfRangeException(paramName, argument, $"Argument less than minimum value {min}"); + } + + return argument; + } + + /// + /// Throws an if the specified number is greater than max. + /// + /// Number to be expected being greater than max. + /// The number that must be greater than the argument. + /// The name of the parameter being checked. + /// The original value of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static int IfGreaterThan(int argument, int max, [CallerArgumentExpression(nameof(argument))] string paramName = "") + { + if (argument > max) + { + ArgumentOutOfRangeException(paramName, argument, $"Argument greater than maximum value {max}"); + } + + return argument; + } + + /// + /// Throws an if the specified number is less or equal than min. + /// + /// Number to be expected being less or equal than min. + /// The number that must be less or equal than the argument. + /// The name of the parameter being checked. + /// The original value of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static int IfLessThanOrEqual(int argument, int min, [CallerArgumentExpression(nameof(argument))] string paramName = "") + { + if (argument <= min) + { + ArgumentOutOfRangeException(paramName, argument, $"Argument less or equal than minimum value {min}"); + } + + return argument; + } + + /// + /// Throws an if the specified number is greater or equal than max. + /// + /// Number to be expected being greater or equal than max. + /// The number that must be greater or equal than the argument. + /// The name of the parameter being checked. + /// The original value of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static int IfGreaterThanOrEqual(int argument, int max, [CallerArgumentExpression(nameof(argument))] string paramName = "") + { + if (argument >= max) + { + ArgumentOutOfRangeException(paramName, argument, $"Argument greater or equal than maximum value {max}"); + } + + return argument; + } + + /// + /// Throws an if the specified number is not in the specified range. + /// + /// Number to be expected being greater or equal than max. + /// The lower bound of the allowed range of argument values. + /// The upper bound of the allowed range of argument values. + /// The name of the parameter being checked. + /// The original value of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static int IfOutOfRange(int argument, int min, int max, [CallerArgumentExpression(nameof(argument))] string paramName = "") + { + if (argument < min || argument > max) + { + ArgumentOutOfRangeException(paramName, argument, $"Argument not in the range [{min}..{max}]"); + } + + return argument; + } + + /// + /// Throws an if the specified number is equal to 0. + /// + /// Number to be expected being not equal to zero. + /// The name of the parameter being checked. + /// The original value of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static int IfZero(int argument, [CallerArgumentExpression(nameof(argument))] string paramName = "") + { + if (argument == 0) + { + ArgumentOutOfRangeException(paramName, "Argument is zero"); + } + + return argument; + } + + #endregion + + #region For Unsigned Integer + + /// + /// Throws an if the specified number is less than min. + /// + /// Number to be expected being less than min. + /// The number that must be less than the argument. + /// The name of the parameter being checked. + /// The original value of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static uint IfLessThan(uint argument, uint min, [CallerArgumentExpression(nameof(argument))] string paramName = "") + { + if (argument < min) + { + ArgumentOutOfRangeException(paramName, argument, $"Argument less than minimum value {min}"); + } + + return argument; + } + + /// + /// Throws an if the specified number is greater than max. + /// + /// Number to be expected being greater than max. + /// The number that must be greater than the argument. + /// The name of the parameter being checked. + /// The original value of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static uint IfGreaterThan(uint argument, uint max, [CallerArgumentExpression(nameof(argument))] string paramName = "") + { + if (argument > max) + { + ArgumentOutOfRangeException(paramName, argument, $"Argument greater than maximum value {max}"); + } + + return argument; + } + + /// + /// Throws an if the specified number is less or equal than min. + /// + /// Number to be expected being less or equal than min. + /// The number that must be less or equal than the argument. + /// The name of the parameter being checked. + /// The original value of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static uint IfLessThanOrEqual(uint argument, uint min, [CallerArgumentExpression(nameof(argument))] string paramName = "") + { + if (argument <= min) + { + ArgumentOutOfRangeException(paramName, argument, $"Argument less or equal than minimum value {min}"); + } + + return argument; + } + + /// + /// Throws an if the specified number is greater or equal than max. + /// + /// Number to be expected being greater or equal than max. + /// The number that must be greater or equal than the argument. + /// The name of the parameter being checked. + /// The original value of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static uint IfGreaterThanOrEqual(uint argument, uint max, [CallerArgumentExpression(nameof(argument))] string paramName = "") + { + if (argument >= max) + { + ArgumentOutOfRangeException(paramName, argument, $"Argument greater or equal than maximum value {max}"); + } + + return argument; + } + + /// + /// Throws an if the specified number is not in the specified range. + /// + /// Number to be expected being greater or equal than max. + /// The lower bound of the allowed range of argument values. + /// The upper bound of the allowed range of argument values. + /// The name of the parameter being checked. + /// The original value of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static uint IfOutOfRange(uint argument, uint min, uint max, [CallerArgumentExpression(nameof(argument))] string paramName = "") + { + if (argument < min || argument > max) + { + ArgumentOutOfRangeException(paramName, argument, $"Argument not in the range [{min}..{max}]"); + } + + return argument; + } + + /// + /// Throws an if the specified number is equal to 0. + /// + /// Number to be expected being not equal to zero. + /// The name of the parameter being checked. + /// The original value of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static uint IfZero(uint argument, [CallerArgumentExpression(nameof(argument))] string paramName = "") + { + if (argument == 0U) + { + ArgumentOutOfRangeException(paramName, "Argument is zero"); + } + + return argument; + } + + #endregion + + #region For Long + + /// + /// Throws an if the specified number is less than min. + /// + /// Number to be expected being less than min. + /// The number that must be less than the argument. + /// The name of the parameter being checked. + /// The original value of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static long IfLessThan(long argument, long min, [CallerArgumentExpression(nameof(argument))] string paramName = "") + { + if (argument < min) + { + ArgumentOutOfRangeException(paramName, argument, $"Argument less than minimum value {min}"); + } + + return argument; + } + + /// + /// Throws an if the specified number is greater than max. + /// + /// Number to be expected being greater than max. + /// The number that must be greater than the argument. + /// The name of the parameter being checked. + /// The original value of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static long IfGreaterThan(long argument, long max, [CallerArgumentExpression(nameof(argument))] string paramName = "") + { + if (argument > max) + { + ArgumentOutOfRangeException(paramName, argument, $"Argument greater than maximum value {max}"); + } + + return argument; + } + + /// + /// Throws an if the specified number is less or equal than min. + /// + /// Number to be expected being less or equal than min. + /// The number that must be less or equal than the argument. + /// The name of the parameter being checked. + /// The original value of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static long IfLessThanOrEqual(long argument, long min, [CallerArgumentExpression(nameof(argument))] string paramName = "") + { + if (argument <= min) + { + ArgumentOutOfRangeException(paramName, argument, $"Argument less or equal than minimum value {min}"); + } + + return argument; + } + + /// + /// Throws an if the specified number is greater or equal than max. + /// + /// Number to be expected being greater or equal than max. + /// The number that must be greater or equal than the argument. + /// The name of the parameter being checked. + /// The original value of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static long IfGreaterThanOrEqual(long argument, long max, [CallerArgumentExpression(nameof(argument))] string paramName = "") + { + if (argument >= max) + { + ArgumentOutOfRangeException(paramName, argument, $"Argument greater or equal than maximum value {max}"); + } + + return argument; + } + + /// + /// Throws an if the specified number is not in the specified range. + /// + /// Number to be expected being greater or equal than max. + /// The lower bound of the allowed range of argument values. + /// The upper bound of the allowed range of argument values. + /// The name of the parameter being checked. + /// The original value of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static long IfOutOfRange(long argument, long min, long max, [CallerArgumentExpression(nameof(argument))] string paramName = "") + { + if (argument < min || argument > max) + { + ArgumentOutOfRangeException(paramName, argument, $"Argument not in the range [{min}..{max}]"); + } + + return argument; + } + + /// + /// Throws an if the specified number is equal to 0. + /// + /// Number to be expected being not equal to zero. + /// The name of the parameter being checked. + /// The original value of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static long IfZero(long argument, [CallerArgumentExpression(nameof(argument))] string paramName = "") + { + if (argument == 0L) + { + ArgumentOutOfRangeException(paramName, "Argument is zero"); + } + + return argument; + } + + #endregion + + #region For Unsigned Long + + /// + /// Throws an if the specified number is less than min. + /// + /// Number to be expected being less than min. + /// The number that must be less than the argument. + /// The name of the parameter being checked. + /// The original value of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ulong IfLessThan(ulong argument, ulong min, [CallerArgumentExpression(nameof(argument))] string paramName = "") + { + if (argument < min) + { + ArgumentOutOfRangeException(paramName, argument, $"Argument less than minimum value {min}"); + } + + return argument; + } + + /// + /// Throws an if the specified number is greater than max. + /// + /// Number to be expected being greater than max. + /// The number that must be greater than the argument. + /// The name of the parameter being checked. + /// The original value of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ulong IfGreaterThan(ulong argument, ulong max, [CallerArgumentExpression(nameof(argument))] string paramName = "") + { + if (argument > max) + { + ArgumentOutOfRangeException(paramName, argument, $"Argument greater than maximum value {max}"); + } + + return argument; + } + + /// + /// Throws an if the specified number is less or equal than min. + /// + /// Number to be expected being less or equal than min. + /// The number that must be less or equal than the argument. + /// The name of the parameter being checked. + /// The original value of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ulong IfLessThanOrEqual(ulong argument, ulong min, [CallerArgumentExpression(nameof(argument))] string paramName = "") + { + if (argument <= min) + { + ArgumentOutOfRangeException(paramName, argument, $"Argument less or equal than minimum value {min}"); + } + + return argument; + } + + /// + /// Throws an if the specified number is greater or equal than max. + /// + /// Number to be expected being greater or equal than max. + /// The number that must be greater or equal than the argument. + /// The name of the parameter being checked. + /// The original value of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ulong IfGreaterThanOrEqual(ulong argument, ulong max, [CallerArgumentExpression(nameof(argument))] string paramName = "") + { + if (argument >= max) + { + ArgumentOutOfRangeException(paramName, argument, $"Argument greater or equal than maximum value {max}"); + } + + return argument; + } + + /// + /// Throws an if the specified number is not in the specified range. + /// + /// Number to be expected being greater or equal than max. + /// The lower bound of the allowed range of argument values. + /// The upper bound of the allowed range of argument values. + /// The name of the parameter being checked. + /// The original value of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ulong IfOutOfRange(ulong argument, ulong min, ulong max, [CallerArgumentExpression(nameof(argument))] string paramName = "") + { + if (argument < min || argument > max) + { + ArgumentOutOfRangeException(paramName, argument, $"Argument not in the range [{min}..{max}]"); + } + + return argument; + } + + /// + /// Throws an if the specified number is equal to 0. + /// + /// Number to be expected being not equal to zero. + /// The name of the parameter being checked. + /// The original value of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ulong IfZero(ulong argument, [CallerArgumentExpression(nameof(argument))] string paramName = "") + { + if (argument == 0UL) + { + ArgumentOutOfRangeException(paramName, "Argument is zero"); + } + + return argument; + } + + #endregion + + #region For Double + + /// + /// Throws an if the specified number is less than min. + /// + /// Number to be expected being less than min. + /// The number that must be less than the argument. + /// The name of the parameter being checked. + /// The original value of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static double IfLessThan(double argument, double min, [CallerArgumentExpression(nameof(argument))] string paramName = "") + { + // strange conditional needed in order to handle NaN values correctly +#pragma warning disable S1940 // Boolean checks should not be inverted + if (!(argument >= min)) +#pragma warning restore S1940 // Boolean checks should not be inverted + { + ArgumentOutOfRangeException(paramName, argument, $"Argument less than minimum value {min}"); + } + + return argument; + } + + /// + /// Throws an if the specified number is greater than max. + /// + /// Number to be expected being greater than max. + /// The number that must be greater than the argument. + /// The name of the parameter being checked. + /// The original value of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static double IfGreaterThan(double argument, double max, [CallerArgumentExpression(nameof(argument))] string paramName = "") + { + // strange conditional needed in order to handle NaN values correctly +#pragma warning disable S1940 // Boolean checks should not be inverted + if (!(argument <= max)) +#pragma warning restore S1940 // Boolean checks should not be inverted + { + ArgumentOutOfRangeException(paramName, argument, $"Argument greater than maximum value {max}"); + } + + return argument; + } + + /// + /// Throws an if the specified number is less or equal than min. + /// + /// Number to be expected being less or equal than min. + /// The number that must be less or equal than the argument. + /// The name of the parameter being checked. + /// The original value of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static double IfLessThanOrEqual(double argument, double min, [CallerArgumentExpression(nameof(argument))] string paramName = "") + { + // strange conditional needed in order to handle NaN values correctly +#pragma warning disable S1940 // Boolean checks should not be inverted + if (!(argument > min)) +#pragma warning restore S1940 // Boolean checks should not be inverted + { + ArgumentOutOfRangeException(paramName, argument, $"Argument less or equal than minimum value {min}"); + } + + return argument; + } + + /// + /// Throws an if the specified number is greater or equal than max. + /// + /// Number to be expected being greater or equal than max. + /// The number that must be greater or equal than the argument. + /// The name of the parameter being checked. + /// The original value of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static double IfGreaterThanOrEqual(double argument, double max, [CallerArgumentExpression(nameof(argument))] string paramName = "") + { + // strange conditional needed in order to handle NaN values correctly +#pragma warning disable S1940 // Boolean checks should not be inverted + if (!(argument < max)) +#pragma warning restore S1940 // Boolean checks should not be inverted + { + ArgumentOutOfRangeException(paramName, argument, $"Argument greater or equal than maximum value {max}"); + } + + return argument; + } + + /// + /// Throws an if the specified number is not in the specified range. + /// + /// Number to be expected being greater or equal than max. + /// The lower bound of the allowed range of argument values. + /// The upper bound of the allowed range of argument values. + /// The name of the parameter being checked. + /// The original value of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static double IfOutOfRange(double argument, double min, double max, [CallerArgumentExpression(nameof(argument))] string paramName = "") + { + // strange conditional needed in order to handle NaN values correctly + if (!(min <= argument && argument <= max)) + { + ArgumentOutOfRangeException(paramName, argument, $"Argument not in the range [{min}..{max}]"); + } + + return argument; + } + + /// + /// Throws an if the specified number is equal to 0. + /// + /// Number to be expected being not equal to zero. + /// The name of the parameter being checked. + /// The original value of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static double IfZero(double argument, [CallerArgumentExpression(nameof(argument))] string paramName = "") + { +#pragma warning disable S1244 // Floating point numbers should not be tested for equality + if (argument == 0.0) +#pragma warning restore S1244 // Floating point numbers should not be tested for equality + { + ArgumentOutOfRangeException(paramName, "Argument is zero"); + } + + return argument; + } + + #endregion +} diff --git a/src/Sql.SemanticSearch.AppHost/Extensions/ResourceBuilderExtensions.cs b/src/Sql.SemanticSearch.AppHost/Extensions/ResourceBuilderExtensions.cs index 23058ff..374a6cc 100644 --- a/src/Sql.SemanticSearch.AppHost/Extensions/ResourceBuilderExtensions.cs +++ b/src/Sql.SemanticSearch.AppHost/Extensions/ResourceBuilderExtensions.cs @@ -1,5 +1,4 @@ -using Sql.SemanticSearch.AppHost.Extensions; -using System.Diagnostics.CodeAnalysis; +using System.Diagnostics.CodeAnalysis; using System.Globalization; namespace Sql.SemanticSearch.AppHost.Extensions; diff --git a/src/Sql.SemanticSearch.AppHost/Sql.SemanticSearch.AppHost.csproj b/src/Sql.SemanticSearch.AppHost/Sql.SemanticSearch.AppHost.csproj index 54195c4..ca09fba 100644 --- a/src/Sql.SemanticSearch.AppHost/Sql.SemanticSearch.AppHost.csproj +++ b/src/Sql.SemanticSearch.AppHost/Sql.SemanticSearch.AppHost.csproj @@ -1,4 +1,4 @@ - + Exe diff --git a/src/Sql.SemanticSearch.Core/Chunking/DocumentChunkingService.cs b/src/Sql.SemanticSearch.Core/Chunking/DocumentChunkingService.cs index e1215d1..1671d72 100644 --- a/src/Sql.SemanticSearch.Core/Chunking/DocumentChunkingService.cs +++ b/src/Sql.SemanticSearch.Core/Chunking/DocumentChunkingService.cs @@ -1,52 +1,33 @@ using Microsoft.Extensions.DataIngestion; using Microsoft.Extensions.Logging; using Microsoft.ML.Tokenizers; -using Polly.Registry; using Sql.SemanticSearch.Core.ArXiv.Exceptions; using Sql.SemanticSearch.Core.Chunking.Interfaces; -using Sql.SemanticSearch.Core.Configuration; -using Sql.SemanticSearch.Core.Data.Interfaces; -using Sql.SemanticSearch.Shared; -using System.Globalization; namespace Sql.SemanticSearch.Core.Chunking; public class DocumentChunkingService( - IDatabaseConnection databaseConnection, IDocumentReader reader, - ResiliencePipelineProvider resiliencePipelineProvider, - AISettings aiSettings, + IngestionChunkWriter chunkWriter, ILogger logger) : IDocumentChunkingService { - private readonly IDatabaseConnection _databaseConnection = databaseConnection; private readonly IDocumentReader _reader = reader; - private readonly ResiliencePipelineProvider _resiliencePipelineProvider = resiliencePipelineProvider; - private readonly AISettings _aiSettings = aiSettings; + private readonly IngestionChunkWriter _chunkWriter = chunkWriter; private readonly ILogger _logger = logger; - // HeaderChunker holds a StringBuilder per instance and is not thread-safe; keep as instance field. - private readonly HeaderChunker _chunker = CreateChunker(); - - // MarkItDownReader is stateless (shells out to the markitdown CLI); safe to share. - //private static readonly MarkItDownReader _reader = new(); - - private static readonly Action _logChunkSaved = - LoggerMessage.Define( - LogLevel.Debug, - new EventId(0, nameof(DocumentChunkingService)), - "Saved chunk {ChunkId} for document {DocumentId}."); + private static readonly Action _logPdfDownloadFailed = + LoggerMessage.Define( + LogLevel.Warning, + new EventId(1, nameof(DocumentChunkingService)), + "Failed to download PDF for document {DocumentId} from {Uri}. Falling back to PdfPig."); public async Task IndexDocument(DatabaseDocument document, CancellationToken cancellationToken = default) { ArgumentNullException.ThrowIfNull(document); - await DeleteExistingChunks(document.Id, cancellationToken); - + var chunker = CreateChunker(); var ingestionDocument = await LoadIngestionDocumentAsync(document, cancellationToken); - await foreach (var chunk in _chunker.ProcessAsync(ingestionDocument, cancellationToken)) - { - await WriteDocumentChunk(document.Id, chunk.Content, cancellationToken); - } + await _chunkWriter.WriteAsync(chunker.ProcessAsync(ingestionDocument, cancellationToken), cancellationToken); } private async Task LoadIngestionDocumentAsync(DatabaseDocument document, CancellationToken cancellationToken) @@ -56,7 +37,19 @@ private async Task LoadIngestionDocumentAsync(DatabaseDocumen throw new ArxivPdfDownloadException($"Cannot download PDF for document {document.Id}: invalid or missing URI."); } - return await _reader.Read(uri, document.ArxivId, default, cancellationToken: cancellationToken); + try + { + return await _reader.Read(uri, document.ArxivId, default, cancellationToken: cancellationToken); + } +#pragma warning disable CA1031 // Intentional catch-all: any failure from MarkItDown should fall back to PdfPig + catch (Exception ex) +#pragma warning restore CA1031 + { + // fallback to PdfPig + _logPdfDownloadFailed(_logger, document.Id, uri, ex); + + return await _reader.ReadWithPdfPig(uri, document.ArxivId, cancellationToken: cancellationToken); + } } private static HeaderChunker CreateChunker() @@ -68,73 +61,4 @@ private static HeaderChunker CreateChunker() OverlapTokens = 0 }); } - - private async Task DeleteExistingChunks(int documentId, CancellationToken cancellationToken) - { - var resiliencePipeline = _resiliencePipelineProvider.GetPipeline(ResiliencePipelineNames.SqlServerRetry); - await resiliencePipeline.ExecuteAsync( - async context => - { - await DeleteChunks(documentId, cancellationToken); - }, - cancellationToken); - } - - private async Task DeleteChunks(int documentId, CancellationToken cancellationToken) => - await _databaseConnection.ExecuteAsync( - """ - DELETE FROM dbo.DocumentChunkEmbeddings - WHERE [Id] IN (SELECT [Id] FROM dbo.DocumentChunks WHERE [DocumentId] = @DocumentId); - - DELETE FROM dbo.DocumentChunks - WHERE [DocumentId] = @DocumentId; - """, - new { DocumentId = documentId }); - - - private async Task WriteDocumentChunk(int documentId, string content, CancellationToken cancellationToken) - { - var resiliencePipeline = _resiliencePipelineProvider.GetPipeline(ResiliencePipelineNames.SqlServerRetry); - await resiliencePipeline.ExecuteAsync( - async context => - { - using var connection = _databaseConnection.CreateConnection(); - connection.Open(); - using var transaction = connection.BeginTransaction(); - - //await DeleteExistingDocumentIfExists(paper.Id, transaction); - - var chunkId = await SaveDocumentChunk(documentId, content, transaction); - await SaveDocumentChunkEmbedding(chunkId, transaction); - _logChunkSaved(_logger, chunkId, documentId, null); - - transaction.Commit(); - }, - cancellationToken); - - } - - - private async Task SaveDocumentChunk(int documentId, string content, System.Data.IDbTransaction transaction) => - await _databaseConnection.ExecuteScalarAsync( - """ - INSERT INTO dbo.DocumentChunks ([DocumentId], [Content]) - VALUES (@DocumentId, @Content); - - SELECT CAST(SCOPE_IDENTITY() as int); - """, - new { DocumentId = documentId, Content = content }, - transaction: transaction); - - /* Note: Embedding model is *NOT* a SQL injection risk, it must be hard-coded so we have to use the settings value. */ - private async Task SaveDocumentChunkEmbedding(int chunkId, System.Data.IDbTransaction transaction) => - await _databaseConnection.ExecuteAsync( - $""" - INSERT INTO dbo.DocumentChunkEmbeddings ([Id], [Embedding]) - SELECT @Id, AI_GENERATE_EMBEDDINGS([Content] USE MODEL {_aiSettings.ExternalEmbeddingModel}) - FROM dbo.DocumentChunks - WHERE [Id] = @Id; - """, - new { Id = chunkId }, - transaction: transaction); } diff --git a/src/Sql.SemanticSearch.Core/Chunking/Interfaces/IDocumentReader.cs b/src/Sql.SemanticSearch.Core/Chunking/Interfaces/IDocumentReader.cs index 6240c4e..0a0ef10 100644 --- a/src/Sql.SemanticSearch.Core/Chunking/Interfaces/IDocumentReader.cs +++ b/src/Sql.SemanticSearch.Core/Chunking/Interfaces/IDocumentReader.cs @@ -5,4 +5,6 @@ namespace Sql.SemanticSearch.Core.Chunking.Interfaces; public interface IDocumentReader { Task Read(Uri uri, string documentIdentifier, string? mediaType = null, CancellationToken cancellationToken = default); + + Task ReadWithPdfPig(Uri uri, string documentIdentifier, string? mediaType = null, CancellationToken cancellationToken = default); } diff --git a/src/Sql.SemanticSearch.Core/Chunking/PdfDocumentReader.cs b/src/Sql.SemanticSearch.Core/Chunking/PdfDocumentReader.cs index cffedcb..71d2a66 100644 --- a/src/Sql.SemanticSearch.Core/Chunking/PdfDocumentReader.cs +++ b/src/Sql.SemanticSearch.Core/Chunking/PdfDocumentReader.cs @@ -1,6 +1,7 @@ using Microsoft.Extensions.DataIngestion; using Sql.SemanticSearch.Core.ArXiv.Interfaces; using Sql.SemanticSearch.Core.Chunking.Interfaces; +using UglyToad.PdfPig; namespace Sql.SemanticSearch.Core.Chunking; @@ -10,7 +11,7 @@ public class PdfDocumentReader( { private readonly IArxivApiClient _arxivApiClient = arxivApiClient; private readonly MarkItDownMcpReader _markItDownMcp = markItDownMcp; - + public async Task Read(Uri uri, string documentIdentifier, string? mediaType = null, CancellationToken cancellationToken = default) { using var stream = await _arxivApiClient.DownloadPdfToMemoryStream(uri, cancellationToken); @@ -22,4 +23,37 @@ public async Task Read(Uri uri, string documentIdentifier, st return ingestionDocument; } + + public async Task ReadWithPdfPig(Uri uri, string documentIdentifier, string? mediaType = null, CancellationToken cancellationToken = default) + { + using var stream = await _arxivApiClient.DownloadPdfToMemoryStream(uri, cancellationToken); + stream.Seek(0, SeekOrigin.Begin); + + using var pdfDocument = PdfDocument.Open(stream); + + var fullText = string.Join( + Environment.NewLine + Environment.NewLine, + pdfDocument.GetPages().Select(page => page.Text)); + + var rootSection = new IngestionDocumentSection(fullText); + + foreach (var page in pdfDocument.GetPages()) + { + var pageText = page.Text; + if (string.IsNullOrWhiteSpace(pageText)) + { + continue; + } + + rootSection.Elements.Add(new IngestionDocumentParagraph(pageText) + { + Text = pageText, + }); + } + + return new IngestionDocument(documentIdentifier) + { + Sections = { rootSection } + }; + } } diff --git a/src/Sql.SemanticSearch.Core/Chunking/SqlServerChunkWriter.cs b/src/Sql.SemanticSearch.Core/Chunking/SqlServerChunkWriter.cs new file mode 100644 index 0000000..3fd4b87 --- /dev/null +++ b/src/Sql.SemanticSearch.Core/Chunking/SqlServerChunkWriter.cs @@ -0,0 +1,163 @@ +using Microsoft.Extensions.DataIngestion; +using Microsoft.Extensions.Logging; +using Polly.Registry; +using Sql.SemanticSearch.Core.Configuration; +using Sql.SemanticSearch.Core.Data.Interfaces; +using Sql.SemanticSearch.Shared; + +namespace Sql.SemanticSearch.Core.Chunking; + +/// Writes ingestion chunks to SQL Server, generating embeddings via AI_GENERATE_EMBEDDINGS. +public sealed class SqlServerChunkWriter( + IDatabaseConnection databaseConnection, + ResiliencePipelineProvider resiliencePipelineProvider, + AISettings aiSettings, + ILogger logger, + VectorStoreWriterOptions? options = default) : IngestionChunkWriter +{ + private readonly IDatabaseConnection _databaseConnection = databaseConnection; + private readonly ResiliencePipelineProvider _resiliencePipelineProvider = resiliencePipelineProvider; + private readonly AISettings _aiSettings = aiSettings; + private readonly ILogger _logger = logger; + private readonly VectorStoreWriterOptions _options = options ?? new VectorStoreWriterOptions(); + + private static readonly Action _logChunkSaved = + LoggerMessage.Define( + LogLevel.Debug, + new EventId(0, nameof(SqlServerChunkWriter)), + "Saved chunk {ChunkId} for document {DocumentId}."); + + public override async Task WriteAsync( + IAsyncEnumerable> chunks, + CancellationToken cancellationToken = default) + { + ArgumentNullException.ThrowIfNull(chunks); + + var resiliencePipeline = _resiliencePipelineProvider.GetPipeline(ResiliencePipelineNames.SqlServerRetry); + + IReadOnlyList? preExistingKeys = null; + + await foreach (var chunk in chunks.WithCancellation(cancellationToken)) + { + var documentId = await LookupDocumentIdAsync(chunk.Document.Identifier, cancellationToken); + preExistingKeys ??= await GetPreExistingChunksIdsAsync(documentId, cancellationToken).ConfigureAwait(false); + + await resiliencePipeline.ExecuteAsync( + async ct => + { + using var connection = _databaseConnection.CreateConnection(); + connection.Open(); + using var transaction = connection.BeginTransaction(); + + var chunkId = await SaveDocumentChunkAsync(documentId, chunk.Content, transaction); + await SaveDocumentChunkEmbeddingAsync(chunkId, transaction); + _logChunkSaved(_logger, chunkId, documentId, null); + + transaction.Commit(); + }, + cancellationToken); + } + + if (preExistingKeys?.Count > 0) + { + await DeleteAsync(preExistingKeys, cancellationToken).ConfigureAwait(false); + } + } + + private async Task LookupDocumentIdAsync(string arxivId, CancellationToken cancellationToken) + { + var resiliencePipeline = _resiliencePipelineProvider.GetPipeline(ResiliencePipelineNames.SqlServerRetry); + + return await resiliencePipeline.ExecuteAsync( + async ct => + { + var id = await _databaseConnection.ExecuteScalarAsync( + "SELECT [Id] FROM dbo.Documents WHERE [ArxivId] = @ArxivId", + new { ArxivId = arxivId }); + + return id ?? throw new InvalidOperationException( + $"Document with ArxivId '{arxivId}' not found in the database."); + }, + cancellationToken); + } + + private async Task SaveDocumentChunkAsync(int documentId, string content, System.Data.IDbTransaction transaction) => + await _databaseConnection.ExecuteScalarAsync( + """ + INSERT INTO dbo.DocumentChunks ([DocumentId], [Content]) + VALUES (@DocumentId, @Content); + + SELECT CAST(SCOPE_IDENTITY() as int); + """, + new { DocumentId = documentId, Content = content }, + transaction: transaction); + + /* Note: Embedding model is *NOT* a SQL injection risk, it must be hard-coded so we have to use the settings value. */ + private async Task SaveDocumentChunkEmbeddingAsync(int chunkId, System.Data.IDbTransaction transaction) => + await _databaseConnection.ExecuteAsync( + $""" + INSERT INTO dbo.DocumentChunkEmbeddings ([Id], [Embedding]) + SELECT @Id, AI_GENERATE_EMBEDDINGS([Content] USE MODEL {_aiSettings.ExternalEmbeddingModel}) + FROM dbo.DocumentChunks + WHERE [Id] = @Id; + """, + new { Id = chunkId }, + transaction: transaction); + + private async Task> GetPreExistingChunksIdsAsync(int documentId, CancellationToken cancellationToken) + { + if (!_options.IncrementalIngestion) + { + return []; + } + + List keys = []; + + var resiliencePipeline = _resiliencePipelineProvider.GetPipeline(ResiliencePipelineNames.SqlServerRetry); + + await resiliencePipeline.ExecuteAsync( + async ct => + { + var ids = await _databaseConnection.QueryAsync( + "SELECT [Id] FROM dbo.DocumentChunks WHERE [DocumentId] = @DocumentId", + new { DocumentId = documentId }); + + keys.AddRange(ids); + }, + cancellationToken); + + return keys; + } + + + private async Task DeleteAsync(IReadOnlyList keys, CancellationToken cancellationToken) + { + if (keys.Count == 0) + { + return; + } + + var resiliencePipeline = _resiliencePipelineProvider.GetPipeline(ResiliencePipelineNames.SqlServerRetry); + await resiliencePipeline.ExecuteAsync( + async context => + { + using var connection = _databaseConnection.CreateConnection(); + connection.Open(); + using var transaction = connection.BeginTransaction(); + + await _databaseConnection.ExecuteAsync( + """ + DELETE FROM dbo.DocumentChunkEmbeddings + WHERE [Id] IN @Ids; + + DELETE FROM dbo.DocumentChunks + WHERE [Id] IN @Ids; + """, + new { Ids = keys }, + transaction: transaction); + + transaction.Commit(); + }, + cancellationToken); + } +} diff --git a/src/Sql.SemanticSearch.Core/Sql.SemanticSearch.Core.csproj b/src/Sql.SemanticSearch.Core/Sql.SemanticSearch.Core.csproj index bd0c768..5cfab96 100644 --- a/src/Sql.SemanticSearch.Core/Sql.SemanticSearch.Core.csproj +++ b/src/Sql.SemanticSearch.Core/Sql.SemanticSearch.Core.csproj @@ -2,15 +2,18 @@ + - - + + + + @@ -18,6 +21,7 @@ + diff --git a/src/Sql.SemanticSearch.Ingestion.Functions/Program.cs b/src/Sql.SemanticSearch.Ingestion.Functions/Program.cs index 65cdba5..aebe2b8 100644 --- a/src/Sql.SemanticSearch.Ingestion.Functions/Program.cs +++ b/src/Sql.SemanticSearch.Ingestion.Functions/Program.cs @@ -54,7 +54,7 @@ .AddSingleton(aiSettings) .AddTransient() .AddTransient() - + .AddTransient, SqlServerChunkWriter>() .AddTransient(); builder.Services.AddHttpClient(client => diff --git a/src/Sql.SemanticSearch.Ingestion.Functions/Sql.SemanticSearch.Ingestion.Functions.csproj b/src/Sql.SemanticSearch.Ingestion.Functions/Sql.SemanticSearch.Ingestion.Functions.csproj index a1ba843..1c53023 100644 --- a/src/Sql.SemanticSearch.Ingestion.Functions/Sql.SemanticSearch.Ingestion.Functions.csproj +++ b/src/Sql.SemanticSearch.Ingestion.Functions/Sql.SemanticSearch.Ingestion.Functions.csproj @@ -18,6 +18,7 @@ +